def put(self, request, id): room = self.get_room(id) serializer = RoomListSerializer(instance=room, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'error': serializer.errors})
def post(self, request): serializer = RoomListSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def room_list(request): if request.method == 'GET': rooms = Room.objects.all() serializer = RoomListSerializer(rooms, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = RoomListSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def room_detail(request, id): try: room = Room.objects.get(id=id) except Room.DoesNotExist as e: return Response({'error': str(e)}) if request.method == 'GET': serializer = RoomListSerializer(room) return Response(serializer.data) elif request.method == 'PUT': serializer = RoomListSerializer(instance=room, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'error': serializer.errors}) elif request.method == 'DELETE': room.delete() return Response({'deleted': True})