From 73bb4fbefb66e6441790ef2d68f02c753d88dbf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=8D?= <79096808+0321minji@users.noreply.github.com> Date: Mon, 2 Sep 2024 13:30:43 +0900 Subject: [PATCH] #100 feat : make land like&dislike api (#103) --- lands/selectors.py | 4 ++++ lands/services.py | 19 ++++++++++++++++++- lands/urls.py | 2 ++ lands/views.py | 28 +++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lands/selectors.py b/lands/selectors.py index 115349e..fa218cf 100644 --- a/lands/selectors.py +++ b/lands/selectors.py @@ -71,6 +71,10 @@ def get_user_items(user_email): user_items=Item.objects.filter(users__email=user_email) return user_items + @staticmethod + def likes(land:Land, user:User): + return land.likeuser_set.filter(pk=user.pk).exists() + class LocationSelector: @staticmethod def show(self, location:Location): diff --git a/lands/services.py b/lands/services.py index 5e14908..35f51ec 100644 --- a/lands/services.py +++ b/lands/services.py @@ -5,7 +5,7 @@ import io, time, uuid from django.conf import settings from Cognisle.settings import development -from .selectors import ItemSelector +from .selectors import ItemSelector, LandSelector from .models import Land,Location,Item, ItemImage from users.models import User from core.utils import s3_file_upload_by_file_data @@ -54,6 +54,23 @@ def create(background:str,user:User): return land + @staticmethod + def like_or_dislike(land:Land, user:User) -> bool: + if LandSelector.likes(land=land,user=user): + land.likeuser_set.remove(user) + land.like_cnt-=1 + + land.full_clean() + land.save() + return False + else: + land.likeuser_set.add(user) + land.like_cnt+=1 + + land.full_clean() + land.save() + return True + class ItemImageService: def __init__(self): pass diff --git a/lands/urls.py b/lands/urls.py index 827b1c2..e94a1dc 100644 --- a/lands/urls.py +++ b/lands/urls.py @@ -12,4 +12,6 @@ path('items/game/',ItemGetApi.as_view(),name='item_get'), #path('items/list/',ItemListApi.as_view(),name='item_list'), #path('items/',AllItemListApi.as_view(),name='all_item_list'), + path('likes/',LandLikeApi.as_view(),name='land_like'), + ] \ No newline at end of file diff --git a/lands/views.py b/lands/views.py index da2c4f5..1cda099 100644 --- a/lands/views.py +++ b/lands/views.py @@ -13,7 +13,7 @@ from .selectors import ItemSelector, LandSelector from rest_framework.exceptions import PermissionDenied from .models import Land, Location, Item, ItemImage -from .services import LandCoordinatorService, ItemImageService, ItemService +from .services import LandCoordinatorService, ItemImageService, ItemService, LandService from drf_yasg import openapi from drf_yasg.utils import swagger_auto_schema @@ -205,6 +205,32 @@ def get(self, request): 'items':output_serializer.data.get('items'), }}, status=status.HTTP_200_OK) +class LandLikeApi(APIView): + permission_classes=(IsAuthenticated,) + + class UserLandItemListInputSerializer(serializers.Serializer): + email = serializers.CharField() + + def put(self,request): + serializers=self.UserLandItemListInputSerializer(data=request.data) + serializers.is_valid(raise_exception=True) + data=serializers.validated_data + + user=get_object_or_404(User,email=data.get('email')) + land=get_object_or_404(Land,user=user) + + likes=LandService.like_or_dislike( + land=land, + user=user + ) + + return Response({ + "status":"success", + "data":{'land_owner':land.user.email, + 'likes':likes, + 'like_cnt':land.like_cnt} + },status=status.HTTP_200_OK) + class ItemImageCreateApi(APIView): permission_classes=(IsAuthenticated,)