def create_order(): json_data = request.json order = (DBDriver().get_orders(order_id=json_data['order_id']) or [None])[0] if order is None: try: order_items = [] created_order = DBDriver().create_order( order_id=json_data['order_id'], order_date=datetime.datetime.strptime(json_data['order_date'], '%d-%m-%Y %H:%M:%S'), order_status=json_data['order_status'], order_discount_rate=json_data['order_discount_rate'], total_price=json_data['total_price'], customer_id=json_data['customer_id'], last_modified_by=json_data['last_modified_by']) for order_item in json_data['order_items']: created_order_item = DBDriver().create_order_item( quantity=order_item['quantity'], price=order_item['price'], order_item_discount_rate=order_item[ 'order_item_discount_rate'], product_id=order_item['product_id'], order_id=created_order.id) order_items.append(created_order_item) return StandardResponse(created_order, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("order already exists", 406).to_json()
def delete_employee(employee_id): employee = (DBDriver().get_employees(id=employee_id) or [None])[0] if employee is not None: try: DBDriver().delete_employee(id=employee_id) return StandardResponse("employee deleted successfully", 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("employee does not exist", 406).to_json()
def delete_stock(stock_id): stock = (DBDriver().get_stocks(id=stock_id) or [None])[0] if stock is not None: try: DBDriver().delete_stock(id=stock_id) return StandardResponse("stock deleted successfully", 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("stock does not exist", 406).to_json()
def delete_category(order_id): order = (DBDriver().get_orders(id=order_id) or [None])[0] if order is not None: try: DBDriver().delete_order(id=order.id) return StandardResponse("order deleted successfully", 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("order does not exist", 406).to_json()
def delete_category(category_id): category = (DBDriver().get_categories(id=category_id) or [None])[0] if category is not None: try: DBDriver().delete_category(id=category_id) return StandardResponse("category deleted successfully", 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("category does not exist", 406).to_json()
def delete_product(product_id): product = (DBDriver().get_products(id=product_id) or [None])[0] if product is not None: try: DBDriver().delete_product(id=product_id) return StandardResponse("product deleted successfully", 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("product does not exist", 406).to_json()
def create_category(): json_data = request.json category = (DBDriver().get_categories(name=json_data['name']) or [None])[0] if category is None: try: created_category = DBDriver().create_category( name=json_data.get('name', None), description=json_data.get('description', None), last_modified_by=json_data.get('last_modified_by', None)) return StandardResponse(created_category, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("category already exists", 406).to_json()
def update_category(category_id): json_data = request.json category = (DBDriver().get_categories(id=category_id) or [None])[0] if category is not None: try: updated_category = DBDriver().update_category( id=category_id, last_modified_by=json_data.get('last_modified_by'), name=json_data.get('name', None), description=json_data.get('description', None)) return StandardResponse(updated_category, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("category does not exist", 406).to_json()
def update_stocks(stock_id): json_data = request.json stock = (DBDriver().get_stocks(id=stock_id) or [None])[0] if stock is not None: try: updated_stock = DBDriver().update_stock(id=stock_id, quantity=json_data.get('quantity', None), retail_price=json_data.get('retail_price', None), last_modified_by=json_data['last_modified_by'], supplier_id=json_data.get('supplier_id', None), product_id=json_data.get('product_id', None)) return StandardResponse(updated_stock, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("stock does not exist", 406).to_json()
def create_stock(): json_data = request.json stock = (DBDriver().get_stocks(product_id=json_data['product_id']) or [None])[0] if stock is None: try: created_stock = DBDriver().create_stock(quantity=json_data.get('quantity', None), retail_price=json_data.get('retail_price', None), last_modified_by=json_data['last_modified_by'], supplier_id=json_data.get('supplier_id', None), product_id=json_data.get('product_id', None)) return StandardResponse(created_stock, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("stock already exists", 406).to_json()
def update_customers(customer_id): json_data = request.json customer = (DBDriver().get_customers(id=customer_id) or [None])[0] if customer is not None: try: updated_customer = DBDriver().update_customer( id=customer_id, name=json_data.get('name', None), mobile_phone=json_data.get('mobile_phone', None), address=json_data.get('address', None), last_modified_by=json_data['last_modified_by']) return StandardResponse(updated_customer, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("category does not exist", 406).to_json()
def update_employee(employee_id): json_data = request.json employee = (DBDriver().get_employees(id=employee_id) or [None])[0] if employee is not None: try: updated_employee = DBDriver().update_employee(username=json_data['username'], password=json_data.get('password', None), name=json_data.get('name', None), mobile_phone=json_data.get('mobile_phone', None), address=json_data.get('address', None), role=json_data.get('role', None)) return StandardResponse(updated_employee, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("employee does not exist", 406).to_json()
def create_employee(): json_data = request.json employee = (DBDriver().get_employees(username=json_data['username']) or [None])[0] if employee is None: try: created_employee = DBDriver().create_employee(username=json_data['username'], password=json_data['password'], name=json_data['name'], mobile_phone=json_data['mobile_phone'], address=json_data.get('address', None), role=json_data['role']) return StandardResponse(created_employee, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("employee already exists", 406).to_json()
def create_customer(): json_data = request.json customer = ( DBDriver().get_customers(mobile_phone=json_data['mobile_phone']) or [None])[0] if customer is None: try: created_customer = DBDriver().create_customer( name=json_data['name'], mobile_phone=json_data['mobile_phone'], address=json_data.get('address', None), last_modified_by=json_data['last_modified_by']) return StandardResponse(created_customer, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("customer already exists", 406).to_json()
def update_suppliers(supplier_id): json_data = request.json supplier = (DBDriver().get_suppliers(id=supplier_id) or [None])[0] if supplier is not None: try: updated_supplier = DBDriver().update_supplier( id=supplier_id, name=json_data.get('name', None), mobile_phone=json_data.get('mobile_phone', None), address=json_data.get('address', None), email=json_data.get('email', None), last_modified_by=json_data.get('last_modified_by', None)) return StandardResponse(updated_supplier, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("suppliers does not exist", 406).to_json()
def create_product(): json_data = request.json product = (DBDriver().get_products(name=json_data['name']) or [None])[0] if product is None: try: created_product = DBDriver().create_product( name=json_data['name'], description=json_data.get('description', None), par_code=json_data['par_code'], price=json_data['price'], last_modified_by=json_data['last_modified_by'], category_id=json_data['category_id']) return StandardResponse(created_product, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("product already exists", 406).to_json()
def update_products(product_id): json_data = request.json product = (DBDriver().get_products(id=product_id) or [None])[0] if product is not None: try: updated_product = DBDriver().update_product( id=product_id, last_modified_by=json_data['last_modified_by'], name=json_data.get('name', None), description=json_data.get('description', None), par_code=json_data.get('par_code', None), price=json_data.get('price', None), category_id=json_data.get('category_id', None)) return StandardResponse(updated_product, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("product does not exist", 406).to_json()
def update_orders(order_db_id): json_data = request.json order = (DBDriver().get_orders(id=order_db_id) or [None])[0] if order is not None: try: if json_data.get('order_date', None) is not None: order_date = datetime.datetime.strptime( json_data.get('order_date'), '%d-%m-%Y %H:%M:%S') else: order_date = None for order_item in json_data['order_items']: DBDriver().update_order_item( id=order_item.get('id'), quantity=order_item.get('quantity', None), price=order_item.get('price', None), order_item_discount_rate=order_item.get( 'order_item_discount_rate', None), product_id=order_item.get('product_id', None), order_id=order_db_id) updated_order = DBDriver().update_order( id=order_db_id, last_modified_by=json_data['last_modified_by'], order_date=order_date, order_status=json_data.get('order_status', None), order_discount_rate=json_data.get('order_discount_rate', None), total_price=json_data.get('total_price', None), customer_id=json_data.get('customer_id', None)) return StandardResponse(updated_order, 200).to_json() except: return StandardResponse("check request json format", 400).to_json() else: return StandardResponse("order does not exist", 406).to_json()
def get_categories(): try: return StandardResponse(DBDriver().get_categories(**request.args), 200).to_json() except: return StandardResponse('categories do not exist', 404).to_json()
def get_products(): try: return StandardResponse(DBDriver().get_products(**request.args), 200).to_json() except: return StandardResponse('products do not exist', 404).to_json()