-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathnotes.py
60 lines (42 loc) · 1.47 KB
/
notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from typing import List
from fastapi import APIRouter, HTTPException, Path
from app.api import crud
from app.api.models import NoteDB, NoteSchema
router = APIRouter()
@router.post('/', response_model=NoteDB, status_code=201)
async def create_note(payload: NoteSchema):
note_id = await crud.post(payload)
response_object = {
'id': note_id,
'title': payload.title,
'description': payload.description,
}
return response_object
@router.get('/{id}/', response_model=NoteDB)
async def read_note(id: int = Path(..., gt=0),):
note = await crud.get(id)
if not note:
raise HTTPException(status_code=404, detail='Note not found')
return note
@router.get('/', response_model=List[NoteDB])
async def read_all_notes():
return await crud.get_all()
@router.put('/{id}/', response_model=NoteDB)
async def update_note(payload: NoteSchema, id: int = Path(..., gt=0),):
note = await crud.get(id)
if not note:
raise HTTPException(status_code=404, detail='Note not found')
note_id = await crud.put(id, payload)
response_object = {
'id': note_id,
'title': payload.title,
'description': payload.description,
}
return response_object
@router.delete('/{id}/', response_model=NoteDB)
async def delete_note(id: int = Path(..., gt=0)):
note = await crud.get(id)
if not note:
raise HTTPException(status_code=404, detail='Note not found')
await crud.delete(id)
return note