예제 #1
0
 def delete_product(self, pid):
     dao = ProductDAO()
     if not dao.getProductById(pid):
         return jsonify(Error="Product not found."), 404
     else:
         dao.delete_product(pid)
     return jsonify(DeleteStatus="OK"), 200
예제 #2
0
 def getProductsByQuantity(self, p_qty):
     dao = ProductDAO()
     product_list = dao.getProductByQty(p_qty)
     result_list = []
     for row in product_list:
         result = self.build_product(row)
         result_list.append(result)
     return jsonify(Product=result_list)
예제 #3
0
 def getProductByName(self, p_name):
     dao = ProductDAO()
     row = dao.getProductByName(p_name)
     if not row:
         return jsonify(error="product not found"), 404
     else:
         product = self.build_product(row)
         return jsonify(Product=product)
예제 #4
0
 def getAllProducts(self):
     dao = ProductDAO()
     product_list = dao.getAllProducts()
     result_list = []
     for row in product_list:
         result = self.build_product(row)
         result_list.append(result)
     return jsonify(Product=result_list)
예제 #5
0
 def browseResourcesAvailable(self):
     dao = ProductDAO()
     product_list = dao.browseResourcesAvailable()
     result_list = []
     for row in product_list:
         result = self.build_product(row)
         result_list.append(result)
     return jsonify(Product=result_list)
예제 #6
0
 def get_categories(self):
     dao = ProductDAO()
     categories = dao.get_categories()
     result_list = []
     for row in categories:
         result = self.build_categories(row)
         result_list.append(result)
     return jsonify(Categories=result_list)
예제 #7
0
 def getAvailabilityOfProduct(self, p_id):
     dao = ProductDAO()
     row = dao.getAvailabilityOfProduct(p_id)
     if not row:
         return jsonify(error="product not found"), 404
     else:
         print(row)
         product = self.build_product(row)
         return jsonify(Product=product)
예제 #8
0
 def getPurchasableProduct(self):
     dao = ProductDAO()
     product_list = dao.getPurchasableProduct()
     result_list = []
     for row in product_list:
         result = self.build_product(row)
         result_list.append(result)
     max_order = int(dao.get_max_order_id()) + 1
     message = "For a new order please use order id: " + str(max_order) + ", if not please reuse your order id."
     return jsonify(Message=message,PurchasableProduct=result_list)
예제 #9
0
 def search_products(self, args):
     dao = ProductDAO()
     ct_id = args.get("ct_id")
     s_id = args.get("s_id")
     p_name = args.get("p_name")
     p_qty = args.get("p_qty")
     p_unit = args.get("p_unit")
     p_priceperunit = args.get("p_priceperunit")
     p_id = args.get("p_id")
     product_list = []
     if len(args) == 1 and ct_id:
         product_list = dao.filter_products(ct_id, 1)
     elif len(args) == 1 and s_id:
         product_list = dao.filter_products(s_id, 2)
     elif len(args) == 1 and p_name:
         product_list = dao.filter_products(p_name, 3)
     elif len(args) == 1 and p_qty:
         product_list = dao.filter_products(p_qty, 4)
     elif len(args) == 1 and p_unit:
         product_list = dao.filter_products(p_unit, 5)
     elif len(args) == 1 and p_priceperunit:
         product_list = dao.filter_products(p_priceperunit, 6)
     elif len(args) == 1 and p_id:
         product_list = dao.filter_products(p_id, 7)
     else:
         return jsonify(Error="Malformed query string"), 400
     result_list = []
     for row in product_list:
         result = self.build_new_product(row)
         result_list.append(result)
     return jsonify(Products=result_list)
예제 #10
0
 def findSpecificProduct(self, args):
     p_id = args.get("p_id")
     district = args.get("district")
     dao = ProductDAO()
     product_list = []
     if (len(args) == 2) and p_id and district:
         product_list = dao.findSpecificProduct(p_id, district)
     else:
         return jsonify(error="Malformed query string"), 400
     result_list = []
     for row in product_list:
         result = self.build_product(row)
         result_list.append(result)
     return jsonify(Product=result_list)
