def put_restaurant_table(restaurant_id, table_id): """ Return a specific restaurant (request by id) GET /restaurants/{restaurant_id}/tables/{table_id} Status Codes: 200 - OK 404 - Restaurant not found 409 - The table has pending reservations tha conflict with the new capacity, those must be deleted first 500 - DB error """ req = request.json q = db.session.query(Table).filter(Table.id == table_id and Table.restaurant_id == restaurant_id).first() if q is None: return Error404("Restaurant or Table not found").get() array,code = get_future_bookings(restaurant_id, table_id) if code == 200: for booking in array: if booking["number_of_people"] > req["capacity"]: return Error409("The table has pending reservations tha conflict with the new capacity, those must be deleted first").get() else: return Error500().get() table_id2 = edit_table(table_id, req) if table_id != table_id2: return Error500().get() restaurant, status_code = get_restaurant_table(restaurant_id, table_id) if status_code == 200: return restaurant, 200 else: # unexpected error return Error500().get()
def delete_restaurant_table(restaurant_id, table_id): """ Delete the restaurant table specified by the id. DELETE /restaurants/{restaurant_id}/tables/{table_id} Deletion is only possible if the restaurant has not yet passed. Otherwise it remains stored (necessary for contact tracing) Status Codes: 204 - Deleted 404 - Restaurant not found 409 - The table has pending reservations, those must be deleted first 500 - Error with the database or the other bookings service """ q = db.session.query(Table).filter(Table.id == table_id and Table.restaurant_id == restaurant_id).first() if q is None: return Error404("Restaurant or Table not found").get() array,code = get_future_bookings(restaurant_id, table_id) if code != 200: return Error500().get() # Cannot connect to the bookings service if len(array) != 0: return Error409("The table has pending reservations, those must be deleted first").get() if not del_table(table_id): return Error500().get() # DB error return NoContent, 204
def post_restaurant_table(restaurant_id): """ Add a new table for the restaurant. POST /restaurants/{restaurant_id}/tables Returns the restaurant if it can be made, otherwise returns an error message. Requires a json object with: - capacity Status Codes: 201 - The restaurant has been created 400 - Data error 404 - Restaurant not found 500 - DB error """ req = request.json r = db.session.query(Restaurant).filter(Restaurant.id == restaurant_id).first() if r is None: return Error404("Restaurant not found").get() table_id = add_table(req, restaurant_id) table, status_code = get_restaurant_table(restaurant_id, table_id) if status_code == 200: return table, 201 else: # unexpected error return Error500().get()
def get_restaurant_tables(restaurant_id, capacity=None): """ Return the tables of a restaurant (request by id) GET /restaurants/{restaurant_id}/tables?capacity=CAPACITY capacity is optional and specify the minimum capacity the returned tables should have Status Codes: 200 - OK 204 - No tables with such capacity or no tables 404 - Restaurant not found """ q = db.session.query(Restaurant).filter(Restaurant.id == restaurant_id) if q is None: return Error404("Restaurant not found").get() q = db.session.query(Table).filter(Table.restaurant_id == restaurant_id) if capacity is not None: q = q.filter(Table.capacity >= capacity) q = q.all() if len(q)==0: return NoContent, 204 return [q.dump() for q in q], 200
def valid_rating(obj, rest_id): #check restaurant existing restaurant = db.session.query(Restaurant).filter_by(id=rest_id).first() if restaurant is None: return Error404("Restaurant not found").get() #check already voted q = db.session.query(Rating).filter_by(restaurant_id=rest_id) rating = q.filter_by(rater_id=obj["rater_id"]).first() if rating is not None: return Error400("Restaurant already rated by the user").get() return None
def get_restaurant_rating(restaurant_id): """ Return the rating of a specific restaurant (request by id) GET /restaurants/{restaurant_id}/rate Status Codes: 200 - OK 404 - Restaurant not found """ q = db.session.query(Restaurant).filter_by(id = restaurant_id).first() if q is None: return Error404("Restaurant not found").get() return q.dump_rating(), 200
def get_restaurant_table(restaurant_id, table_id): """ Return a specific restaurant (request by id) GET /restaurants/{restaurant_id}/tables/{table_id} Status Codes: 200 - OK 400 - Data error 404 - Restaurant or Table not found 500 - DB error """ q = db.session.query(Table).filter(Table.id == table_id and Table.restaurant_id == restaurant_id).first() if q is None: return Error404("Restaurant or Table not found").get() return q.dump(), 200
def put_restaurant(restaurant_id): """ Return a specific restaurant (request by id) GET /restaurants/{restaurant_id} Status Codes: 200 - OK 404 - Restaurant not found 409 - The restaurant has pending reservations tha conflict with the new times, those must be deleted first 500 - DB error """ p = db.session.query(Restaurant).filter_by(id = restaurant_id).first() if p is None: return Error404("Restaurant not found").get() req = request.json err = valid_openings(req["first_opening_hour"],req["first_closing_hour"],req["second_opening_hour"],req["second_closing_hour"]) if err is not None: return err array,code = get_future_bookings(restaurant_id) if code == 200: for booking in array: hour = dateutil.parser.parse(booking["booking_datetime"]).hour() if not (req["first_opening_hour"] <= hour <= req["first_closing_hour"] or req["second_opening_hour"] <= hour <= req["second_closing_hour"]): return Error409("The restaurant has pending reservations tha conflict with the new times, those must be deleted first").get() else: return Error500().get() rest_id = edit_restaurant(restaurant_id, req) restaurant, status_code = get_restaurant(rest_id) if status_code == 200: return restaurant, 200 else: # unexpected error return Error500().get()