def create_item(body_params): """ Create an item :param body_params: :bodyparam title: Title of the item :bodyparam description: Description of the item :bodyparam category_id: Identifier of the category to which this item will belong :raise ValidationError 400: if form is messed up :raise DuplicatedEntity 400: If try to create an existed object. :raise BadRequest 400: if the body mimetype is not JSON :raise Unauthorized 401: If not login :raise NotFound 404: If category_id is not valid :return: the created item """ if CategoryModel.find_by_id(body_params['category_id']) is None: raise NotFound(error_message='Category with this id doesn\'t exist.') if ItemModel.query.filter_by(title=body_params['title']).first(): raise DuplicatedEntity(error_message='Item with this title exists.') body_params['creator_id'] = get_jwt_identity() item = ItemModel(**body_params) item.save() return create_data_response(item_schema.dump(item))
def delete_category(category_id): """ Delete the category with id :param category_id: ID of the category we want to delete :raise Unauthorized 401: If user is not login-ed :raise Forbidden 403: if user try to update other user's category :raise Not Found 404: If category with that id doesn't exist :return: 204 response """ category = CategoryModel.find_by_id(category_id) if category is None: raise NotFound(error_message='Category with this id doesn\'t exist.') creator_id = get_jwt_identity() if creator_id != category.creator_id: raise Forbidden( error_message='You can\'t delete other users\'s category') db.session.query(ItemModel).filter( ItemModel.category_id == category_id).delete() # delete all items in this category category.delete() return Response(status=StatusCodeEnum.NO_CONTENT)
def update_item(item_id, body_params): """ Update the item with id :param item_id: ID of the item we want to update :param body_params: :bodyparam title: Title of the item :bodyparam description: Description of the item :raise ValidationError 400: if form is messed up :raise DuplicatedEntity 400: if there is a item with the title. :raise BadRequest 400: if the body mimetype is not JSON :raise Unauthorized 401: If not login :raise Forbidden 403: If user tries to delete other user's items :raise NotFound 404: If category_id is not valid or item with id is not valid :return: the updated item """ category_id = body_params.get('category_id') # After SchemaValidation, category_id is either None or a number, None will pass through this test if category_id and CategoryModel.find_by_id(category_id) is None: raise NotFound(error_message='Category with this id doesn\'t exist.') item = ItemModel.find_by_id(item_id) if item is None: raise NotFound(error_message='Item with this id doesn\'t exist.') if item.creator_id != get_jwt_identity(): raise Forbidden(error_message='You can\'t update other users\'s item') title = body_params.get('title') description = body_params.get('description') if title: if ItemModel.query.filter_by(title=title).first(): raise DuplicatedEntity(error_message='Item with this title has already existed.') item.title = title if description: item.description = description if category_id: item.category_id = category_id item.save() return create_data_response(item_schema.dump(item))
def get_item(item_id): """ Get the item with id :param item_id: id of the category :raise Not Found 404: If item with that id doesn't exist :return: Item with that id """ item = ItemModel.find_by_id(item_id) if item is None: raise NotFound(error_message='Item with this id doesn\'t exist.') return create_data_response(item_schema.dump(item))
def get_category(category_id): """ Get the category by id :param category_id: id of the category want to get :raise Not Found 404: If category with that id doesn't exist :return: Category with that id """ category = CategoryModel.find_by_id(category_id) if category is None: raise NotFound(error_message='Category with this id doesn\'t exist.') return create_data_response(category_schema.dump(category))
def delete_item(item_id): """ Delete the item with id :param item_id: ID of the item we want to delete :raise Unauthorized 401: If not login :raise Forbidden 403: If user tries to delete other user's items :raise Not Found 404: If item with that id doesn't exist :return: 204 response """ item = ItemModel.find_by_id(item_id) if item is None: raise NotFound(error_message='Item with this id doesn\'t exist.') if item.creator_id != get_jwt_identity(): raise Forbidden(error_message='You can\'t delete other users\'s item') item.delete() return Response(status=StatusCodeEnum.NO_CONTENT)
def update_category(category_id, body_params): """ Update the category with id :param category_id: ID of the category we want to update :param body_params: :bodyparam title: Title of the category :bodyparam description: Description of the category :raise ValidationError 400: if form is messed up :raise DuplicatedEntity 400: if there is a category with the title :raise BadRequest 400: if the body mimetype is not JSON :raise Unauthorized 401: If user is not login-ed :raise Forbidden 403: if user try to update other user's category :raise Not Found 404: If category with that id doesn't exist :return: the updated category """ category = CategoryModel.find_by_id(category_id) if category is None: raise NotFound(error_message='Category with this id doesn\'t exist.') creator_id = get_jwt_identity() if creator_id != category.creator_id: raise Forbidden( error_message='You can\'t update other users\'s category') title = body_params.get('title') description = body_params.get('description') if title: if CategoryModel.query.filter_by(title=title).first(): raise DuplicatedEntity( error_message='There is already a category with this title.') category.title = title if description: category.description = description category.save() return create_data_response(category_schema.dump(category))