def post(body): name = body['name'] session = Session() place = session.query(PlaceDAO).filter(PlaceDAO.name == name).first() if place: session.commit() session.close() return jsonify( {'message': f'There exist a place record with id {name}'}), 404 else: placedao = PlaceDAO() placedao.name = name placedao.rating = body['rating'] for address in body['addresses']: addressdao = AddressDAO() addressdao.city = address['city'] addressdao.postcode = address['postcode'] addressdao.houseNo = address['houseNo'] addressdao.street = address['street'] placedao.addresses.append(addressdao) session.add(placedao) session.commit() session.refresh(placedao) session.close() return jsonify({'place_id': placedao.id}), 201
def create(body): session = Session() delivery = DeliveryDAO( body['customer_id'], body['provider_id'], body['package_id'], datetime.now(), datetime.strptime(body['delivery_time'], '%Y-%m-%d %H:%M:%S.%f'), StatusDAO(STATUS_CREATED, datetime.now())) session.add(delivery) session.commit() session.refresh(delivery) session.close() return jsonify({'delivery_id': delivery.id}), 200
def create_account(body): session = Session() account = AccountDAO(body['customer_name'], body['customer_address'], body['customer_email'], body['customer_password'], datetime.now()) #Simply add objects to table, advantage of ORM session.add(account) session.commit() session.refresh(account) session.close() #Use Jsonify to return json object with status code when API is called. return jsonify({'customer_id': account.id}), 200
def register_product(body): session = Session() product = ProductDAO(body['title'], body['overview'], body['release_date'], body['runtime'], body['adult'], body['original_language'], body['budget'], body['revenue'], body['product_quantity'], body['unit_price']) session.add(product) session.commit() session.refresh(product) session.close() return jsonify({'product_id': product.id}), 200
def create(body): session = Session() cart = CartDAO(body['user_id'], body['product_id'], body['product_name'], body['product_quantity'], body['product_price'], 'static short description', '/product/path', 'path/to/img') session.add(cart) session.commit() session.refresh(cart) session.close() #return jsonify({'Product added to cart!': "Product added to cart!"}), 200 return jsonify({ 'Succes!': f'Product with name: {cart.product_name} has been added to the cart.' }), 200
def edit_post(post_slug): session = Session() edit = True post = session.query(Post).filter(Post.slug == post_slug).first() if request.method == 'POST': post.set_title(request.form['title']) post.body = request.form['body'] session.add(post) session.commit() session.refresh(post) edit = False session.close() return render_template('post.html', post=post, edit=edit)
def update_quantity_price(p_id, req_data): session = Session() product = session.query(ProductDAO).filter( ProductDAO.id == p_id).first() if product: entities_updated = [] if 'quantity' in req_data: product.product_quantity = req_data['quantity'] entities_updated.append('quantity, ') if 'unit_price' in req_data: product.unit_price = req_data['unit_price'] entities_updated.append('unit_price, ') session.commit() session.refresh(product) return jsonify( {'message': f'{" ".join(entities_updated)} were updated'}), 200 else: return jsonify({'message': f'There is no product with id {p_id}'}), 404