def patch(self, sale_id): """ Update a single sale """ # To do check if user is admin schema = SaleSchema(partial=True) update_data = request.get_json() validated_update_data, errors = schema.load(update_data) if errors: return dict(status="fail", message=errors), 400 sale = Sale.get_by_id(sale_id) if not sale: return dict(status="fail", message=f"Sale with id {sale_id} not found"), 404 updated_sale = Sale.update(sale, **validated_update_data) if not updated_sale: return dict(status='fail', message='Internal Server Error'), 500 return dict(status="success", message="Sale updated successfully"), 200
def test_sale_object_created_successfully(self): "Test sale project creation was successful" sale = Sale(1, 2, 3) self.assertListEqual( [1, 2, 3], [sale.product_id, sale.products_sold, sale.seller_id] )
def post(self): 'Handles all post requests to /sales endpoint' parser = reqparse.RequestParser() parser.add_argument( 'product_id', type=int, required=True, help="Product id can not be empty" ) parser.add_argument( 'products_sold', type=int, required=True, help="Products sold can not be empty" ) args = parser.parse_args() product_id = args['product_id'] products_sold = args['products_sold'] if products: for product in products: if product.product_id == int(product_id): sale = Sale( product_id = product.product_id, products_sold = products_sold ) sale.sale_id = len(sales) + 1 sales.append(sale) return { 'message': '{} {}(s) successfully sold'.format( products_sold, product.product_name ) } return { 'message': 'Product with Product id {} does not exist'.format(product_id) }
def post(self): """ Creating an Sale ad """ sale_schema = SaleSchema() sale_data = request.get_json() validated_sale_data, errors = sale_schema.load(sale_data) if errors: return dict(status='fail', message=errors), 400 sale = Sale(**validated_sale_data) saved_sale = sale.save() if not saved_sale: return dict(status='fail', message='Internal Server Error'), 500 new_sale_data, errors = sale_schema.dumps(sale) return dict(status='success', data=dict(sale=json.loads(new_sale_data))), 201
def get(self): """ Getting All sales """ sale_schema = SaleSchema(many=True) sales = Sale.find_all() sales_data, errors = sale_schema.dumps(sales) if errors: return dict(status="fail", message="Internal Server Error"), 500 return dict(status="success", data=dict(sales=json.loads(sales_data))), 200
def delete(self, sale_id): """ Delete a single sale """ sale = Sale.get_by_id(sale_id) if not sale: return dict(status="fail", message=f"Sale with id {sale_id} not found"), 404 deleted_sale = sale.delete() if not deleted_sale: return dict(status='fail', message='Internal Server Error'), 500 return dict(status='success', message="Successfully deleted"), 200
def get(self, sale_id): """ Getting individual sale """ schema = SaleSchema() sale = Sale.get_by_id(sale_id) if not sale: return dict(status="fail", message=f"Sale with id {sale_id} not found"), 404 sale_data, errors = schema.dumps(sale) if errors: return dict(status="fail", message=errors), 500 return dict(status='success', data=dict(sale=json.loads(sale_data))), 200
def add_sale_record(self, product_id, quantity, attendant, date): # creating a sales record item = self.dbcon.fetch_single_product(product_id=product_id) if item: if item["quantity"] > int(quantity): product = item["product"] _quantity = int(quantity) amount = (item["unit_price"]*int(quantity)) _attendant = attendant _date = date new_sale = Sale(product_name=product, quantity=_quantity, unit_price=amount, attendant=_attendant, date=_date) self.dbcon.create_sale_record(product=new_sale.product_name, quantity=new_sale.quantity, amount=new_sale.unit_price, attendant=new_sale.attendant, date=new_sale.date) new_quantity = int(item["quantity"])- _quantity self.dbcon.update_product(product=product, quantity=new_quantity, unit_price=item["unit_price"], product_id=product_id) return True else: return False return False