|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 | 21 | # SOFTWARE. |
22 | 22 |
|
23 | | -from fastapi import APIRouter, Depends |
| 23 | +from typing import Union, List |
| 24 | +from fastapi import APIRouter, Depends, status |
| 25 | +from fastapi.responses import JSONResponse, Response |
24 | 26 | from sqlalchemy.orm import Session |
25 | 27 |
|
26 | 28 | from mrmat_python_api_fastapi.db import get_db |
27 | | -from .db import Resource |
28 | | -from .schema import ResourceSchema |
| 29 | +from mrmat_python_api_fastapi.apis import StatusSchema |
| 30 | +from .db import Resource, Owner |
| 31 | +from .schema import ResourceSchema, ResourceInputSchema, OwnerSchema, OwnerInputSchema |
29 | 32 |
|
30 | 33 | router = APIRouter() |
31 | 34 |
|
32 | 35 |
|
33 | | -@router.get('/', |
| 36 | +@router.get('/resources', |
34 | 37 | name='list_resources', |
35 | 38 | summary='List all known resources', |
36 | 39 | description='Returns all currently known resources and their metadata', |
37 | | - response_model=list[ResourceSchema]) |
| 40 | + response_model=List[ResourceSchema]) |
38 | 41 | async def get_all(db: Session = Depends(get_db)): |
39 | | - resources = db.query(Resource).all() |
40 | | - return resources |
| 42 | + return db.query(Resource).all() |
41 | 43 |
|
42 | 44 |
|
43 | | -@router.get('/{resource_id}', |
| 45 | +@router.get('/resources/{identity}', |
44 | 46 | name='get_resource', |
45 | 47 | summary='Get a single resource', |
46 | | - description='Return a single resource identified by its resource id') |
47 | | -async def get(resource_id: int): |
48 | | - pass |
| 48 | + description='Return a single resource identified by its resource id', |
| 49 | + response_model=Union[ResourceSchema, StatusSchema]) |
| 50 | +async def get(identity: int, db: Session = Depends(get_db)): |
| 51 | + resource = db.query(Resource).get(identity).one_or_none() |
| 52 | + if not resource: |
| 53 | + return JSONResponse(status_code=404, |
| 54 | + content=StatusSchema(code=404, msg='Not found').dict()) |
| 55 | + return resource |
49 | 56 |
|
50 | 57 |
|
51 | | -@router.post('/', |
| 58 | +@router.post('/resources', |
52 | 59 | name='create_resource', |
53 | 60 | summary='Create a resource', |
54 | | - description='Create a resource owned by the authenticated user') |
55 | | -async def create(): |
56 | | - pass |
| 61 | + description='Create a resource owned by the authenticated user', |
| 62 | + response_model=Union[ResourceSchema, StatusSchema]) |
| 63 | +async def create(data: ResourceInputSchema, db: Session = Depends(get_db)): |
| 64 | + resource = Resource(name=data.name) |
| 65 | + db.add(resource) |
| 66 | + db.commit() |
| 67 | + return resource |
57 | 68 |
|
58 | 69 |
|
59 | | -@router.put('/{resource_id}', |
| 70 | +@router.put('/resources/{identity}', |
60 | 71 | name='modify_resource', |
61 | 72 | summary='Modify a resource', |
62 | | - description='Modify a resource by its resource id') |
63 | | -async def modify(resource_id: int): |
64 | | - pass |
| 73 | + description='Modify a resource by its resource id', |
| 74 | + response_model=Union[ResourceSchema, StatusSchema]) |
| 75 | +async def modify(identity: int, data: ResourceInputSchema, db: Session = Depends(get_db)): |
| 76 | + resource = db.query(Resource).get(identity) |
| 77 | + if not resource: |
| 78 | + return JSONResponse(status_code=404, |
| 79 | + content=StatusSchema(code=404, msg='Not found').dict()) |
| 80 | + resource.name = data.name |
| 81 | + db.add(resource) |
| 82 | + db.commit() |
| 83 | + return resource |
65 | 84 |
|
66 | 85 |
|
67 | | -@router.delete('/{resource_id}', |
| 86 | +@router.delete('/resources/{identity}', |
68 | 87 | name='remove_resource', |
69 | 88 | summary='Remove a resource', |
70 | | - description='Remove a resource by its resource id') |
71 | | -async def remove(resource_id: int): |
72 | | - pass |
| 89 | + description='Remove a resource by its resource id', |
| 90 | + status_code=204, |
| 91 | + responses={ |
| 92 | + status.HTTP_204_NO_CONTENT: {'description': 'The resource was removed'}, |
| 93 | + status.HTTP_410_GONE: {'description': 'The resource was already gone'} |
| 94 | + }) |
| 95 | +async def remove(identity: int, response: Response, db: Session = Depends(get_db)): |
| 96 | + resource = db.query(Resource).get(identity) |
| 97 | + if not resource: |
| 98 | + return JSONResponse(status_code=204, |
| 99 | + content=StatusSchema(code=404, msg='Not found').dict()) |
| 100 | + db.delete(resource) |
| 101 | + db.commit() |
| 102 | + response.status_code = 410 |
| 103 | + return {} |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +@router.get('/owners', |
| 108 | + name='list_owners', |
| 109 | + summary='List all known owners', |
| 110 | + description='Returns all currently known owners and their metadata', |
| 111 | + response_model=List[OwnerSchema]) |
| 112 | +async def get_all(db: Session = Depends(get_db)): |
| 113 | + return db.query(Owner).all() |
| 114 | + |
| 115 | + |
| 116 | +@router.get('/owners/{identity}', |
| 117 | + name='get_owner', |
| 118 | + summary='Get a single owner', |
| 119 | + description='Return a single owner identified by its owner id', |
| 120 | + response_model=Union[OwnerSchema, StatusSchema]) |
| 121 | +async def get(identity: int, db: Session = Depends(get_db)): |
| 122 | + owner = db.query(Owner).get(identity) |
| 123 | + if not owner: |
| 124 | + return JSONResponse(status_code=404, |
| 125 | + content=StatusSchema(code=404, msg='Not found').dict()) |
| 126 | + return owner |
| 127 | + |
| 128 | + |
| 129 | +@router.post('/owners', |
| 130 | + name='create_owner', |
| 131 | + summary='Create a owner', |
| 132 | + description='Create a owner', |
| 133 | + response_model=Union[OwnerSchema, StatusSchema]) |
| 134 | +async def create(data: OwnerInputSchema, db: Session = Depends(get_db)): |
| 135 | + owner = Owner(name=data.name, client_id='TODO') |
| 136 | + db.add(owner) |
| 137 | + db.commit() |
| 138 | + return owner |
| 139 | + |
| 140 | + |
| 141 | +@router.put('/owners/{identity}', |
| 142 | + name='modify_owner', |
| 143 | + summary='Modify a owner', |
| 144 | + description='Modify a owner by its owner id', |
| 145 | + response_model=Union[OwnerSchema, StatusSchema]) |
| 146 | +async def modify(identity: int, data: ResourceInputSchema, db: Session = Depends(get_db)): |
| 147 | + owner = db.query(Owner).get(identity) |
| 148 | + if not owner: |
| 149 | + return JSONResponse(status_code=404, |
| 150 | + content=StatusSchema(code=404, msg='Not found').dict()) |
| 151 | + owner.name = data.name |
| 152 | + db.add(owner) |
| 153 | + db.commit() |
| 154 | + return owner |
| 155 | + |
| 156 | + |
| 157 | +@router.delete('/owners/{identity}', |
| 158 | + name='remove_owner', |
| 159 | + summary='Remove a owner', |
| 160 | + description='Remove a owner by its owner id', |
| 161 | + status_code=204, |
| 162 | + responses={ |
| 163 | + status.HTTP_204_NO_CONTENT: {'description': 'The owner was removed'}, |
| 164 | + status.HTTP_410_GONE: {'description': 'The owner was already gone'} |
| 165 | + }) |
| 166 | +async def remove(identity: int, response: Response, db: Session = Depends(get_db)): |
| 167 | + owner = db.query(Owner).get(identity) |
| 168 | + if not owner: |
| 169 | + response.status_code = status.HTTP_410_GONE |
| 170 | + return |
| 171 | + db.delete(owner) |
| 172 | + db.commit() |
| 173 | + response.status_code = status.HTTP_204_NO_CONTENT |
0 commit comments