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 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 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 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 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 updateMembership(m_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_membership = session.query(Membership).filter( Membership.id == m_id).one() sql_membership.m_type = request.json.get('m_type', sql_membership.m_type) sql_membership.discount = request.json.get('discount', sql_membership.discount) #check weather the discount is between 0 and 100 if not 0 <= int(sql_membership.discount) < 100: return jsonify( error_envelop( 400, 'DataError', 'Enter the valid discount amount (0 to 100)')) sql_membership.description = request.json.get( 'description', sql_membership.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(m_id))) return jsonify(error_envelop(100, 'UnknownError', 'UnknownError Found'))
def updateItemCategory(id): '''This function is used to update the item category in the database''' with SessionManager(Session) as session: try: item_category = session.query(ItemCategory).filter( ItemCategory.id == id).one() item_category.name = request.json.get('name', item_category.name) item_category.extra = request.json.get('extra', item_category.extra) #sesion.add(item_category) session.commit() except IntegrityError: # if name already exsits in database return jsonify( error_envelop(400, 'Integrity Error', 'Name already Exists')) except: return jsonify(error_envelop(404, 'ValueError', 'Id not found')) #now the item is succesfulluy updated return jsonify(update_envelop(200))
def updateCustomerByMembership(m_id, c_id): with SessionManager(Session) as session: try: customer = session.query(Customer).filter( Customer.id == c_id).one() customer.first_name = request.json.get('first_name', customer.first_name) customer.middle_name = request.json.get('middle_name', customer.middle_name) customer.last_name = request.json.get('last_name', customer.last_name) customer.address = request.json.get('address', customer.address) customer.gender = request.json.get('gender', customer.gender) customer.age = request.json.get('age', customer.age) customer.email = request.json.get('email', customer.email) customer.contact_number = request.json.get('contact_number', customer.contact_number) try: customer.age = int(customer.age) except: return jsonify( error_envelop(200, "AgeError", 'Plase use the integer for age')) if not 7 < int(customer.age) < 99: return jsonify( error_envelop( 400, 'AgeError', 'Please input the valid age between 7 and 99')) #id everything goes right ..make the commit session.commit() return jsonify(update_envelop(200, data=request.json)) except: return jsonify( error_envelop(400, 'UnknownError', 'Error need to be identified!!'))
def updateEmployeeByPosition(p_id, e_id): '''{"first_name" : "Robus sujan gauli", "last_name" : "Gasd" , "gender" : "M", "date_of_birth" : "2014-12-12", "salary" : 2343 , "contact_number" : "27567344", "address" : "ktdm" , "age" : 13} ''' with SessionManager(Session) as session: try: #employee = session.query(Employee).filter(Employee.id == e_id).one() employee = session.query(Employee).filter( Employee.id == e_id).one() employee.first_name = request.json.get('first_name', employee.first_name) employee.middle_name = request.json.get('middle_name', employee.middle_name) employee.last_name = request.json.get('last_name', employee.last_name) employee.gender = request.json.get('gender', employee.gender) employee.date_of_birth = request.json.get('date_of_birth', employee.date_of_birth) employee.salary = request.json.get('salary', employee.salary) employee.contact_number = request.json.get('contact_number', employee.contact_number) employee.address = request.json.get('address', employee.address) employee.age = request.json.get('age', employee.age) employee.photo_uri = request.json.get('photo_uri', employee.photo_uri) employee.email = request.json.get('email', employee.email) if not 6 <= int(employee.age) < 99: return jsonify( error_envelop(400, "Age Error", "Please enter the age between 6 and 99 ")) if (not employee.contact_number.isdigit()) or len( employee.contact_number) > 14: return jsonify( error_envelop(400, 'ContactError', 'Please enter the valid contact number')) if len(employee.gender) != 1 or not ('M' in employee.gender or 'F' in employee.gender): return jsonify( error_envelop( 400, 'GenderError', 'Please enter the valid gender. options : M or F')) #id everything goes right ..make the commit 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', 'Contact number :{0} already Exists'.format( contact_number))) except NoResultFound: return jsonify( error_envelop(404, 'ValueError', 'Id : {0} not found'.format(e_id))) except: return jsonify( error_envelop(400, 'UnknownError', 'Error need to be identified!!'))