Fakultas Ilmu Komputer UI

Commit 4b9ececa authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

[REFACTOR] Refactor Like & Dislike API using Serializer and constraint

parent a72ee740
Pipeline #80593 passed with stages
in 8 minutes and 36 seconds
from rest_framework import serializers
from .models import Lokasi, Fasilitas, Kegiatan, KomentarKegiatan, FotoKegiatan
from .models import Lokasi, Fasilitas, Kegiatan, \
KomentarKegiatan, FotoKegiatan, Likes, Dislikes
import pytz
TIMEZONE_INDONESIA = {
......@@ -86,6 +87,17 @@ class FasilitasSerializerRead(FasilitasSerializer):
fields = FasilitasSerializer.Meta.fields + ('place_id', 'creator', "creator_email")
class LikesSerializer(serializers.ModelSerializer):
class Meta:
model = Likes
fields = '__all__'
class DislikesSerializer(serializers.ModelSerializer):
class Meta:
model = Dislikes
fields = '__all__'
class FotoKegiatanSerializer(serializers.ModelSerializer):
place_id = serializers.CharField(source='kegiatan.lokasi.place_id', read_only=True)
......
......@@ -10,7 +10,8 @@ from rest_framework import viewsets
from rest_framework.exceptions import ValidationError
from notification.utils import send_komentar_notification
from .serializers import LokasiSerializer, FasilitasSerializerRead, FasilitasSerializerWrite
from .serializers import LokasiSerializer, FasilitasSerializerRead,\
FasilitasSerializerWrite, LikesSerializer, DislikesSerializer
from .models import Lokasi, Fasilitas, Komentar, Likes, Dislikes
from .permissions import UserPermission
......@@ -136,35 +137,32 @@ def update_like_fasilitas(request, place_id, id, operation):
try:
fasilitas = Fasilitas.objects.get(lokasi__place_id=place_id, id=id)
user = request.user
try:
like = Likes.objects.get(fasilitas=fasilitas, user=user)
except Likes.DoesNotExist:
like = None
try:
dislike = Dislikes.objects.get(fasilitas=fasilitas, user=user)
except Dislikes.DoesNotExist:
dislike = None
serializer_kwargs = {"user": user.id, "fasilitas": fasilitas.id}
if operation == "like":
if like != None:
return JsonResponse({'response': "You have already liked this facility"}, status=HTTPStatus.ACCEPTED)
else:
Likes.objects.create(fasilitas=fasilitas, user=user)
if dislike != None:
dislike.delete()
try:
serializer = LikesSerializer(data=serializer_kwargs)
serializer.is_valid(raise_exception=True)
serializer.save()
Dislikes.objects.filter(**serializer_kwargs).delete()
except ValidationError:
return JsonResponse({'response': "You have already liked this facility"},
status=HTTPStatus.ACCEPTED)
elif operation == "dislike":
if dislike != None:
return JsonResponse({'response': "You have already disliked this facility"}, status=HTTPStatus.ACCEPTED)
else:
Dislikes.objects.create(fasilitas=fasilitas, user=user)
if like != None:
like.delete()
try:
serializer = DislikesSerializer(data=serializer_kwargs)
serializer.is_valid(raise_exception=True)
serializer.save()
Likes.objects.filter(**serializer_kwargs).delete()
except ValidationError:
return JsonResponse({'response': "You have already disliked this facility"},
status=HTTPStatus.ACCEPTED)
fasilitas.like = Likes.objects.filter(fasilitas=fasilitas).count()
fasilitas.dislike = Dislikes.objects.filter(
fasilitas=fasilitas).count()
return JsonResponse({'response': "You have successfuly {}d this facility".format(operation)}, status=HTTPStatus.CREATED)
except KeyError as missing_key:
return JsonResponse({'response': missing_key_message(str(missing_key))}, status=HTTPStatus.BAD_REQUEST)
fasilitas.save()
return JsonResponse({'response': "You have successfuly {}d this facility".format(operation)},
status=HTTPStatus.CREATED)
except Exception as error:
return JsonResponse({'response': str(error)}, status=HTTPStatus.NOT_FOUND)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment