Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
.env
Empty file added ecommerce_store/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions ecommerce_store/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions ecommerce_store/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class EcommerceStoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'ecommerce_store'
31 changes: 31 additions & 0 deletions ecommerce_store/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
Empty file.
10 changes: 10 additions & 0 deletions ecommerce_store/models.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions ecommerce_store/serializer.py
Original file line number Diff line number Diff line change
@@ -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__'
3 changes: 3 additions & 0 deletions ecommerce_store/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
12 changes: 12 additions & 0 deletions ecommerce_store/urls.py
Original file line number Diff line number Diff line change
@@ -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/<int:product_id>', views.get_product),
path('delete_product/<int:product_id>', views.delete_product)
]
58 changes: 58 additions & 0 deletions ecommerce_store/views.py
Original file line number Diff line number Diff line change
@@ -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)

22 changes: 18 additions & 4 deletions helloworld/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,7 +39,9 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'tryHello',
'library'
'library',
'ecommerce_store',
'django_extensions'
]

MIDDLEWARE = [
Expand Down Expand Up @@ -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')
}
}

Expand Down
1 change: 1 addition & 0 deletions helloworld/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
]
19 changes: 19 additions & 0 deletions library/migrations/0003_book_test_column.py
Original file line number Diff line number Diff line change
@@ -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,
),
]
18 changes: 18 additions & 0 deletions library/migrations/0004_alter_book_test_column.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
17 changes: 17 additions & 0 deletions library/migrations/0005_remove_book_test_column.py
Original file line number Diff line number Diff line change
@@ -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',
),
]
2 changes: 2 additions & 0 deletions library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
urlpatterns = [
path('createbook/', views.create_book, name='createbook'),
path('createAuthor/', views.create_author, name='createauthor'),
path('getBook/<int:pk>', views.get_book_by_id, name = 'getbook'),
path('getAuthor/<int:pk>', views.get_author_by_id, name = 'getbook')
]
22 changes: 19 additions & 3 deletions library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from library.serializer import BookSerializer, AuthorSerializer


from .models import Book, Author
# Create your views here.

@api_view(['POST'])
Expand All @@ -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..
@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)