def get_items_by_category_id(self, cat_id): try: items_raw = self.session.query(Item).filter_by(cat_id=cat_id) except Exception: self.session.rollback() return None finally: self.session.close() item_schema = ItemSchema(many=True) items = item_schema.dump(items_raw).data return items
def get_all_items(self): try: items_raw = self.session.query(Item).all() except Exception: self.session.rollback() return None finally: self.session.close() item_schema = ItemSchema(many=True) items = item_schema.dump(items_raw).data return items
def get_item_by_name(self, name): try: item = self.session.query(Item).filter_by(name=name).one() except Exception: self.session.rollback() return None finally: self.session.close() item_schema = ItemSchema() data = item_schema.dump(item).data return data
def get_item_by_id(self, id): try: item = self.session.query(Item).filter_by(id=id).one() item_schema = ItemSchema() data = item_schema.dump(item).data except Exception: self.session.rollback() return None finally: self.session.close() return data
def get_latest_items(self, limit): try: items_raw = self.session.query(Item)\ .order_by(desc(Item.id))\ .limit(limit)\ .all() except Exception: self.session.rollback() return None finally: self.session.close() item_schema = ItemSchema(many=True) items = item_schema.dump(items_raw).data return items
def post(self, name): if ItemModel.find_by_name(name): return {'message': "An item with name '{}' already exists.".format(name)}, 400 # bad request json_data = request.get_json() try: # Make sure that input in equal to schema data = ItemSchema().load(json_data) except ValidationError as err: response = jsonify(err.messages) response.status_code = 422 return response item = ItemModel(name, **data) try: item.save_to_db() except ValueError: return {"message": "An error occurred inserting the item."}, 500 # Internal server error return item.json(), 201 # created status code
def put(self, name): json_data = request.get_json() item = ItemModel.find_by_name(name) # Item is already there, but characteristics will be modified if item: # Specify which parameters will be modified item.price = json_data['price'] item.save_to_db() else: try: # Make sure that input in equal to schema data = ItemSchema().load(json_data) except ValidationError as err: response = jsonify(err.messages) response.status_code = 422 return response item = ItemModel(name, **data) item.save_to_db() return item.json()
from models.item import Item, ItemSchema from datetime import datetime item_schema = ItemSchema() def create_item(data, new_user): #Called from item_controller.py print(f'Enter create_item:new_user:{new_user}') new_item = Item(model_name=data['model_name'], model_type=data['model_type'], model_num=data['model_num'], color=data['color'], price=data['price'], user_id=new_user, date_created=datetime.utcnow(), last_modified=datetime.utcnow()) try: new_item.save() message = 'Item saved successfully' return message, 200 except Exception as e: return str(e), 400 # return "Created new item" def fetch_one_item(item_id): print(f'*******Enter fetch_one_item::item_service.py') x = Item.get_one_item(item_id) # get_one_item() defined in item.py print(f'x:{x}')