Fakultas Ilmu Komputer UI

Commit 9c03bec9 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

[GREEN] Implement Add and Update Foto Kegiatan

parent 2dee9919
......@@ -50,6 +50,12 @@ urlpatterns = [
path('lokasi/list-foto-kegiatan/<str:place_id>/<int:kegiatan_id>',
views_kegiatan.list_foto_kegiatan, name='list-foto-kegiatan'),
path('lokasi/add-foto-kegiatan/<str:place_id>/<int:kegiatan_id>',
views_kegiatan.add_foto_kegiatan, name='add-foto-kegiatan'),
path('lokasi/update-foto-kegiatan/<str:place_id>/<int:kegiatan_id>/<int:id>',
views_kegiatan.update_foto_kegiatan, name='update-foto-kegiatan'),
path('lokasi/list-kegiatan-by-latest-added/<int:start_index>/<int:query_limit>',
views_kegiatan.list_kegiatan_by_latest_added, name='list-kegiatan-by-latest-added'),
......
......@@ -95,6 +95,40 @@ def list_foto_kegiatan(request, place_id, kegiatan_id):
new_dict = {item['id']: dict(item) for item in data_response}
return JsonResponse(new_dict, status=HTTPStatus.OK)
@api_view(['POST'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def add_foto_kegiatan(request, place_id, kegiatan_id):
try:
kegiatan = Kegiatan.objects.get(lokasi__place_id=place_id, id=kegiatan_id)
data = request.data.dict()
data.update({"kegiatan": kegiatan.id})
serializer = FotoKegiatanSerializer(data=data)
serializer.is_valid(raise_exception=True)
serializer.save()
return JsonResponse(serializer.data, status=HTTPStatus.CREATED)
except Kegiatan.DoesNotExist:
raise NotFound(detail="Kegiatan doesn't exist")
@api_view(['PUT'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def update_foto_kegiatan(request, place_id, kegiatan_id, id):
try:
foto_kegiatan = FotoKegiatan.objects.get(kegiatan__lokasi__place_id=place_id,
kegiatan_id=kegiatan_id,
id=id)
serializer = FotoKegiatanSerializer(foto_kegiatan, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
return JsonResponse(serializer.data, status=HTTPStatus.ACCEPTED)
except FotoKegiatan.DoesNotExist:
raise NotFound(detail="Foto Kegiatan doesn't exist")
@api_view(['GET'])
@authentication_classes([])
@permission_classes([])
......
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