async def get_all(self, request): """Returns information about all users""" users = await UserModel().get_all(request.app['db']) if not users: log.debug('Not found users') raise web.HTTPNotFound() return web.Response(body=json.dumps(users).encode('utf-8'), content_type='application/json')
async def get(self, request): """Returns information about user by user_id""" user_id = request.match_info.get('user_id') user = await UserModel().get(request.app['db'], user_id) if not user: log.debug('Not found user for user_id: {}'.format(user_id)) raise web.HTTPNotFound() return web.Response(body=json.dumps(user).encode('utf-8'), content_type='application/json')
async def get(self, request): """Returns information about entity by entity_id""" entity_id = request.match_info.get('entity_id') entity = await EntityModel().get(request.app['db'], entity_id) if not entity: log.debug('Not found entity for entity_id: {}'.format(entity_id)) raise web.HTTPNotFound() return web.Response(body=json.dumps(entity).encode('utf-8'), content_type='application/json')
async def get(self, request): """Returns information about comment by comment_id""" comment_id = request.match_info.get('comment_id') comment = await CommentTreeModel().get(request.app['db'], comment_id=comment_id) if not comment: log.debug( 'Not found comment for comment_id: {}'.format(comment_id)) raise web.HTTPNotFound() return web.Response(body=json.dumps(comment).encode('utf-8'), content_type='application/json')
async def create(self, request): """Create comment""" data = await self._create_request_validation(request) data['engine'] = request.app['db'] comment_id = await CommentTreeModel().create(**data) if not comment_id: raise web.HTTPBadRequest() url = request.app.router['comment_details'].url( parts={'comment_id': comment_id}) return web.HTTPCreated(body=json.dumps({ 'url': url }).encode('utf-8'), content_type='application/json')
async def create(self, request): """Create user with given name""" data = await request.post() user_name = data.get('name') if not user_name: log.debug('Not found user name in request') raise web.HTTPBadRequest() user_id = await UserModel().create(request.app['db'], user_name) if not user_id: log.debug('Not unique user name: {}'.format(user_name)) raise web.HTTPBadRequest() url = request.app.router['user_details'].url( parts={'user_id': user_id}) return web.HTTPCreated(body=json.dumps({ 'url': url }).encode('utf-8'), content_type='application/json')
async def search(self, request): """Returns information about coments node by comment_id or entity_id""" entity_id = request.GET.get('entity_id') comment_id = request.GET.get('comment_id') try: entity_id = int(entity_id) if entity_id else None comment_id = int(comment_id) if comment_id else None except ValueError: log.debug('CREATE: Not valid format entity_id or comment_id') pass comment = await CommentTreeModel().get_tree(request.app['db'], entity_id=entity_id, comment_id=comment_id) if not comment: log.debug('Not found comments node') raise web.HTTPNotFound() return web.Response(body=json.dumps(comment).encode('utf-8'), content_type='application/json')
async def create(self, request): """Create entity with given name, type""" data = await request.post() entity_name = data.get('name') entity_type = data.get('type') if not entity_name or not entity_type: log.debug('Not found entity_name or entity_type in request') raise web.HTTPBadRequest() entity_id = await EntityModel().create(request.app['db'], entity_name, entity_type) if not entity_id: raise web.HTTPBadRequest() url = request.app.router['entity_details'].url( parts={'entity_id': entity_id}) return web.HTTPCreated(body=json.dumps({ 'url': url }).encode('utf-8'), content_type='application/json')
async def get_all(self, request): """Returns information about comments""" entity_id = request.GET.get('entity_id') if not entity_id: log.debug('Not found entity_id in request') raise web.HTTPBadRequest() try: page = int(request.GET.get('page', 1)) except ValueError: log.debug('Not valid format page') raise web.HTTPBadRequest() comments = await CommentTreeModel().get_all_first( request.app['db'], entity_id, page) if not comments: log.debug( 'Not found comment for entity_id: {}, on page: {}'.format( entity_id, page)) raise web.HTTPNotFound() return web.Response(body=json.dumps(comments).encode('utf-8'), content_type='application/json')