forked from menard-codes/NotesAppBackend-FastAPI-React
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
107 lines (80 loc) · 2.47 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm.exc import UnmappedInstanceError
from schemas import NoteInput
from model.database import DBSession
from model import models
app = FastAPI()
origins = [
"http://localhost:5173",
"https://notes-app-frontend-fast-api-react.vercel.app"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
@app.get("/notes")
def read_notes():
db = DBSession()
try:
notes = db.query(models.Note).all()
finally:
db.close()
return notes
@app.post("/note")
def add_note(note: NoteInput):
db = DBSession()
try:
if len(note.title) == 0 and len(note.note_body) == 0:
raise HTTPException(
status_code=400, detail={
"status": "Error 400 - Bad Request",
"msg": "Both 'title' and 'note_body' are empty. These are optional attributes but at least one must be provided."
})
new_note = models.Note(
title=note.title, note_body=note.note_body
)
db.add(new_note)
db.commit()
db.refresh(new_note)
finally:
db.close()
return new_note
@app.put("/note/{note_id}")
def update_note(note_id: int, updated_note: NoteInput):
if len(updated_note.title) == 0 and len(updated_note.note_body) == 0:
raise HTTPException(status_code=400, detail={
"status": "Error 400 - Bad Request",
"msg": "The note's `title` and `note_body` can't be both empty"
})
db = DBSession()
try:
note = db.query(models.Note).filter(models.Note.id == note_id).first()
note.title = updated_note.title
note.note_body = updated_note.note_body
db.commit()
db.refresh(note)
finally:
db.close()
return note
@app.delete("/note/{note_id}")
def delete_note(note_id: int):
db = DBSession()
try:
note = db.query(models.Note).filter(models.Note.id == note_id).first()
db.delete(note)
db.commit()
except UnmappedInstanceError:
raise HTTPException(status_code=400, detail={
"status": "Error 400 - Bad Request",
"msg": f"Note with `id`: `{note_id}` doesn't exist."
})
finally:
db.close()
return {
"status": "200",
"msg": "Note deleted successfully"
}