Skip to content

Commit

Permalink
#100 feat : make land like&dislike api (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
0321minji authored Sep 2, 2024
1 parent e852de6 commit 73bb4fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lands/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
19 changes: 18 additions & 1 deletion lands/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lands/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

]
28 changes: 27 additions & 1 deletion lands/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,)

Expand Down

0 comments on commit 73bb4fb

Please sign in to comment.