|
1 | 1 | import functools |
2 | 2 |
|
3 | 3 | from aiohttp import web |
4 | | -from aiopg.sa.engine import Engine |
5 | 4 | from pydantic import parse_obj_as |
6 | 5 | from servicelib.aiohttp.requests_validation import ( |
7 | 6 | parse_request_body_as, |
|
12 | 11 | from simcore_postgres_database.utils_tags import ( |
13 | 12 | TagNotFoundError, |
14 | 13 | TagOperationNotAllowedError, |
15 | | - TagsRepo, |
16 | 14 | ) |
17 | 15 |
|
18 | 16 | from .._meta import API_VTAG as VTAG |
19 | | -from ..db.plugin import get_database_engine |
20 | 17 | from ..login.decorators import login_required |
21 | 18 | from ..security.decorators import permission_required |
22 | 19 | from ..utils_aiohttp import envelope_json_response |
| 20 | +from . import _api |
23 | 21 | from .schemas import ( |
24 | 22 | TagCreate, |
25 | | - TagGet, |
26 | 23 | TagGroupCreate, |
27 | 24 | TagGroupGet, |
28 | 25 | TagGroupPathParams, |
@@ -55,70 +52,56 @@ async def wrapper(request: web.Request) -> web.StreamResponse: |
55 | 52 | @permission_required("tag.crud.*") |
56 | 53 | @_handle_tags_exceptions |
57 | 54 | async def create_tag(request: web.Request): |
58 | | - engine: Engine = get_database_engine(request.app) |
| 55 | + assert request.app # nosec |
59 | 56 | req_ctx = TagRequestContext.parse_obj(request) |
60 | 57 | new_tag = await parse_request_body_as(TagCreate, request) |
61 | 58 |
|
62 | | - repo = TagsRepo(user_id=req_ctx.user_id) |
63 | | - async with engine.acquire() as conn: |
64 | | - tag = await repo.create( |
65 | | - conn, |
66 | | - read=True, |
67 | | - write=True, |
68 | | - delete=True, |
69 | | - **new_tag.dict(exclude_unset=True), |
70 | | - ) |
71 | | - model = TagGet.from_db(tag) |
72 | | - return envelope_json_response(model) |
| 59 | + created = await _api.create_tag( |
| 60 | + request.app, user_id=req_ctx.user_id, new_tag=new_tag |
| 61 | + ) |
| 62 | + return envelope_json_response(created) |
73 | 63 |
|
74 | 64 |
|
75 | 65 | @routes.get(f"/{VTAG}/tags", name="list_tags") |
76 | 66 | @login_required |
77 | 67 | @permission_required("tag.crud.*") |
78 | 68 | @_handle_tags_exceptions |
79 | 69 | async def list_tags(request: web.Request): |
80 | | - engine: Engine = get_database_engine(request.app) |
81 | | - req_ctx = TagRequestContext.parse_obj(request) |
82 | 70 |
|
83 | | - repo = TagsRepo(user_id=req_ctx.user_id) |
84 | | - async with engine.acquire() as conn: |
85 | | - tags = await repo.list_all(conn) |
86 | | - return envelope_json_response( |
87 | | - [TagGet.from_db(t).dict(by_alias=True) for t in tags] |
88 | | - ) |
| 71 | + req_ctx = TagRequestContext.parse_obj(request) |
| 72 | + got = await _api.list_tags(request.app, user_id=req_ctx.user_id) |
| 73 | + return envelope_json_response(got) |
89 | 74 |
|
90 | 75 |
|
91 | 76 | @routes.patch(f"/{VTAG}/tags/{{tag_id}}", name="update_tag") |
92 | 77 | @login_required |
93 | 78 | @permission_required("tag.crud.*") |
94 | 79 | @_handle_tags_exceptions |
95 | 80 | async def update_tag(request: web.Request): |
96 | | - engine: Engine = get_database_engine(request.app) |
97 | 81 | req_ctx = TagRequestContext.parse_obj(request) |
98 | 82 | path_params = parse_request_path_parameters_as(TagPathParams, request) |
99 | 83 | tag_updates = await parse_request_body_as(TagUpdate, request) |
100 | 84 |
|
101 | | - repo = TagsRepo(user_id=req_ctx.user_id) |
102 | | - async with engine.acquire() as conn: |
103 | | - tag = await repo.update( |
104 | | - conn, path_params.tag_id, **tag_updates.dict(exclude_unset=True) |
105 | | - ) |
106 | | - model = TagGet.from_db(tag) |
107 | | - return envelope_json_response(model) |
| 85 | + updated = await _api.update_tag( |
| 86 | + request.app, |
| 87 | + user_id=req_ctx.user_id, |
| 88 | + tag_id=path_params.tag_id, |
| 89 | + tag_updates=tag_updates, |
| 90 | + ) |
| 91 | + return envelope_json_response(updated) |
108 | 92 |
|
109 | 93 |
|
110 | 94 | @routes.delete(f"/{VTAG}/tags/{{tag_id}}", name="delete_tag") |
111 | 95 | @login_required |
112 | 96 | @permission_required("tag.crud.*") |
113 | 97 | @_handle_tags_exceptions |
114 | 98 | async def delete_tag(request: web.Request): |
115 | | - engine: Engine = get_database_engine(request.app) |
116 | 99 | req_ctx = TagRequestContext.parse_obj(request) |
117 | 100 | path_params = parse_request_path_parameters_as(TagPathParams, request) |
118 | 101 |
|
119 | | - repo = TagsRepo(user_id=req_ctx.user_id) |
120 | | - async with engine.acquire() as conn: |
121 | | - await repo.delete(conn, tag_id=path_params.tag_id) |
| 102 | + await _api.delete_tag( |
| 103 | + request.app, user_id=req_ctx.user_id, tag_id=path_params.tag_id |
| 104 | + ) |
122 | 105 |
|
123 | 106 | raise web.HTTPNoContent(content_type=MIMETYPE_APPLICATION_JSON) |
124 | 107 |
|
|
0 commit comments