def post(self): data = parser.parse_args() if CategoryModel.query.filter_by(name=data['name']).first(): msg = "A category with name:'{}' already exists.".format( data['name']) return {"message": msg}, 400 category = CategoryModel(**data) try: category.save() except: return {"message": "An error occurred while inserting Category"}, 500 return category.json(), 201
def put(self, category_id): data = parser.parse_args() category = CategoryModel.find_by_id(category_id) if category is None: new_category = CategoryModel(**data) try: new_category.save() return new_category.json(), 201 except: return {"message": "An error occurred while inserting Category"}, 500 try: category.update(**data) except: return {"message": "An error occurred while updating Category."}, 500 return category.json(), 200