def getCustomer(c_id): with SessionManager(Session) as session: try: sql_customer = session.query(Customer).filter( Customer.id == c_id).one() customer = dict( first_name=sql_customer.first_name, middle_name=sql_customer.middle_name, last_name=sql_customer.last_name, contact_number=sql_customer.contact_number, address=sql_customer.address, gender=sql_customer.gender, age=sql_customer.age, email=sql_customer.email, id=sql_customer.id, customer_join_date=sql_customer.customer_join_date, membership=dict( m_type=sql_customer.c_membership.m_type, discount=sql_customer.c_membership.discount, description=sql_customer.c_membership.description, id=sql_customer.c_membership.id)) return jsonify(envelop(data=customer, code=200)) except NoResultFound: return jsonify( error_envelop(404, 'NoResultFound', 'Id : {0} Not Found'.format(c_id))) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def getCustomersByMembership(m_id): '''A function to get the customers based on memberships''' with SessionManager(Session) as session: try: sql_customers = session.query(Customer).filter( Customer.membership_id == m_id).order_by(Customer.id).all() customers = [ dict(first_name=customer.first_name, middle_name=customer.middle_name, last_name=customer.last_name, contact_number=customer.contact_number, address=customer.address, gender=customer.gender, age=customer.age, email=customer.email, id=customer.id, uri=url_for('getCustomerByMembership', m_id=m_id, c_id=customer.id), customer_join_date=customer.customer_join_date) for customer in sql_customers ] return jsonify(envelop(data=customers, code=200)) except: return jsonify( error_envelop(400, 'UnkownError', 'Somethig went wrong'))
def getCustomerByMembership(m_id, c_id): with SessionManager(Session) as session: try: sql_customer = session.query(Customer).filter( Customer.id == c_id).one() sql_membership = sql_customer.c_membership customer = dict(id=sql_customer.id, uri=url_for('getCustomerByMembership', m_id=m_id, c_id=c_id), first_name=sql_customer.first_name, middle_name=sql_customer.middle_name, last_name=sql_customer.last_name, contact_number=sql_customer.contact_number, gender=sql_customer.gender, age=sql_customer.age, email=sql_customer.email, customer_join_date=sql_customer.customer_join_date, address=sql_customer.address, membership=dict(m_type=sql_membership.m_type, discount=sql_membership.discount, id=sql_membership.id, uri=url_for('getMembership', m_id=m_id))) return jsonify(envelop(customer, 200)) except NoResultFound: #causes when there is no requested id in the database return jsonify( error_envelop(404, 'NoResultFound', ' Cannot Get!!Id : {0} Not Found'.format(c_id))) except: return jsonify( error_envelop(400, 'UnknownError', 'Something went wrong'))
def updateVat(vat_id): ''' PUT /api/v1/vats/6 HTTP/1.1 {"name" : "New vat name", "value" : 34} Result : { "data": { "name": "New" }, "meta": { "code": 200, "message": "Updated Successfully" } } ''' with SessionManager(Session) as session: try: sql_vat = session.query(Vat).filter(Vat.id == vat_id).one() sql_vat.name = request.json.get('name', sql_vat.name) sql_vat.value = request.json.get('value', sql_vat.value) session.commit() return jsonify(update_envelop(200, data=request.json)) except IntegrityError: # if name already exsits in database return jsonify(error_envelop(400, 'Integrity Error','Name already Exists')) except NoResultFound: return jsonify(error_envelop(404, 'ValueError', 'Id : {0} not found'.format(vat_id))) #now the item is succesfulluy updated return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def getMembership(m_id): '''This function will return the particular payment from list of payments Example : GET /api/v1/dinetables/1 HTTP/1.1 Result : { "alias": "Robus", "capacity": 8, "id": 1, "uri" : "/api/v1/dinetables/1" } ''' with SessionManager(Session) as session: try: sql_membership = session.query(Membership).filter( Membership.id == m_id).one() m_type = sql_membership.m_type discount = sql_membership.discount description = sql_membership.description id = sql_membership.id uri = url_for('getMembership', m_id=sql_membership.id) data = dict(m_type=m_type, discount=discount, description=description, id=id, uri=uri) return jsonify(envelop(data, 200)) except NoResultFound: return jsonify( error_envelop(404, 'NoResultFound', 'Id : {0} Not Found'.format(m_id))) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def getDinetables(): '''This function will return the all the payments available Example : GET /api/v1/dinetables HTTP/1.1 Result : { "data": [ { "capacity": 2, "alias" : "table 1", "status" : "empty" },..... ''' with SessionManager(Session) as session: try: sql_dinetables = session.query(DineTable).order_by(DineTable.id).all() dinetables = [dict(capacity=dinetable.capacity, id=dinetable.id, alias = dinetable.alias, uri = url_for('getDineTable', d_id=dinetable.id), status = dinetable.status ) for dinetable in sql_dinetables] return jsonify(envelop(dinetables, 200)) except: return jsonify(error_envelop(400, ' UnkownError', 'Error need to identified'))
def updateDineTable(d_id): ''' PUT /api/v1/dientables/6 HTTP/1.1 {"alias" : "new table name", "capacity" : 3} Result : { "data": { "capacity": 3, "alias" : "new table name" }, "meta": { "code": 200, "message": "Updated Successfully" } } ''' with SessionManager(Session) as session: try: sql_dinetable = session.query(DineTable).filter(DineTable.id == d_id).one() sql_dinetable.alias = request.json.get('alias', sql_dinetable.alias) sql_dinetable.capacity = request.json.get('capacity', sql_dinetable.capacity) sql_dinetable.status = request.json.get('status', sql_dinetable.status) session.commit() return jsonify(update_envelop(200, data=request.json)) except IntegrityError: # if name already exsits in database return jsonify(error_envelop(400, 'Integrity Error','Name already Exists')) except NoResultFound: return jsonify(error_envelop(404, 'ValueError', 'Id : {0} not found'.format(d_id))) except DataError: return jsonify(error_envelop(404, 'DataError', 'Please use the status of enum : (unorder, order, served, cooking, empty) defined')) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def setDineTable(): '''This function is used to store the new DineTable in the database Example : POST /api/v1/dinetables HTTP/1.1 { "capicity": 4, "alias" : "Table1", "status" : "empty"} Result : { "meta": { "code": 200, "message": "Created Successfully" } } ''' with SessionManager(Session) as session: try: capacity = request.json['capacity'] alias = request.json['alias'] status = request.json.get('status', 'empty') dine_table = DineTable(capacity=capacity, alias=alias, status=status) session.add(dine_table) session.commit() return jsonify(post_envelop(200, data = request.json)) except DataError: #this excepyion might probably occur if the value key has a value of non integer return jsonify(error_envelop(400, 'DataError', 'Use the correct value')) except IntegrityError: return jsonify(error_envelop(400, 'IntegrityError','Value : {0} already exists'.format(alias))) except: return jsonify(error_envelop(400,'UnknownError','Error need to be identified'))
def updatePayment(p_id): ''' PUT /api/v1/payments/6 HTTP/1.1 {"c_type" : "offsite"} Result : { "data": { "p_type": "offsite" }, "meta": { "code": 200, "message": "Updated Successfully" } } ''' with SessionManager(Session) as session: try: sql_payment = session.query(Payment).filter(Payment.id == p_id).one() sql_payment.p_type = request.json.get('p_type', sql_payment.p_type) session.commit() return jsonify(update_envelop(200, data=request.json)) except IntegrityError: # if name already exsits in database return jsonify(error_envelop(400, 'Integrity Error','Name already Exists')) except NoResultFound: return jsonify(error_envelop(404, 'ValueError', 'Id : {0} not found'.format(p_id))) #now the item is succesfulluy updated return jsonify(error_envelop(400,'UnknownError','Error need to be identified'))
def setPayment(): '''This function is used to store the new payment in the database Example : POST /api/v1/payments HTTP/1.1 { "p_type": " onsite"} Result : { "meta": { "code": 200, "message": "Created Successfully" } } ''' with SessionManager(Session) as session: try: p_type = request.json['p_type'] payment = Payment(p_type=p_type) session.add(payment) session.commit() return jsonify(post_envelop(200, data = request.json)) except DataError: #this excepyion might probably occur if the value key has a value of non integer return jsonify(error_envelop(400, 'DataError', 'Use the correct value')) except IntegrityError: return jsonify(error_envelop(400, 'IntegrityError','Value : {0} already exists'.format(p_type))) except: return jsonify(error_envelop(400,'UnknownError','Error need to be identified'))
def getMemberships(): '''This function will return the all the payments available Example : GET /api/v1/dinetables HTTP/1.1 Result : { "data": [ { "capacity": 2, "alias" : "table 1", "status" : "empty" },..... ''' with SessionManager(Session) as session: try: sql_memberships = session.query(Membership).order_by( Membership.id).all() memberships = [ dict(m_type=membership.m_type, id=membership.id, discount=membership.discount, uri=url_for('getMembership', m_id=membership.id), description=membership.description) for membership in sql_memberships ] return jsonify(envelop(memberships, 200)) except: return jsonify( error_envelop(400, ' UnkownError', 'Error need to identified'))
def getItemCategories(): '''A function to get all the names in item categories''' #check to see if there is any query parameter for pagination in the http url for pagination if request.args and ('page' and 'size' in request.args): page = int(request.args['page']) size = int(request.args['size']) #initiate the session next_pagination = None with SessionManager(Session) as session: if request.args: #if there is query parameter in the url perform the pagination categories = session.query(ItemCategory).order_by( ItemCategory.id).all()[size * page:size * page + size] #set the next pagination url as a string next_pagination = url_for( 'getItemCategories') + '?page={0}&size={1}'.format( page + 1, size) else: #display all the items categories = session.query(ItemCategory).order_by( ItemCategory.id).all() #create a list of dictionary with all the necessary data in the dictionary alist = [ dict(name=category.name, extra=category.extra, id=category.id) for category in categories ] return jsonify( envelop(code=200, data=alist, pagination=next_pagination))
def setVat(): '''This function is used to store the new vat in the database Example : POST /api/v1/vats HTTP/1.1 {"name" : "food", "value":34} Result : { "meta": { "code": 200, "message": "Created Successfully" } } ''' with SessionManager(Session) as session: try: name = request.json['name'] value = request.json['value'] vat = Vat(name=name, value=value) session.add(vat) session.commit() return jsonify(post_envelop(200, data = request.json)) except DataError: #this excepyion might probably occur if the value key has a value of non integer return jsonify(error_envelop(400, 'DataError', 'Use the correct value')) except IntegrityError: return jsonify(error_envelop(400, 'IntegrityError','Value : {0} already exists'.format(name))) except: return jsonify(error_envelop(400,'UnknownError','Error need to be identified'))
def getEmployees(): from datetime import date with SessionManager(Session) as session: sql_employees = session.query(Employee).order_by(Employee.id).all() employees = [ dict( first_name=employee.first_name, middle_name=employee.middle_name, last_name=employee.last_name, contact_number=employee.contact_number, address=employee.address, gender=employee.gender, age=employee.age, email=employee.email, id=employee.id, date_of_birth=str(employee.date_of_birth), salary=employee.salary, photo_uri=employee.photo_uri, position=dict(name=employee.e_position.name, description=employee.e_position.description, id=employee.e_position.id), #uri = url_for('getCustomerByMembership', m_id=m_id, c_id = customer.id), join_date=str(employee.join_date.date())) for employee in sql_employees ] return jsonify(envelop(data=employees, code=200)) return jsonify( error_envelop(400, 'UnknownError', 'Error need to be identified'))
def getEmployee(e_id): with SessionManager(Session) as session: sql_employee = session.query(Employee).filter( Employee.id == e_id).one() position = sql_employee.e_position employee = dict( first_name=sql_employee.first_name, middle_name=sql_employee.middle_name, last_name=sql_employee.last_name, contact_number=sql_employee.contact_number, address=sql_employee.address, gender=sql_employee.gender, age=sql_employee.age, email=sql_employee.email, id=sql_employee.id, date_of_birth=str(sql_employee.date_of_birth), salary=sql_employee.salary, photo_uri=sql_employee.photo_uri, position=dict(name=position.name, description=position.description, id=position.id), #uri = url_for('getCustomerByMembership', m_id=m_id, c_id = customer.id), join_date=str(sql_employee.join_date.date())) return jsonify(envelop(data=employee, code=200))
def setEmployeePosition(): '''This method is used to store the new position for the employee Example : POST /api/v1/employeepostions {"name" : "Cook", "description" : "THis is desription"} ''' with SessionManager(Session) as session: try: name = request.json['name'] description = request.json.get('description', 'NA') sql_pos = EmployeePosition(name=name, description=description) session.add(sql_pos) session.commit() return jsonify(post_envelop(200, data=request.json)) except DataError: #this excepyion might probably occur if the value key has a value of non integer return jsonify( error_envelop(400, 'DataError', 'Use the correct value')) except IntegrityError: return jsonify( error_envelop(400, 'IntegrityError', 'Value : {0} already exists'.format(name))) except: return jsonify( error_envelop(400, 'UnknownError', 'Error need to be identified'))
def getEmployeesByPositions(p_id): '''A function to get the customers based on Positions''' with SessionManager(Session) as session: try: sql_employees = session.query(Employee).filter( Employee.employee_position_id == p_id).order_by( Employee.id).all() employees = [ dict( first_name=employee.first_name, middle_name=employee.middle_name, last_name=employee.last_name, contact_number=employee.contact_number, address=employee.address, gender=employee.gender, age=employee.age, email=employee.email, id=employee.id, date_of_birth=str(employee.date_of_birth), salary=employee.salary, photo_uri=employee.photo_uri, #uri = url_for('getCustomerByMembership', m_id=m_id, c_id = customer.id), join_date=str(employee.join_date.date())) for employee in sql_employees ] return jsonify(envelop(data=employees, code=200)) except: return jsonify( error_envelop(400, 'UnkownError', 'Somethig went wrong'))
def updateEmployeePosition(p_id): ''' PUT /api/v1/dientables/6 HTTP/1.1 {"name" : " positions name"} Result : { "meta": { "code": 200, "message": "Updated Successfully" } } ''' with SessionManager(Session) as session: try: sql_position = session.query(EmployeePosition).filter( EmployeePosition.id == p_id).one() sql_position.name = request.json.get('name', sql_position.name) sql_position.description = request.json.get( 'description', sql_position.description) session.commit() return jsonify(update_envelop(200, data=request.json)) except IntegrityError: # if name already exsits in database return jsonify( error_envelop(400, 'Integrity Error', 'Name already Exists')) except NoResultFound: return jsonify( error_envelop(404, 'ValueError', 'Id : {0} not found'.format(p_id))) return jsonify(error_envelop(400, 'UnknownError', 'UnknownError Found'))
def getCategoryItems(cat_id): '''This function is used to get all the items in the category Example : /api/v1/itemcategories/12/items Result : this gets the items of particualar category ''' list_of_items = [] #declaring the empty list outside the context manager with SessionManager(Session) as session: try: category = session.query(ItemCategory).filter( ItemCategory.id == cat_id).one() items = category.c_items list_of_items = [ dict(id=item.id, name=item.name, unit_price=item.unit_price, item_photo_uri=item.item_photo_uri, url_id=url_for('getItem', item_id=item.id), description=item.description) for item in items ] except: return jsonify( error_envelop(error_code=404, error_type='Value Error', error_message='ID is not available')) return jsonify(envelop(code=200, data=list_of_items, pagination=None))
def getBills(): '''Get all the bills ''' with SessionManager(Session) as session: sql_bills = session.query(Bill).order_by(Bill.id).all() bills = [] for sql_bill in sql_bills: customer = sql_bill.customer.first_name if sql_bill.customer else 'Not Available' dinetable = sql_bill.dinetable.alias if sql_bill.dinetable else 'Not Available' employee = sql_bill.employee.first_name if sql_bill.employee else 'Not Available' payment = sql_bill.payment.p_type if sql_bill.payment else 'Not Available' vat = sql_bill.vat.name if sql_bill.vat else 'Not Available' service_charge = sql_bill.service_charge.name if sql_bill.service_charge else 'Not Available' bill_description = sql_bill.bill_description on_site = sql_bill.on_site total_price = sql_bill.total_price bill_time_stamp = sql_bill.bill_time_stamp b = dict(customer=customer, dinetable=dinetable, employee=employee, payment=payment, vat=vat, service_charge=service_charge, bill_description=bill_description, on_site=on_site, total_price=total_price, id=sql_bill.id, bill_time_stamp=str(bill_time_stamp)) bills.append(b) return jsonify(envelop(data=bills, code=200))
def getItem(item_id): ''' This function is used to get the particular item in items.. Example : GET /api/v1/items/1 HTTP/1.1 Result : { "description": "No Description Available", "id": 1, "item_photo_uri": "Image URI Not Available", "name": "coke", "unit_price": 45 } ''' item = {} with SessionManager(Session) as sesion: #check to see if id exisst in items list try: sql_item = sesion.query(Item).filter(Item.id == item_id).one() sql_item_cat = sql_item.item_category item['name'] = sql_item.name item['id'] = sql_item.id item['url_id'] = url_for('getItem', item_id=item_id) item['item_photo_uri'] = sql_item.item_photo_uri item['description'] = sql_item.description item['unit_price'] = sql_item.unit_price item['item_category'] = dict(name=sql_item_cat.name, extra=sql_item_cat.extra, id=sql_item_cat.id) except: return jsonify(error_envelop(404, 'ValueError', 'Invalid ID')) return jsonify(envelop(data=item, code=200))
def updateServiceCharge(s_id): ''' PUT /api/v1/servicecharges/6 HTTP/1.1 {"name" : "New service name", "value" : 34} Result : { "data": { "name": "New service name" }, "meta": { "code": 200, "message": "Updated Successfully" } } ''' with SessionManager(Session) as session: try: sql_service = session.query(ServiceCharge).filter(ServiceCharge.id == s_id).one() sql_service.name = request.json.get('name', sql_service.name) sql_service.value = request.json.get('value', sql_service.value) session.commit() return jsonify(update_envelop(200, data=request.json)) except IntegrityError: # if name already exsits in database return jsonify(error_envelop(400, 'Integrity Error','Name already Exists')) except NoResultFound: return jsonify(error_envelop(404, 'ValueError', 'Id : {0} not found'.format(s_id))) #now the item is succesfulluy updated return jsonify(error_envelop(400,'UnknownError','Error need to be identified'))
def getEmployeePositions(): '''This function will return the all the positions available Example : GET /api/v1/employeepositions HTTP/1.1 Result : { "data": [ { "capacity": 2, "alias" : "table 1", "status" : "empty" },..... ''' with SessionManager(Session) as session: try: sql_positions = session.query(EmployeePosition).order_by( EmployeePosition.id).all() positions = [ dict(name=position.name, id=position.id, uri=url_for('getEmployeePosition', p_id=position.id), description=position.description) for position in sql_positions ] return jsonify(envelop(positions, 200)) except: return jsonify( error_envelop(400, ' UnkownError', 'Error need to identified'))
def getEmployeePosition(p_id): '''This function will return the particular payment from list of payments Example : GET /api/v1/dinetables/1 HTTP/1.1 Result : { "alias": "Robus", "capacity": 8, "id": 1, "uri" : "/api/v1/dinetables/1" } ''' with SessionManager(Session) as session: try: sql_position = session.query(EmployeePosition).filter( EmployeePosition.id == p_id).one() name = sql_position.name description = sql_position.description id = sql_position.id uri = url_for('getEmployeePosition', p_id=sql_position.id) data = dict(name=name, description=description, id=id, uri=uri) return jsonify(envelop(data, 200)) except NoResultFound: return jsonify( error_envelop(404, 'NoResultFound', 'Id : {0} Not Found'.format(p_id))) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def getDailyTotalBills(): with SessionManager(Session) as session: result = session.execute( 'select count(id) from bills where bill_time_stamp::date = current_date limit 1' ) for row in result.fetchall(): total_bills = row[0] return jsonify(total_bills=total_bills)
def getTopDineTable(): with SessionManager(Session) as session: result = session.execute( 'select dinetable_id, count(dinetable_id) from bills where bill_time_stamp::date=current_date group by dinetable_id order by count(dinetable_id) desc limit 1' ) for row in result.fetchall(): dinetable_id = row[0] total_bills = row[1] return jsonify(dinetable_id=dinetable_id, total_bills=total_bills)
def setBill(): with SessionManager(Session) as session: try: total_price = request.json['total_price'] on_site = request.json.get('on_site') customer_id = request.json['customer_id'] dinetable_id = request.json['dinetable_id'] employee_id = request.json['employee_id'] payment_id = request.json['payment_id'] vat_id = request.json['vat_id'] service_charge_id = request.json['service_charge_id'] bill_description = request.json.get('bill_description', 'NA') item_orders = request.json['item_orders'] if len(item_orders) < 1: return jsonify( error_envelop(400, 'BillError', 'Please enter atleast one item!!')) #if there is more than one elements in bills then create a new bill object bill = Bill(total_price=total_price, bill_description=bill_description, on_site=on_site, customer_id=customer_id, dinetable_id=dinetable_id, employee_id=employee_id, payment_id=payment_id, vat_id=vat_id, service_charge_id=service_charge_id) session.add(bill) for item_order in item_orders: item_id = item_order['item_id'] quantity = item_order['quantity'] order_price = item_order['order_price'] session.add( ItemOrder(item_id=item_id, quantity=quantity, order_price=order_price, bill=bill)) session.commit() return jsonify(post_envelop(200, data=request.json)) except DataError: #this excepyion might probably occur if the value key has a value of non integer return jsonify( error_envelop(400, 'DataError', 'Use the correct value')) except IntegrityError: return jsonify( error_envelop(400, 'IntegrityError', 'Foreign Key violations. Use the correct id')) except: return jsonify( error_envelop(400, 'UnknownError', 'Error need to be identified'))
def getDailyTopItem(): with SessionManager(Session) as session: result = session.execute( 'select sum(quantity) , item_id from itemorder where order_time_stamp::date >= current_date group by item_id order by sum(quantity) desc limit 1' ) for row in result.fetchall(): item_id = row[1] total_order = row[0] return jsonify(item_id=item_id, total_orders=total_order)
def deleteItemCategory(id): with SessionManager(Session) as session: try: cat_item = session.query(ItemCategory).filter( ItemCategory.id == id).one() session.delete(cat_item) session.commit() except: return jsonify(error_envelop(404, 'ValueError', 'Id Not Found')) #if succesfully deleted return jsonify(delete_envelop(200))
def setItemCategory(): name = request.json['name'] extra = request.json.get('extra', 'No Information found') with SessionManager(Session) as session: cat = ItemCategory(name=name, extra=extra) session.add(cat) try: session.commit() except IntegrityError: return jsonify(dict(error='{0} already exists!!'.format(name))) return jsonify(dict(status='created successfully'))