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 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 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 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 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 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 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 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 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 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 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 getBill(b_id): with SessionManager(Session) as session: try: bill = session.query(Bill).filter(Bill.id == b_id).one() #cus = bill.customer customer = bill.customer.first_name if bill.customer else 'Not Available' dinetable = bill.dinetable.alias if bill.dinetable else 'Not Available' employee = bill.employee.first_name if bill.employee else 'Not Available' payment = bill.payment.p_type if bill.payment else 'Not Available' vat = bill.vat.name if bill.vat else 'Not Available' service_charge = bill.service_charge.name if bill.service_charge else 'Not Available' bill_description = bill.bill_description on_site = bill.on_site total_price = bill.total_price bill_time_stamp = bill.bill_time_stamp items_orders = bill.items list_item_orders = [] for item_order in items_orders: list_item_orders.append( dict(item_id=item_order.item_id, bill_id=item_order.bill_id, quantity=item_order.quantity, order_price=item_order.order_price, order_time_stamp=item_order.order_time_stamp, item_name=item_order.item.name if item_order.item else 'Not Available', item_unit_price=item_order.item.unit_price if item_order.item else 0.0)) b = dict(customer=customer, dinetable=dinetable, employee=employee, payment=payment, vat=vat, bill_time_stamp=str(bill_time_stamp), service_charge=service_charge, bill_description=bill_description, on_site=on_site, total_price=total_price, item_orders=list_item_orders) return jsonify(envelop(data=b, code=200)) except NoResultFound: return jsonify( error_envelop(404, 'NoResultFound', 'Id : {0} Not Found'.format(b_id))) return jsonify(error_envelop(400, 'UnknownError', 'Somethig went wrong'))
def getItemCategory(id): ''' This function gets the single category given the id''' with SessionManager(Session) as session: try: category = session.query(ItemCategory).filter( ItemCategory.id == id).one() except: return jsonify( error_envelop(error_code=404, error_type='Value Error', error_message='ID is not available')) category_dict = dict(name=category.name, extra=category.extra, id=category.id) return jsonify(envelop(code=200, data=category_dict, pagination=None))
def getVats(): '''This function will return the all the vats available Example : GET /api/v1/vats HTTP/1.1 Result : { "data": [ { "name": "Pizzaaa iera", "value": 13 },..... ''' with SessionManager(Session) as session: try: sql_vats = session.query(Vat).order_by(Vat.id).all() vats = [dict(name=vat.name, value=vat.value, id=vat.id, uri=url_for('getVat', vat_id=vat.id)) for vat in sql_vats] return jsonify(envelop(vats, 200)) except: return jsonify(error_envelop(400, ' UnkownError', 'Error need to identified'))
def getPayment(p_id): '''This function will return the particular payment from list of payments Example : GET /api/v1/payments/1 HTTP/1.1 Result : { "id": 1, "uri" : "/api/v1/payments/1" "p_type": "onsite" } ''' with SessionManager(Session) as session: try: sql_payment = session.query(Payment).filter(Payment.id == p_id).one() p_type = sql_payment.p_type id = sql_payment.id uri = url_for('getPayment', p_id=sql_payment.id) data = dict(p_type=p_type, 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 getPayments(): '''This function will return the all the payments available Example : GET /api/v1/payments HTTP/1.1 Result : { "data": [ { "P_type": "onsite", },..... ''' with SessionManager(Session) as session: try: sql_payments = session.query(Payment).order_by(Payment.id).all() payments = [dict(p_type=payment.p_type, id=payment.id, uri=url_for('getPayment', p_id=payment.id) ) for payment in sql_payments] return jsonify(envelop(payments, 200)) except: return jsonify(error_envelop(400, ' UnkownError', 'Error need to identified'))
def getServiceCharges(): '''This function will return the all the service charges available Example : GET /api/v1/servicecharges HTTP/1.1 Result : { "data": [ { "name": "service charge", "value": 10 },..... ''' with SessionManager(Session) as session: try: sql_servicecharges = session.query(ServiceCharge).order_by(ServiceCharge.id).all() servicecharges = [dict(name=service.name, value=service.value, id=service.id, uri=url_for('getServiceCharge', s_id=service.id)) for service in sql_servicecharges] return jsonify(envelop(servicecharges, 200)) except: return jsonify(error_envelop(400, ' UnkownError', 'Error need to identified'))
def getVat(vat_id): '''This function will return the particular vat from the list of vats Example : GET /api/v1/vats/1 HTTP/1.1 Result : { "id": 1, "uri" : "/api/v1/vats/1" "name": "Coke Vat", "value": 13 } ''' with SessionManager(Session) as session: try: sql_vat = session.query(Vat).filter(Vat.id == vat_id).one() name = sql_vat.name value = sql_vat.value vat_id = sql_vat.id uri = url_for('getVat', vat_id=vat_id) data = dict(name=name, value=value, id=vat_id, uri=uri) return jsonify(envelop(data, 200)) except NoResultFound: return jsonify(error_envelop(404, 'NoResultFound', 'Id : {0} Not Found'.format(vat_id))) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def getServiceCharge(s_id): '''This function will return the particular vat from the list of vats Example : GET /api/v1/servicescharges/1 HTTP/1.1 Result : { "id": 1, "uri" : "/api/v1/servicecharges/1" "name": "Charges", "value": 10 } ''' with SessionManager(Session) as session: try: sql_service = session.query(ServiceCharge).filter(ServiceCharge.id == s_id).one() name = sql_service.name value = sql_service.value service_charge_id = sql_service.id uri = url_for('getServiceCharge', s_id=service_charge_id) data = dict(name=name, value=value, id=service_charge_id, uri=uri) return jsonify(envelop(data, 200)) except NoResultFound: return jsonify(error_envelop(404, 'NoResultFound', 'Id : {0} Not Found'.format(s_id))) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def getEmployeeByPosition(p_id, e_id): with SessionManager(Session) as session: try: sql_employee = session.query(Employee).filter( Employee.id == e_id).one() sql_position = sql_employee.e_position employee = dict(id=sql_employee.id, uri=url_for('getEmployeeByPosition', p_id=p_id, e_id=e_id), first_name=sql_employee.first_name, middle_name=sql_employee.middle_name, last_name=sql_employee.last_name, contact_number=sql_employee.contact_number, gender=sql_employee.gender, age=sql_employee.age, email=sql_employee.email, join_date=str(sql_employee.join_date.date()), address=sql_employee.address, date_of_birth=str(sql_employee.date_of_birth), salary=sql_employee.salary, photo_uri=sql_employee.photo_uri, position=dict(name=sql_position.name, description=sql_position.description, id=sql_position.id, uri=url_for('getEmployeePosition', p_id=p_id))) return jsonify(envelop(employee, 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(e_id))) except: return jsonify( error_envelop(400, 'UnknownError', 'Something went wrong'))
def getDineTable(d_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_dinetable = session.query(DineTable).filter(DineTable.id == d_id).one() alias = sql_dinetable.alias id = sql_dinetable.id capacity = sql_dinetable.capacity uri = url_for('getDineTable', d_id=sql_dinetable.id) status = sql_dinetable.status data = dict(alias=alias, capacity=capacity, id=id, uri=uri, status=status) return jsonify(envelop(data, 200)) except NoResultFound: return jsonify(error_envelop(404, 'NoResultFound', 'Id : {0} Not Found'.format(d_id))) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def getItems(): ''' This function return all the items in the all the categories Example : GET /api/v1/items HTTP/1.1 Result : { "id": 1, "item_photo_uri": "Image URI Not Available", "name": "coke", "unit_price": 45 },............. ... ''' page = None size = None pagination = None sql_items = [] #empty items if 'page' and 'size' in request.args: page = int(request.args['page']) size = int(request.args['size']) with SessionManager(Session) as session: #perform the pagination if given request.args if isinstance(page, int) and isinstance(size, int): sql_items = session.query(Item).order_by( Item.id).all()[page * size:page * size + size] pagination = url_for('getItems') + '?page={0}&size={1}'.format( page + 1, size) elif not request.args: #if there is no arguments in the url sql_items = session.query(Item).order_by(Item.id).all() items = [ dict(url_id=url_for('getItem', item_id=item.id), id=item.id, name=item.name, item_photo_uri=item.item_photo_uri, description=item.description, unit_price=item.unit_price, item_category_id=item.item_category_id) for item in sql_items ] return jsonify(envelop(data=items, code=200, pagination=pagination))
def getCustomers(): with SessionManager(Session) as session: sql_customers = session.query(Customer).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, customer_join_date=customer.customer_join_date, membership=dict(m_type=customer.c_membership.m_type, discount=customer.c_membership.discount, description=customer.c_membership.description, id=customer.c_membership.id)) for customer in sql_customers ] return jsonify(envelop(data=customers, code=200)) return jsonify( error_envelop(404, 'UnknownError', 'Error need to be identified'))