def put(self, id): """ Update a category """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 json_data = request.get_json(force=True) category_validator(json_data) cur.execute("SELECT * FROM categories WHERE id={};".format(id)) category = cur.fetchone() store_id = get_store_id(get_jwt_identity()) if not category or category[1] != store_id: msg = {"message": 'Category does not exist'}, 404 return msg name = category[2] if 'name' in json_data: c_name = json_data['name'].lower() name = "".join(c_name.split()) cur.execute("SELECT * FROM categories WHERE name='{}';".format(name)) category_check = cur.fetchone() if category_check: msg = 'That category already exists' return {"status": "Failed", "message": msg}, 406 cur.execute("UPDATE categories SET name='{}' WHERE id ={}".format( name, id)) conn.commit() cur.execute("SELECT * FROM categories WHERE id={};".format(id)) new_c = cur.fetchone() format_new_c = {"category_name": new_c[2]} return {"status": "Updated!", "category": format_new_c}, 200
def post(self, c_id, p_id): """ Add a category to a product c_id : the category id p_id : the product id """ store_id = get_store_id(get_jwt_identity()) cur.execute("SELECT * FROM categories WHERE id='{}';".format(c_id)) category = cur.fetchone() if not category or category[1] != store_id: msg = 'Category does not exist' return {"status": "Failed!", "message": msg}, 404 cur.execute("SELECT * FROM products WHERE id={};".format(p_id)) product = cur.fetchone() if not product or product[1] != store_id: msg = 'Product does not exist' return {"message": msg}, 404 category_name = category[2] cur.execute("UPDATE products SET category='{}' WHERE id ='{}'".format( category_name, p_id)) conn.commit() cur.execute("SELECT * FROM products WHERE id={};".format(p_id)) new_p = cur.fetchone() format_new_p = { "product_name": new_p[2], "inventory": new_p[3], "price": new_p[4], 'category': new_p[5], 'added_at': new_p[6] } return {"status": "Updated!", "product": format_new_p}, 200
def post(self, id): """ Add a new product to a category """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 store_id = get_store_id(get_jwt_identity()) cur.execute("SELECT * FROM categories WHERE id='{}';".format(id)) category = cur.fetchone() if not category or category[1] != store_id: msg = 'Category does not exist' return {"message": msg}, 404 json_data = request.get_json(force=True) product_validator(json_data) cur.execute("SELECT * FROM products WHERE name='{}';".format( json_data['name'])) product = cur.fetchone() if product and product[1] == store_id: msg = 'Product already exists.Update product inventory instead' return {"message": msg}, 409 cat_name = category[2] new_pro = Product(store_id, json_data['name'], json_data['inventory'], json_data['price'], cat_name) new_pro.add_product() res = new_pro.json_dump() res = { "status": "Success!", "message": "Product successfully added", "data": res }, 201 return res
def put(self, id): """ Update a product """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 json_data = request.get_json(force=True) res = product_update_validator(json_data) if not res: cur.execute("SELECT * FROM products WHERE id={};".format(id)) product = cur.fetchone() store_id = get_store_id(get_jwt_identity()) if not product or product[1] != store_id: return { "status": "Failed!", "message": 'Product does not exist' }, 404 name = product[2] inventory = product[3] price = product[4] if 'name' in json_data: name = json_data['name'].lower() cur.execute( "SELECT * FROM products WHERE name='{}';".format(name)) product_check = cur.fetchone() if product_check: msg = 'That product already exists' return {"status": "Failed!", "message": msg}, 406 if 'inventory' in json_data: inventory = json_data['inventory'] if 'price' in json_data: price = json_data['price'] cur.execute( "UPDATE products SET name='{}',inventory='{}',price='{}'\ WHERE id ={}".format(name, inventory, price, id)) conn.commit() cur.execute("SELECT * FROM products WHERE id={};".format(id)) new_p = cur.fetchone() format_new_p = { "product_name": new_p[2], "inventory": new_p[3], "price": new_p[4], 'category': new_p[5], 'added_at': new_p[6] } res = {"status": "Updated!", "product": format_new_p}, 200 return res
def put(self, id): """ Update a product on a cart """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 json_data = request.get_json(force=True) res = sales_validator(json_data) if not res: cur.execute("SELECT * FROM carts WHERE id={};".format(id)) product = cur.fetchone() seller = get_user_by_email(get_jwt_identity()) seller_id = seller[0] if not product or product[1] != seller_id: return { "status": "Failed!", "message": "That product is not in the cart" }, 404 cur.execute("SELECT * FROM products WHERE name='{}';".format( product[2])) p = cur.fetchone() number = int(json_data['number']) total_num = p[3] + product[3] if number > int(total_num): msg = 'There are only {0} {1} available'.format( total_num, p[2]) return {"status": "Failed!", "message": msg}, 400 new_amnt = number * p[4] cur.execute( "UPDATE carts SET number={0},amount={1} WHERE id ={2}".format( number, new_amnt, id)) conn.commit() new_p_inv = total_num - number cur.execute( "UPDATE products SET inventory= '{}' WHERE name ='{}'".format( new_p_inv, product[2])) conn.commit() cur.execute("SELECT * FROM carts WHERE id={};".format(id)) new_c = cur.fetchone() format_new_c = { "product": new_c[2], "number": new_c[3], "amount": new_c[4] } res = {"status": "Cart Updated", "cart": format_new_c}, 200 return res
def post(self): """ Add a product to the manager """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 json_data = request.get_json(force=True) res = product_validator(json_data) if not res: store_id = get_store_id(get_jwt_identity()) cur.execute("SELECT * FROM products WHERE name='{}';".format( json_data['name'].lower())) product = cur.fetchone() if product and product[1] == store_id: msg = 'Product already exists.Update product inventory instead' return {"status": "Failed!", "message": msg}, 409 cat_name = 'Category-not-set' new_pro = Product(store_id, json_data['name'].lower(), json_data['inventory'], json_data['price'], cat_name) new_pro.add_product() res = new_pro.json_dump() res = { "status": "Success!", "message": "Product successfully added", "data": res }, 201 return res
def delete(self, id): """ Remove a product from cart """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 cur.execute("SELECT * FROM carts WHERE id={};".format(id)) product = cur.fetchone() seller = get_user_by_email(get_jwt_identity()) seller_id = seller[0] if not product or product[1] != seller_id: return { "status": "Failed!", "message": "That product is not in the cart" }, 400 new_p_inv = product[3] cur.execute( "UPDATE products SET inventory= inventory + {} WHERE name ='{}'". format(new_p_inv, product[2])) conn.commit() cur.execute("DELETE FROM carts WHERE id='{}';".format(id)) conn.commit() format_c = { "product": product[2], "number": product[3], "amount": product[4] } return {"status": "Deleted!", "product": format_c}, 200
def delete(self, id): """ Delete a category """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 cur.execute("SELECT * FROM categories WHERE id={};".format(id)) category = cur.fetchone() store_id = get_store_id(get_jwt_identity()) if not category or category[1] != store_id: msg = {"message": 'Category does not exist'}, 404 return msg cur.execute("DELETE FROM categories WHERE id={};".format(id)) conn.commit() format_c = {"category_name": category[2]} return {"status": "Deleted!", "prpduct": format_c}, 200
def post(self, id): """ Add a product to cart """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 json_data = request.get_json(force=True) res = sales_validator(json_data) if not res: number = json_data['number'] cur.execute("SELECT * FROM products WHERE id={};".format(id)) product = cur.fetchone() store_id = get_store_id(get_jwt_identity()) if not product or product[1] != store_id: msg = 'Product does not exist' return {"status": "Failed!", "message": msg}, 404 product_name = product[2] if product[3] < int(number): msg = 'There are only {0} {1} available'.format( product[3], product_name) return {"status": "Failed!", "message": msg}, 400 amount = number * product[4] seller = get_user_by_email(get_jwt_identity()) seller_id = seller[0] new_cart = Cart(seller_id, product_name, number, amount) new_cart.add_to_cart() res = new_cart.json_dump() new_inventory = product[3] - number cur.execute( "UPDATE products SET inventory={0} WHERE id ={1}".format( new_inventory, id)) res = { "status": "Success!", "message": "Added to cart", "product": res } return res
def delete(self, id): """ Delete a product """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 cur.execute("SELECT * FROM products WHERE id={};".format(id)) product = cur.fetchone() store_id = get_store_id(get_jwt_identity()) if not product or product[1] != store_id: msg = 'Product does not exist' return {"status": "Failed!", "message": msg}, 404 cur.execute("DELETE FROM products WHERE id={};".format(id)) conn.commit() format_p = { "product_name": product[2], "inventory": product[3], "price": product[4] } return {"status": "Deleted!", "product": format_p}, 200
def get(self, id): """ Get single product """ current_user = get_jwt_identity() if current_user is None: msg = 'Please login to access to access this resource' return {"status": "Failed!", "message": msg}, 400 cur.execute("SELECT * FROM products WHERE id={};".format(id)) product = cur.fetchone() store_id = get_store_id(get_jwt_identity()) if not product or product[1] != store_id: msg = 'Product does not exist' return {"status": "Failed!", "message": msg}, 404 format_p = { "product_name": product[2], "inventory": product[3], "price": product[4], 'category': product[5], 'added_at': product[6] } return {"status": "Success!", "product": format_p}, 200
def post(self): """ Create a category """ json_data = request.get_json(force=True) res = category_validator(json_data) if res: return res store_id = get_store_id(get_jwt_identity()) c_name = json_data['name'].lower() category_name = "".join(c_name.split()) cur.execute( "SELECT * FROM categories WHERE name='{}';".format(category_name)) category = cur.fetchone() if category and category[1] == store_id: return { "status": "Failed!", "message": "Category already exists" }, 409 new_cat = Category(store_id, category_name) new_cat.add_category() return {"status": "Success!", "Category": new_cat.json_dump()}, 201