def get(self, reservation_id): """ Deletes a reservation with a given reservation ID. --- tags: - Reservations API parameters: - in: path name: reservationID description: The reservation ID belonging to reservation to be canceled. required: true type: integer responses: 200: description: 200 OK schema: type: string 401: description: Reservation ID is not valid schema: type: string """ reservation = Reservations.query.filter_by(reservationID=reservation_id).first() if reservation is None: return "401 Reservation ID is not valid" dh = DAVHandler("http://127.0.0.1:5190/", CALDAV_ADMIN_USER, CALDAV_ADMIN_PASSWD, ) stock = Stock.query.filter_by(itemID=reservation.itemID).first() stock.stockQuantity += reservation.quantity db.session.delete(reservation) db.session.commit() dh.delete_event(reservation.username, reservation.reservationID) dh.delete_event(stock.provider_name, reservation.reservationID) return "200 OK"
from caldavHandler import DAVHandler __author__ = "mfernandes" dh = DAVHandler("http://127.0.0.1:5190/", "admin-caldav", "FZiFFEE1Cc6FXtC8M4bjekrtbqLXyjeM") # dh = DAVHandler("http://127.0.0.1:5190/testcaldav/meals.ics/", None, None) # add_event(self, uid, event_name, start_timestamp): dh.add_event("test-caldav", 123247, "Test5", 1447716529, 2)
def post(self, reservation_id): """ Updates the date or the quantity of a reservation with a give reservation ID. --- tags: - Reservations API parameters: - in: path name: reservationID description: reservation ID of the reservation needs to be updated required: true type: integer - in: body name: update parameters schema: type: object required: - quantity - timestamp properties: quantity: type: integer description: If the quantity needs to be updates this value must be != 0 default: 0 timestamp: type: integer description: If the date needs to be updates this value must be != 0 default: 0 responses: 200: description: 200 OK schema: type: string 401: description: Invalid parameter value schema: type: string 402: description: Reservation ID is not valid schema: type: string 403: description: Invalid stock schema: type: string """ if database_exists(DATABASE) is False: db.create_all() in_data = request.get_json(force=True) if in_data['quantity'] < 0 or in_data['timestamp'] < 0: return "401 Invalid parameter value" reservation = Reservations.query.filter_by(reservationID=reservation_id).first() if reservation is None: return "402 Reservation ID is not valid" dh = DAVHandler("http://127.0.0.1:5190/", CALDAV_ADMIN_USER, CALDAV_ADMIN_PASSWD, ) if in_data['quantity'] > 0: stock = Stock.query.filter_by(itemID=reservation.itemID).first() if in_data['quantity'] > reservation.quantity: more = in_data['quantity'] - reservation.quantity if more <= stock.stockQuantity: reservation.quantity = in_data['quantity'] stock.stockQuantity -= more else: return "403 Invalid stock" elif in_data['quantity'] < reservation.quantity: less = reservation.quantity - in_data['quantity'] reservation.quantity = in_data['quantity'] stock.stockQuantity += less dh.update_event(reservation.username, reservation.reservationID, stock.itemName, reservation.timestamp, in_data['quantity']) dh.update_event(stock.provider_name, reservation.reservationID, stock.itemName, reservation.timestamp, in_data['quantity']) if in_data['timestamp'] > 0: reservation.timestamp = in_data['timestamp'] dh.update_event(reservation.username, reservation.reservationID, stock.itemName, in_data['timestamp'], reservation.quantity) dh.update_event(stock.provider_name, stock.itemName, in_data['timestamp'], reservation.quantity) db.session.commit() return "200 OK"
def post(self): """ Set a new reservation on the service and returns the ID assigned to that reservations. --- tags: - Reservations API parameters: - in: body name: reservation schema: type: object required: - itemID - quantity - clientID - username - timestamp properties: itemID: type: integer description: item ID for identification quantity: type: integer description: quantity to be reserved clientID: type: integer description: ID of the client that wants to make the reservation username: type: string description: username of the user that wants to make the reservation timestamp: type: integer description: the date tha the reservations is scheduled. responses: 200: description: Returns the reservation ID that was generated. schema: type: object required: - reservationID properties: reservationID: type: integer description: The reservation ID generated 401: description: Invalid itemID schema: type: string 402: description: Invalid stock schema: type: string """ if database_exists(DATABASE) is False: db.create_all() in_data = request.get_json(force=True) stock = Stock.query.filter_by(itemID=in_data['itemID']).first() if stock is None: return "401 Invalid itemID" if stock.stockQuantity < in_data['quantity']: return "402 Invalid stock" res = Reservations(stock.itemID, in_data['quantity'], in_data['clientID'], in_data['username'], in_data['timestamp']) db.session.add(res) stock.stockQuantity -= in_data['quantity'] db.session.commit() reservation = Reservations.query.filter_by(itemID=stock.itemID, clientID=in_data['clientID']).first() dh = DAVHandler("http://127.0.0.1:5190/", CALDAV_ADMIN_USER, CALDAV_ADMIN_PASSWD, ) dh.add_event(reservation.username, reservation.reservationID, stock.itemName, in_data['timestamp'], in_data['quantity']) dh.add_event(stock.provider_name, reservation.reservationID, stock.itemName, in_data['timestamp'], in_data['quantity']) return {"reservationID": reservation.reservationID}