def create_booking(): data = request.get_json() or {} if 'name' not in data or 'date' not in data or 'phone' not in data: return bad_request('name,date,phone are required') item = Booking() item.from_dict(data) db.session.add(item) db.session.commit() response = jsonify(item.to_dict()) response.status_code = 201 return response
def bookings(): #""" Get All Bookings. """ if request.method == 'GET': bookings = Booking.query.all() if bookings is None or bookings == []: return bad_request('No bookings exist.') else: response = jsonify({ 'bookings': [booking.to_dict() for booking in bookings], '_link': url_for('api.bookings') }) response.headers['Location'] = url_for('api.bookings') return response elif request.method == 'POST': data = request.get_json() or {} if 'user_id' not in data and 'restr_id' not in data and 'table_id' not in data: return bad_request( 'user_id, restr_id and table_id was missing in the request.') if 'user_id' not in data: return bad_request('user_id name was missing in the request.') if 'restr_id' not in data: return bad_request('restr_id was missing in the request.') if 'table_id' not in data: return bad_request('table_id was missing in the request.') user = requests.get('http://localhost:5001/api/v1/users/' + data['user_id']) if user.status_code != 200: return bad_request('user_id in the request does not exist.') else: user_data = user.json() restr = requests.get('http://localhost:5002/api/v1/restaurants/' + data['restr_id']) if restr.status_code != 200: return bad_request('restaurant_id in the request does not exist.') else: restr_data = restr.json() table = requests.get("http://localhost:5002/api/v1/restaurants/" + data["restr_id"] + "/tables/" + data['table_id']) if table.status_code != 200: return bad_request( 'table_id does not exist for restaurant_id in the request.') else: table_data = table.json() booking = Booking() booking.from_dict(data) db.session.add(booking) db.session.commit() response = jsonify(booking.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.booking', id=booking.id) return response elif request.method == 'PUT': return bad_request( "You cannot update a booking on this end point. Use this for a PUT request: " + url_for('api.booking') + "/<id>") elif request.method == 'DELETE': return bad_request( "You cannot delete a booking on this end point. Use this for a DELETE request: " + url_for('api.booking') + "/<id>") else: return bad_request("That's a bad request.")