async def post(self, request): data = await request.json() article = Article( title=data['title'], description=data['description'], created_at=data['created_at'], created_by=data['created_by'], priority=data['priority'] ) session.add(article) session.commit() return Response(status=201, body=self.resource.encode({ 'notes': [ { 'id': note.id, 'title': note.title, 'description': note.description, 'created': note.created, 'created_by': note.created_by, } for note in session.query(Article) ] }), content_type='application/json')
async def delete(self, instance_id): note = session.query(Article).filter(Article.id == instance_id).first() if not note: abort(404, message="Note {} doesn't exist".format(id)) session.delete(note) session.commit() return Response(status=204)
async def get(self, instance_id): instance = session.query(Article).filter( Article.id == instance_id ).first() if not instance: return Response( status=404, body=json.dumps({'not found': 404}), content_type='application/json' ) data = self.resource.render_and_encode(instance) return Response(status=200, body=data, content_type='application/json')
async def put(self, request, instance_id): data = await request.json() note = session.query(Article).filter(Article.id == instance_id).first() note.title = data['title'] note.description = data['description'] note.created_at = data['created_at'] note.created_by = data['created_by'] note.priority = data['priority'] session.add(note) session.commit() return Response(status=201, body=self.resource.render_and_encode(note), content_type='application/json')
async def get(self) -> Response: data = [] articles = session.query(Article).all() for instance in self.resource.collection.values(): data.append(self.resource.render(instance)) data = self.resource.encode(data) return Response(status=200, body=self.resource.encode({ 'articles': [ { 'id': article.id, 'title': article.title, 'description': article.description, 'created': article.created, 'created_by': article.created_by } for article in articles ] }), content_type='application/json')