예제 #11
0
 def insert_product(self, form):
     if len(form) != 6:
         return jsonify(Error="Malformed post request"), 400
     else:
         ct_id = form['ct_id']
         s_id = form['s_id']
         p_name = form['p_name']
         p_qty = form['p_qty']
         p_unit = form['p_unit']
         p_priceperunit = form['p_priceperunit']
         if  s_id and p_name and p_qty and p_unit and p_priceperunit and ct_id:
             dao = ProductDAO()
             p_id = dao.insert_product(ct_id, s_id, p_name, p_qty, p_unit, p_priceperunit)
             result = self.build_product_attributes(p_id, ct_id, s_id, p_name, p_qty, p_unit, p_priceperunit)
             return jsonify(Product=result), 201
         else:
             return jsonify(Error="Unexpected attributes in post request"), 400
예제 #12
0
 def update_product(self, p_id, form):
     dao = ProductDAO()
     if not dao.getProductById(p_id):
         return jsonify(Error="Product not found."), 404
     else:
         if len(form) != 6:
             return jsonify(Error="Malformed update request"), 400
         else:
             ct_id = form['ct_id']
             s_id = form['s_id']
             p_name = form['p_name']
             p_qty = form['p_qty']
             p_unit = form['p_unit']
             p_priceperunit = form['p_priceperunit']
         if s_id and p_name and p_qty and p_unit and p_priceperunit and ct_id:
             dao.update_product(p_id, ct_id, s_id, p_name, p_qty, p_unit, p_priceperunit)
             result = self.build_product_attributes(p_id, ct_id, s_id, p_name, p_qty, p_unit, p_priceperunit)
             return jsonify(Product=result), 200
         else:
             return jsonify(Error="Unexpected attributes in update request"), 400
예제 #13
0
 def buy_product(self, form):
     if len(form) != 5:
         return jsonify(Error="Malformed post request"), 400
     else:
         o_id = int(form['o_id'])
         pin_id = int(form['pin_id'])
         qty = int(form['od_qty'])
         p_id = int(form['p_id'])
         s_id = int(form['s_id'])
         if o_id and pin_id and qty and p_id and s_id:
             if qty <= 0 or o_id <= 0 or pin_id <= 0 or p_id <= 0 or s_id <= 0:
                 return jsonify(Error="Invalid inputs o_id and/or pin_id and/or s_id and/or p_id and/or od_qty")
             else:
                 dao = ProductDAO()
                 pdao = peopledao()
                 date = time.strftime("%d%m%Y")
                 verify_o_id = pdao.check_o_id(o_id)  # returns true if o_id is less or equal max o_id, else false
                 verify_pin_id = pdao.check_pin(pin_id)  # return c_id if pin_id is valid, false otherwise
                 verify_sup = pdao.check_sup(s_id)  # returns ba_id if valid s_id, false otherwise
                 verify_product = dao.check_product(p_id, qty)  # return true if valid p_id & qty is less than db qty
                 if not (verify_o_id and verify_pin_id and verify_product and verify_sup):
                     return jsonify(
                         Error="Invalid inputs o_id and/or pin_id and/or s_id and/or p_id and/or od_qty")
                 else:
                     c_id = verify_pin_id[2]
                     ba_id = verify_sup[2]
                     p_priceperunit = verify_product[2]
                     pname = verify_product[3]
                     product_supplier = verify_product[4]
                     pin_fname = verify_pin_id[0]
                     pin_lname = verify_pin_id[1]
                     sup_fname = verify_sup[0]
                     sup_lname = verify_sup[1]
                     if product_supplier != s_id:
                         return jsonify(Error="supplier does not supply that item")
                     real_o_id = pdao.check_or_create_o_id(o_id, c_id, date)
                     order = dao.insert_product_to_OrderDetails(qty, p_priceperunit, real_o_id, pin_id, s_id,
                                                                ba_id, p_id)
                     if not order:
                         return jsonify(Error="Purchase did not go through")
                     else:
                         order_details = self.build_order_attributes(real_o_id, order, qty, p_priceperunit, s_id,
                                                                     ba_id, p_id, pin_id, c_id, date, sup_fname,
                                                                     sup_lname, pin_fname, pin_lname, pname)
                         new_qty = verify_product[1] - qty
                         dao.update_product_qty(p_id, new_qty)
                         return jsonify(OrderDetails=order_details)
         else:
             return jsonify(Error="Invalid Inputs")