def listallpurchases(): error = False purch = Purchase() try: results = purch.listall() except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'results': results})
def listUserPurchase(): result = [] error = False id_user = request.json['id_user'] try: purch = Purchase() result = purch.userList(id_user) except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success', 'data': result})
def listUserPurchaseItems(): result = [] error = False purch = Purchase() purch.id = request.json['id'] try: result = purch.listItems() except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success', 'data': result})
def getPurchItem(): error = False id_user = request.json['id_user'] id_prod = request.json['id_prod'] try: purch = Purchase() result = purch.getItem(id_user, id_prod) except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success', 'data': result})
def deletePurchase(): error = False id = request.json['id'] new = Purchase() new.id = id try: new.delete() except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success'})
def main(): client = Client('max', 44) seller = Seller('Frank', 55, 1024) purchase_1 = Purchase(client, datetime.now(), 512) purchase_2 = Purchase(client, datetime(2019, 4, 9), 256) client.regist_purchase(purchase_1) client.regist_purchase(purchase_2) print(client) print(seller) total_values = client.get_total_purchase() print( f'total value of purchases {total_values}$ in {len(client.purchases)} purchases' ) print(f"last purchase date: {client.get_last_purchase()}")
def modPurchase(): error = False id = request.json['id'] price = request.json['price'] state = request.json['state'] id_user = request.json['id_user'] id_coupon = request.json['id_coupon'] new = Purchase(price, None, state, id_user, id_coupon, id) try: new.mod() except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success'})
def addPurchase(): error = False price = request.json['price'] date = request.json['date'] state = request.json['state'] id_user = request.json['id_user'] id_coupon = request.json['id_coupon'] id_shipping = request.json['id_shipping'] new = Purchase(price, date, state, id_user, id_coupon, id_shipping) try: new.add() except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success'})
def getUserCartInfo(): result = [] error = False user = User() user.id = request.json['user_id'] try: result = user.getCartInfo() if len(result) == 0: new = Purchase() new.id_user = user.id new.add() result = user.getCartInfo() except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success', 'data': result})
def getPurchaseInfo(): error = False result = {} id = request.json['id'] purch = Purchase() purch.id = id try: purch.get() result['purchase'] = purch.json() ship = Shipping() ship.id = id ship.get() if (ship.id != None): result['shipping'] = ship.json() if (purch.id_coupon != None): coup = Coupon() coup.id = purch.id_coupon coup.get() result['coupon'] = coup.json() except (Exception) as err: error = True return handleError(err) finally: if not (error): return jsonify({'result': 'success', 'data': result})