diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..0a1958cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +.env \ No newline at end of file diff --git a/ecommerce_store/__init__.py b/ecommerce_store/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ecommerce_store/admin.py b/ecommerce_store/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/ecommerce_store/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/ecommerce_store/apps.py b/ecommerce_store/apps.py new file mode 100644 index 00000000..780981ca --- /dev/null +++ b/ecommerce_store/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EcommerceStoreConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'ecommerce_store' diff --git a/ecommerce_store/migrations/0001_initial.py b/ecommerce_store/migrations/0001_initial.py new file mode 100644 index 00000000..616a68b8 --- /dev/null +++ b/ecommerce_store/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# Generated by Django 5.1.1 on 2024-10-24 16:12 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=30)), + ], + ), + migrations.CreateModel( + name='Product', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('price', models.DecimalField(decimal_places=2, max_digits=5)), + ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ecommerce_store.category')), + ], + ), + ] diff --git a/ecommerce_store/migrations/__init__.py b/ecommerce_store/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ecommerce_store/models.py b/ecommerce_store/models.py new file mode 100644 index 00000000..6187a43e --- /dev/null +++ b/ecommerce_store/models.py @@ -0,0 +1,10 @@ +from django.db import models + +# Create your models here. +class Category(models.Model): + name = models.CharField(max_length=30) + +class Product(models.Model): + name = models.CharField(max_length=100) + category = models.ForeignKey(Category, on_delete=models.CASCADE) + price = models.DecimalField(max_digits=5, decimal_places=2) diff --git a/ecommerce_store/serializer.py b/ecommerce_store/serializer.py new file mode 100644 index 00000000..c27df9af --- /dev/null +++ b/ecommerce_store/serializer.py @@ -0,0 +1,12 @@ +from rest_framework import serializers +from .models import Category, Product + +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = Category + fields = '__all__' + +class ProductSerializer(serializers.ModelSerializer): + class Meta: + model = Product + fields = '__all__' \ No newline at end of file diff --git a/ecommerce_store/tests.py b/ecommerce_store/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/ecommerce_store/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/ecommerce_store/urls.py b/ecommerce_store/urls.py new file mode 100644 index 00000000..ea2a2bac --- /dev/null +++ b/ecommerce_store/urls.py @@ -0,0 +1,12 @@ +from django.urls import path + +from ecommerce_store import views + +urlpatterns=[ + path('createcategory', views.create_category), + path('getcategories', views.get_categories), + path('createproduct', views.create_product), + path('getproducts', views.get_products), + path('getproduct/', views.get_product), + path('delete_product/', views.delete_product) +] \ No newline at end of file diff --git a/ecommerce_store/views.py b/ecommerce_store/views.py new file mode 100644 index 00000000..596c8e20 --- /dev/null +++ b/ecommerce_store/views.py @@ -0,0 +1,58 @@ +from django.shortcuts import render + +# Create your views here. +from rest_framework import status +from rest_framework.decorators import api_view +from rest_framework.response import Response + +from ecommerce_store.serializer import CategorySerializer, ProductSerializer + +from .models import Category, Product + +@api_view(['POST']) +def create_category(request): + category = CategorySerializer(data = request.data) + if category.is_valid(): + category.save() + return Response(category.data, status=status.HTTP_201_CREATED) + + return Response(category.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY) + +@api_view(['GET']) +def get_categories(request): + categories = Category.objects.all() + category_serializer = CategorySerializer(categories,many=True) + return Response(category_serializer.data, status=status.HTTP_200_OK) + +@api_view(['POST']) +def create_product(request): + product = ProductSerializer(data=request.data) + if product.is_valid(): + product.save() + return Response(product.data, status=status.HTTP_201_CREATED) + return Response(product.errors,status=status.HTTP_400_BAD_REQUEST) + +@api_view(['GET']) +def get_products(request): + products = Product.objects.all() + product_serializer = ProductSerializer(products, many=True) + return Response(product_serializer.data, status=status.HTTP_200_OK) + +@api_view(['GET']) +def get_product(request,product_id): + try: + product = Product.objects.get(pk=product_id) + except Product.DoesNotExist: + product = None + product_serializer = ProductSerializer(product) + return Response(product_serializer.data, status= status.HTTP_200_OK) + +@api_view(['DELETE']) +def delete_product(request,product_id): + try: + product = Product.objects.get(pk=product_id) + product.delete() + except Product.DoesNotExist: + return Response(status=status.HTTP_404_NOT_FOUND) + return Response({"message": "delete successfully"}, status= status.HTTP_200_OK) + diff --git a/helloworld/settings.py b/helloworld/settings.py index 4ada6bff..096ae740 100644 --- a/helloworld/settings.py +++ b/helloworld/settings.py @@ -11,6 +11,7 @@ """ from pathlib import Path +import environ # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -38,7 +39,9 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'tryHello', - 'library' + 'library', + 'ecommerce_store', + 'django_extensions' ] MIDDLEWARE = [ @@ -71,14 +74,25 @@ WSGI_APPLICATION = 'helloworld.wsgi.application' - +env = environ.Env() +environ.Env.read_env() # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases +# DATABASES = { +# 'default': { +# 'ENGINE': 'django.db.backends.sqlite3', +# 'NAME': BASE_DIR / 'db.sqlite3', +# } +# } DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + 'ENGINE': 'django.db.backends.mysql', + 'NAME': env('DB_NAME'), + 'HOST': env('DB_HOST'), + 'USER': env('DB_USER'), + 'PASSWORD': env('DB_PASSWORD'), + 'PORT': env('DB_PORT') } } diff --git a/helloworld/urls.py b/helloworld/urls.py index faac64cd..1ea46f2f 100644 --- a/helloworld/urls.py +++ b/helloworld/urls.py @@ -21,4 +21,5 @@ path('admin/', admin.site.urls), path('', include('tryHello.urls')), path('api/', include('library.urls')), + path('api/v2/', include('ecommerce_store.urls')) ] diff --git a/library/migrations/0003_book_test_column.py b/library/migrations/0003_book_test_column.py new file mode 100644 index 00000000..848c1b10 --- /dev/null +++ b/library/migrations/0003_book_test_column.py @@ -0,0 +1,19 @@ +# Generated by Django 5.1.1 on 2024-10-11 15:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('library', '0002_book_publish_date'), + ] + + operations = [ + migrations.AddField( + model_name='book', + name='test_column', + field=models.CharField(default=None, max_length=100), + preserve_default=False, + ), + ] diff --git a/library/migrations/0004_alter_book_test_column.py b/library/migrations/0004_alter_book_test_column.py new file mode 100644 index 00000000..8b18838d --- /dev/null +++ b/library/migrations/0004_alter_book_test_column.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.1 on 2024-10-11 15:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('library', '0003_book_test_column'), + ] + + operations = [ + migrations.AlterField( + model_name='book', + name='test_column', + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/library/migrations/0005_remove_book_test_column.py b/library/migrations/0005_remove_book_test_column.py new file mode 100644 index 00000000..1d77c065 --- /dev/null +++ b/library/migrations/0005_remove_book_test_column.py @@ -0,0 +1,17 @@ +# Generated by Django 5.1.1 on 2024-10-24 15:31 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('library', '0004_alter_book_test_column'), + ] + + operations = [ + migrations.RemoveField( + model_name='book', + name='test_column', + ), + ] diff --git a/library/urls.py b/library/urls.py index b4361e4d..60c88393 100644 --- a/library/urls.py +++ b/library/urls.py @@ -5,4 +5,6 @@ urlpatterns = [ path('createbook/', views.create_book, name='createbook'), path('createAuthor/', views.create_author, name='createauthor'), + path('getBook/', views.get_book_by_id, name = 'getbook'), + path('getAuthor/', views.get_author_by_id, name = 'getbook') ] \ No newline at end of file diff --git a/library/views.py b/library/views.py index 4f40abfa..ebafb803 100644 --- a/library/views.py +++ b/library/views.py @@ -5,7 +5,7 @@ from library.serializer import BookSerializer, AuthorSerializer - +from .models import Book, Author # Create your views here. @api_view(['POST']) @@ -30,5 +30,21 @@ def create_author(request): return Response(author_serializer.errors, status=status.HTTP_400_BAD_REQUEST) -# TODO: GET BOOK BY ID and return json response.. -# TODO: GET AUTHOR BY ID and return json response.. \ No newline at end of file +@api_view(['GET']) +def get_book_by_id(request,pk): + try: + book = Book.objects.get(pk=pk) + except Book.DoesNotExist: + return Response({"detail": "Not found."},status=status.HTTP_404_NOT_FOUND) + + book_serializer = BookSerializer(book) + return Response(book_serializer.data, status=status.HTTP_200_OK) + +@api_view(['GET']) +def get_author_by_id(request, pk): + try: + author = Author.objects.get(pk=pk) + except Author.DoesNotExist: + return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND) + author_serilizer = AuthorSerializer(author) + return Response(author_serilizer.data, status=status.HTTP_200_OK) \ No newline at end of file