-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (51 loc) · 1.47 KB
/
main.py
File metadata and controls
67 lines (51 loc) · 1.47 KB
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
app = FastAPI()
notes = []
note_id = 1
class Note(BaseModel):
title: str
content: str
@app.post("/notes")
def add(note: Note):
global note_id
singleNote = {
"id":note_id,
"title":note.title,
"content":note.content
}
notes.append(singleNote)
note_id+=1
return singleNote
@app.get("/allNotes")
def allNotes():
return notes
@app.delete("/deleteNote/{note_id}")
def deleteNote(note_id: int):
for item in range(len(notes)):
if notes[item]["id"]== note_id:
notes.pop(item)
return {"message":"Note has been removed"}
raise HTTPException (
status_code=404, detail="Note ID not found"
)
class noteUpdate(BaseModel):
title: Optional[str] = None
content: Optional[str] = None
@app.patch("/updateNote")
def updateNotes(note: noteUpdate, note_id: int):
if note.title is None and note.content is None:
raise HTTPException (
status_code=400, detail="Nothing to update"
)
for item in notes:
if item["id"] == note_id:
if note.title is not None:
item["title"] = note.title
if note.content is not None:
item["content"] = note.content
return item
raise HTTPException (
status_code=404, detail="Note ID not found"
)