Exemple #1
0
 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)
Exemple #2
0
 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)
Exemple #3
0
 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)
Exemple #4
0
 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
Exemple #5
0
 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)
Exemple #6
0
 def update(self, instance, validated_data):
     return RoomService.save(validated_data, instance)
Exemple #7
0
 def create(self, validated_data):
     return RoomService.save(validated_data)