class AppHandler(tornado.web.RequestHandler): def initialize(self): self.repository = Repository() def get(self): self.write( json.dumps(self.repository.get(self.get_argument('id')), cls=EntityEncoder)) def post(self): self.write( json.dumps(self.repository.update( json.loads(self.request.body, cls=EntityDecoder)), cls=EntityEncoder)) def put(self): self.write( json.dumps(self.repository.create( json.loads(self.request.body, cls=EntityDecoder)), cls=EntityEncoder)) def delete(self): self.write(json.dumps(self.repository.remove(self.get_argument('id'))))
class Service(metaclass=Singleton): def __init__(self): self.repository = Repository() @log def save(self, body): entity = json.loads(body, cls=EntityDecoder) return json.dumps(self.repository.create(entity), cls=EntityEncoder) @log def remove(self, id): return json.dumps(self.repository.remove(id), cls=EntityEncoder) @log def get(self, id): return json.dumps(self.repository.get(id), cls=EntityEncoder) @log def update(self, body): entity = json.loads(body, cls=EntityDecoder) return json.dumps(self.repository.update(entity), cls=EntityEncoder)