def delete(self, request, pk=None, *args, **kwargs): try: room = RoomService.get(pk=pk) if room: room_in_used = RoomBookingService.room_is_in_used(room.id) if room_in_used: return Response({'error': 'Room is already in used'}, status=status.HTTP_400_BAD_REQUEST) RoomService.delete(room) return Response(status=status.HTTP_204_NO_CONTENT) except Exception as ex: # todo: handle log return Response({'error': str(ex)}, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk, *args, **kwargs): try: data = request.data.copy() room = RoomService.get(pk=pk) serializer = self.get_serializer(room, data=data, partial=True) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) except Exception as ex: # todo: handle log return Response({'error': str(ex)}, status=status.HTTP_400_BAD_REQUEST)
def retrieve(self, request, pk, *args, **kwargs): try: room = RoomService.get(pk=pk) if room: serializer = self.get_serializer(room) data = serializer.data return Response(data, status=status.HTTP_200_OK) else: return Response(status=status.HTTP_400_BAD_REQUEST) except Exception as ex: # todo: handle log return Response({'error': str(ex)}, status=status.HTTP_400_BAD_REQUEST)
def validate(self, data): room_booking_id = Utils.safe_int(data.get('room_booking_id'), default=0) invalid_booking = False room_booking = RoomBookingService.get(room_booking_id) if not room_booking: invalid_booking = True if not invalid_booking: room = RoomService.get(room_booking.room_id) if not room: invalid_booking = True if invalid_booking: raise Exception("The movie plan is cancelled!") seat_number = Utils.safe_int(data.get('seat_number'), default=0) if not seat_number or seat_number > room.seats: raise Exception("The seat number is invalid!") customer_id = self.instance.id if self.instance else 0 if not TicketService.is_ticket_available(room_booking_id, seat_number, customer_id=customer_id): raise Exception("The seat number is already booked!") return data
def list(self, request, *args, **kwargs): rooms = RoomService.get_all_rooms() data = { 'rooms': self.get_serializer(rooms, many=True).data } return Response(data, status=status.HTTP_200_OK)
def update(self, instance, validated_data): return RoomService.save(validated_data, instance)
def create(self, validated_data): return RoomService.save(validated_data)