def get(self, obj_id=None): """ Gets booking(s). Returns all bookings if no obj_id is passed --- tags: - bookings parameters: - in: path name: obj_id definitions: - schema: id: Booking required: - customerID - room_number optional: - is_active properties: customerID: type: string description: the customer's id room_number: type: integer description: the room's number is_booked: type: boolean description: the room's booking status responses: 200: description: Returns the specified booking or a list of bookings $ref: '#/definitions/Booking' """ if not obj_id: query = Booking.query().fetch() output = self.output_fields output.update(self.resource_fields) resp = { 'results': marshal(query, output), 'count': len(query) } return resp, 200 try: booking = Booking.get_by_id(int(obj_id)) if not booking: abort(404, message="Booking with key ({}) not found".format(obj_id)) output = self.output_fields output.update(self.resource_fields) return marshal(booking, output), 200 except Exception: abort(404, message="Booking with key ({}) not found".format(obj_id))
def delete(self, obj_id): """ DELETE booking Delete a booking --- tags: - bookings parameters: - in: path name: obj_id schema: id: Booking responses: 204: description: Booking deleted """ try: booking = Booking.get_by_id(int(obj_id)) booking.key.delete() return {"status": "Booking with id - {} successfully deleted" % obj_id}, 204 except Exception: raise CustomException(code=400, name='Validation Failed', data={"description": 'DELETE failed for Booking with ' 'key {}'.format(obj_id)})
def post(self, obj_id=None): """ Create a new booking/ Update a booking --- tags: - bookings parameters: - in: path name: obj_id - in: body name: body schema: id: Booking required: - room_number - customerID optional: - is_active properties: customerID: type: string description: customer id room_number: type: integer description: room number is_active: type: boolean description: room booked responses: 201: description: Booking created """ data = self.prepare_data() if obj_id: form = UpdateBookingForm(data, csrf_enabled=False) if form.validate(): booking = Booking.get_by_id(int(obj_id)) if not booking: abort(404, message="Booking with key ({}) not found".format(obj_id)) booking.is_active = form.is_active.data booking.put() room = Room.query(Room.number == booking.room_number).get() room.is_booked = True if booking.is_active is True else False room.put() output = self.output_fields output.update(self.resource_fields) return marshal(booking, output), 200 error_data = self.prepare_errors(form.errors) raise CustomException(code=400, name='Validation Failed', data=error_data) else: form = BookingForm(data, csrf_enabled=False) if form.validate(): booking = Booking(customerID=form.customerID.data, room_number=int(form.room_number.data), is_active=True) booking.put() booking.id = str(booking.key.id()) booking.put() room = Room.query(Room.number == booking.room_number).get() room.is_booked = True if booking.is_active is True else False room.put() output = self.output_fields output.update(self.resource_fields) return marshal(booking, output), 201 error_data = self.prepare_errors(form.errors) raise CustomException(code=400, name='Validation Failed', data=error_data)