async def put(request, post_id): post_repo = get_post_repo() try: data = request.json except InvalidUsage as e: return text( 'request body "%s" cannot be parsed. Err: %s' % (request.body, e), status=http.HTTPStatus.BAD_REQUEST, ) updated_post = await post_repo.update(post_id, data) return text(updated_post)
async def post(request): """ :param request: :return: post_id should be included inside post body """ post_repo = get_post_repo() try: post = request.json except InvalidUsage as e: return text( 'request body "%s" cannot be parsed. Err: %s' % (request.body, e), status=http.HTTPStatus.BAD_REQUEST, ) post = await post_repo.save(post) return text(post)
async def delete(request, post_id): post_repo = get_post_repo() await post_repo.delete(post_id) return text("ok")
async def get_one(request, post_id): post_repo = get_post_repo() post = await post_repo.get(post_id) return text(post)
async def get_all(request): post_repo = get_post_repo() posts = await post_repo.get_all() return text(posts)
def __init__(self, db): self._db = db self.post_repo = get_post_repo()