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() 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 except: return jsonify(error_envelop(404, 'ValueError', 'Invalid ID')) return jsonify(envelop(data=item, 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 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)) 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 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'))
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 setCategoryItem(cat_id): '''This function is used to set the item on the category using the on the given cate id''' name = request.json['name'] unit_price = int(request.json['unit_price']) with SessionManager(Session) as session: category = session.query(ItemCategory).filter( ItemCategory.id == cat_id).one() category_item = Item(name=name, unit_price=unit_price) category.c_items.append(category_item) session.commit() return jsonify(dict(status='created'))
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 updateCategoryItem(cat_id, item_id): '''This function is used to update the database item. Example : PUT /api/v1/itemcategories/12/items/1 HTTP/1.1 Request Body {"name" : "Changed Name", "unit_price" : 234} Result : {"Status" : " Successfully Updated", "code" =200} ''' with SessionManager(Session) as session: try: sql_item = session.query(Item).filter(Item.id == item_id).one() #it will return the value if exists or throws an exception if does not find one sql_item.name = request.json.get('name', sql_item.name) sql_item.unit_price = request.json.get('unit_price', sql_item.unit_price) session.commit() except: return jsonify( error_envelop(404, 'ValueError', 'Id : {0} not found'.format(item_id))) return jsonify(dict(status='created Successfully'))
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 deleteCategoryItem(cat_id, item_id): ''' This function will delete the item in the categories Example : DELETE api/v1/itemcategories/12/items/2 HTTP/1.1 Result : { "meta": { "code": 200, "message": "Deleted Successfully" } } ''' with SessionManager(Session) as session: try: sql_item = session.query(Item).filter(Item.id == item_id).one() session.delete(sql_item) session.commit() return jsonify(delete_envelop(code=200)) except: return jsonify( error_envelop( error_code=404, error_type='ValueError', error_message='No ID:{0} found!!'.format(item_id)))
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) for item in sql_items ] return jsonify(envelop(data=items, code=200, pagination=pagination))