From f0061cc81513694b9020e27dd3c800e7ca644cd2 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Tue, 8 Oct 2024 22:19:54 +0530 Subject: [PATCH 1/4] Add get api for book and author --- library/urls.py | 2 ++ library/views.py | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) 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 From 236d58fb47c74ea0a8ce96c0ee222cd4bf1353b1 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Thu, 24 Oct 2024 22:19:49 +0530 Subject: [PATCH 2/4] Add ecommerce store --- .gitignore | 1 + ecommerce_store/__init__.py | 0 ecommerce_store/admin.py | 3 +++ ecommerce_store/apps.py | 6 +++++ ecommerce_store/migrations/0001_initial.py | 31 ++++++++++++++++++++++ ecommerce_store/migrations/__init__.py | 0 ecommerce_store/models.py | 10 +++++++ ecommerce_store/serializer.py | 12 +++++++++ ecommerce_store/tests.py | 3 +++ ecommerce_store/urls.py | 7 +++++ ecommerce_store/views.py | 17 ++++++++++++ helloworld/settings.py | 3 ++- helloworld/urls.py | 1 + 13 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 ecommerce_store/__init__.py create mode 100644 ecommerce_store/admin.py create mode 100644 ecommerce_store/apps.py create mode 100644 ecommerce_store/migrations/0001_initial.py create mode 100644 ecommerce_store/migrations/__init__.py create mode 100644 ecommerce_store/models.py create mode 100644 ecommerce_store/serializer.py create mode 100644 ecommerce_store/tests.py create mode 100644 ecommerce_store/urls.py create mode 100644 ecommerce_store/views.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7e99e367 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ 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..61df10a2 --- /dev/null +++ b/ecommerce_store/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from ecommerce_store import views + +urlpatterns=[ + path('createcategory/', views.create_author) +] \ No newline at end of file diff --git a/ecommerce_store/views.py b/ecommerce_store/views.py new file mode 100644 index 00000000..70d39ceb --- /dev/null +++ b/ecommerce_store/views.py @@ -0,0 +1,17 @@ +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 serializer import CategorySerializer, ProductSerializer + +@api_view(['POST']) +def create_category(request): + category = CategorySerializer(data = request.data) + if category.is_valid(): + category = category.save() + return Response(category.data, status=status.HTTP_201_CREATED) + + return Response(category.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY) \ No newline at end of file diff --git a/helloworld/settings.py b/helloworld/settings.py index 4ada6bff..5499bfa7 100644 --- a/helloworld/settings.py +++ b/helloworld/settings.py @@ -38,7 +38,8 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'tryHello', - 'library' + 'library', + 'ecommerce_store' ] MIDDLEWARE = [ diff --git a/helloworld/urls.py b/helloworld/urls.py index faac64cd..00c2de87 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.py')) ] From 4167718966123df8525b4edd21314fcaf1f17895 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Sun, 27 Oct 2024 20:44:45 +0530 Subject: [PATCH 3/4] Add CRUD operations for products api --- ecommerce_store/urls.py | 7 ++- ecommerce_store/views.py | 47 +++++++++++++++++-- helloworld/settings.py | 3 +- helloworld/urls.py | 2 +- library/migrations/0003_book_test_column.py | 19 ++++++++ .../migrations/0004_alter_book_test_column.py | 18 +++++++ .../0005_remove_book_test_column.py | 17 +++++++ 7 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 library/migrations/0003_book_test_column.py create mode 100644 library/migrations/0004_alter_book_test_column.py create mode 100644 library/migrations/0005_remove_book_test_column.py diff --git a/ecommerce_store/urls.py b/ecommerce_store/urls.py index 61df10a2..ea2a2bac 100644 --- a/ecommerce_store/urls.py +++ b/ecommerce_store/urls.py @@ -3,5 +3,10 @@ from ecommerce_store import views urlpatterns=[ - path('createcategory/', views.create_author) + 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 index 70d39ceb..596c8e20 100644 --- a/ecommerce_store/views.py +++ b/ecommerce_store/views.py @@ -5,13 +5,54 @@ from rest_framework.decorators import api_view from rest_framework.response import Response -from serializer import CategorySerializer, ProductSerializer +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 = category.save() + category.save() return Response(category.data, status=status.HTTP_201_CREATED) - return Response(category.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY) \ No newline at end of file + 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 5499bfa7..4f6b39db 100644 --- a/helloworld/settings.py +++ b/helloworld/settings.py @@ -39,7 +39,8 @@ 'django.contrib.staticfiles', 'tryHello', 'library', - 'ecommerce_store' + 'ecommerce_store', + 'django_extensions' ] MIDDLEWARE = [ diff --git a/helloworld/urls.py b/helloworld/urls.py index 00c2de87..1ea46f2f 100644 --- a/helloworld/urls.py +++ b/helloworld/urls.py @@ -21,5 +21,5 @@ path('admin/', admin.site.urls), path('', include('tryHello.urls')), path('api/', include('library.urls')), - path('api/v2/', include('ecommerce_store.py')) + 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', + ), + ] From c22dc961187107524a2a14717f4c3a4206391b9e Mon Sep 17 00:00:00 2001 From: Abhishek Date: Mon, 28 Oct 2024 21:32:50 +0530 Subject: [PATCH 4/4] use environment variable --- .gitignore | 3 ++- helloworld/settings.py | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7e99e367..0a1958cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.pyc \ No newline at end of file +*.pyc +.env \ No newline at end of file diff --git a/helloworld/settings.py b/helloworld/settings.py index 4f6b39db..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 @@ -73,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') } }