Skip to content

Commit

Permalink
#8 fix : remove category field from products (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
0321minji authored Jan 20, 2024
1 parent 833013b commit c514803
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 20 deletions.
7 changes: 0 additions & 7 deletions products/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
from . import models


@admin.register(models.Category)
class Category(admin.ModelAdmin):

""" """

pass


@admin.register(models.Product)
class Product(admin.ModelAdmin):
Expand Down
18 changes: 18 additions & 0 deletions products/migrations/0003_product_like_cnt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0 on 2024-01-13 05:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('products', '0002_alter_productkeyword_product_and_more'),
]

operations = [
migrations.AddField(
model_name='product',
name='like_cnt',
field=models.PositiveIntegerField(default=0),
),
]
20 changes: 20 additions & 0 deletions products/migrations/0004_product_likeuser_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0 on 2024-01-13 05:59

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('products', '0003_product_like_cnt'),
]

operations = [
migrations.AddField(
model_name='product',
name='likeuser_set',
field=models.ManyToManyField(blank=True, related_name='liked_products', to=settings.AUTH_USER_MODEL),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.0 on 2024-01-20 05:23

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('products', '0004_product_likeuser_set'),
]

operations = [
migrations.RemoveField(
model_name='product',
name='category',
),
migrations.RemoveField(
model_name='product',
name='like_cnt',
),
migrations.RemoveField(
model_name='product',
name='likeuser_set',
),
migrations.DeleteModel(
name='Category',
),
]
5 changes: 1 addition & 4 deletions products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
from core.models import TimeStampedModel
# Create your models here.

class Category(models.Model):
name = models.CharField(max_length=100, blank= False)

class Product(TimeStampedModel):
name = models.CharField(max_length=100)
category = models.ForeignKey('Category', related_name='products', on_delete=models.CASCADE, null=False, blank=False)
#category = models.ForeignKey('Category', related_name='products', on_delete=models.CASCADE, null=False, blank=False)
basic_price = models.CharField(max_length=500, blank = False)
option = models.TextField(blank = True)
info = models.TextField(blank = True)
Expand Down
30 changes: 24 additions & 6 deletions products/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.conf import settings

from products.models import Category, Product, ProductKeyword, ProductPhoto
from products.models import Product, ProductKeyword, ProductPhoto
from users.models import User
from .selectors import ProductSelector
#from core.exceptions import ApplicationError

class ProductCoordinatorService:
def __init__(self, user:User):
self.user=user
#pass
@transaction.atomic
def create(self, category: str, name : str,keywords : list[str],basic_price : str,option : str,
def create(self, name : str,keywords : list[str],basic_price : str,option : str,
product_photos : list[str],info : str,notice : str,period : str,transaction_direct : bool,
transaction_package : bool,refund : str,
) -> Product:
product_service=ProductService()

product= product_service.create(
reformer=self.user,
category=category,
name=name,
basic_price=basic_price,
option=option,
Expand All @@ -50,13 +50,31 @@ def __init__(self):
pass

@staticmethod
def create(category: str,name : str,basic_price : str,option : str,info : str,notice : str,
def like_or_dislike(product:Product, user: User)-> bool:
if ProductSelector.likes(product=product, user=user):
product.likeuser_set.remove(user)
product.like_cnt-=1

product.full_clean()
product.save()

return False
else:
product.likeuser_set.add(user)
product.like_cnt +=1

product.full_clean()
product.save()

return True


@staticmethod
def create(name : str,basic_price : str,option : str,info : str,notice : str,
period : str,transaction_direct : bool,transaction_package : bool,refund : str, reformer : User):
category = get_object_or_404(Category, id=category)

product = Product(
name = name,
category = category,
basic_price = basic_price,
option = option,
info = info,
Expand Down
4 changes: 1 addition & 3 deletions products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from users.models import User

from .models import Category, Product
from .models import Product
from .services import ProductCoordinatorService, ProductPhotoService, ProductKeywordService, ProductService


Expand All @@ -16,7 +16,6 @@ class ProductCreateApi(APIView):

class ProductCreateInputSerializer(serializers.Serializer):
name = serializers.CharField()
category = serializers.CharField()
keywords = serializers.ListField(required=False)
basic_price = serializers.CharField()
option = serializers.CharField()
Expand All @@ -37,7 +36,6 @@ def post(self,request):

product = service.create(
name=data.get('name'),
category=data.get('category'),
keywords=data.get('keywords', []),
basic_price=data.get('basic_price'),
option=data.get('option'),
Expand Down

0 comments on commit c514803

Please sign in to comment.