|
| 1 | +from aiohttp import web |
| 2 | + |
| 3 | +from benchmarks.aiohttp.without_fastopenapi.schemas import ( |
| 4 | + RecordCreate, |
| 5 | + RecordResponse, |
| 6 | + RecordUpdate, |
| 7 | +) |
| 8 | +from benchmarks.aiohttp.without_fastopenapi.storage import RecordStore |
| 9 | + |
| 10 | +store = RecordStore() |
| 11 | + |
| 12 | + |
| 13 | +async def get_all_records(request: web.Request): |
| 14 | + records = store.get_all() |
| 15 | + return web.json_response([record.model_dump() for record in records], status=200) |
| 16 | + |
| 17 | + |
| 18 | +async def get_one_record(request: web.Request): |
| 19 | + record_id = request.match_info.get("record_id") |
| 20 | + record = store.get_by_id(record_id) |
| 21 | + if record: |
| 22 | + return web.json_response(record.model_dump(), status=200) |
| 23 | + else: |
| 24 | + return web.json_response({"error": "Record not found"}, status=404) |
| 25 | + |
| 26 | + |
| 27 | +async def create_record(request: web.Request): |
| 28 | + try: |
| 29 | + data = await request.json() |
| 30 | + record_data = RecordCreate(**data) |
| 31 | + new_record = store.create(record_data) |
| 32 | + return web.json_response(new_record.model_dump(), status=201) |
| 33 | + except ValueError as e: |
| 34 | + return web.json_response({"error": str(e)}, status=400) |
| 35 | + |
| 36 | + |
| 37 | +async def replace_record(request: web.Request): |
| 38 | + record_id = request.match_info.get("record_id") |
| 39 | + if not store.get_by_id(record_id): |
| 40 | + return web.json_response({"error": "Record not found"}, status=404) |
| 41 | + try: |
| 42 | + data = await request.json() |
| 43 | + record_data = RecordCreate(**data) |
| 44 | + store.delete(record_id) |
| 45 | + record = {"id": record_id, **record_data.model_dump()} |
| 46 | + store.records[record_id] = record |
| 47 | + return web.json_response(RecordResponse(**record).model_dump(), status=200) |
| 48 | + except ValueError as e: |
| 49 | + return web.json_response({"error": str(e)}, status=400) |
| 50 | + |
| 51 | + |
| 52 | +async def update_record(request: web.Request): |
| 53 | + record_id = request.match_info.get("record_id") |
| 54 | + try: |
| 55 | + data = await request.json() |
| 56 | + record_data = RecordUpdate(**data) |
| 57 | + updated_record = store.update(record_id, record_data) |
| 58 | + if updated_record: |
| 59 | + return web.json_response(updated_record.model_dump(), status=200) |
| 60 | + else: |
| 61 | + return web.json_response({"error": "Record not found"}, status=404) |
| 62 | + except ValueError as e: |
| 63 | + return web.json_response({"error": str(e)}, status=400) |
| 64 | + |
| 65 | + |
| 66 | +async def delete_record(request: web.Request): |
| 67 | + record_id = request.match_info.get("record_id") |
| 68 | + if store.delete(record_id): |
| 69 | + return web.Response(status=204) |
| 70 | + else: |
| 71 | + return web.json_response({"error": "Record not found"}, status=404) |
| 72 | + |
| 73 | + |
| 74 | +app = web.Application() |
| 75 | +app.router.add_get("/records", get_all_records) |
| 76 | +app.router.add_get("/records/{record_id}", get_one_record) |
| 77 | +app.router.add_post("/records", create_record) |
| 78 | +app.router.add_put("/records/{record_id}", replace_record) |
| 79 | +app.router.add_patch("/records/{record_id}", update_record) |
| 80 | +app.router.add_delete("/records/{record_id}", delete_record) |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + web.run_app(app, host="127.0.0.1", port=8000) |
0 commit comments