-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (79 loc) · 3 KB
/
Copy pathmain.py
File metadata and controls
85 lines (79 loc) · 3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
class NoteCreate(BaseModel):
title: str # required
content: str # required
is_important: bool = False
class NoteUpdate(BaseModel):
title: Optional[str] = None
content: Optional[str] = None
is_important: Optional[bool] = None
notes_ls = []
counter = 1
app = FastAPI()
@app.get("/")
def landing_msg():
return{"msg": "Welcome to Notes Application. "}
@app.get("/notes")
def get_all_notes(keyword: str = None):
match_notes = []
if keyword is not None:
for note in notes_ls:
if keyword.lower() in note["title"].lower():
match_notes.append(note)
if match_notes:
return match_notes
else:
raise HTTPException(status_code=404, detail="Note does not exist. ")
else:
return{"TotalNotes": len(notes_ls), "Notes": notes_ls}
@app.get("/notes/important")
def get_note_by_imp():
imp_notes = []
for note in notes_ls:
if note["is_important"] == True:
imp_notes.append(note)
if imp_notes:
return imp_notes
else:
raise HTTPException(status_code=404, detail="Note not found")
@app.get("/notes/{note_id}")
def get_note_by_id(note_id: int):
for note in notes_ls:
if note["id"] == note_id:
return{"title": note['title'], "Details": note['content']}
raise HTTPException(status_code=404, detail="Note not found")
@app.delete("/notes/{note_id}")
def del_note_by_id(note_id: int):
for note in notes_ls:
if note["id"] == note_id:
notes_ls.remove(note)
return{"msg": "Note Deleted Successfully. "}
raise HTTPException(status_code=404, detail="Note not Exist. ")
@app.post("/notes")
def create_notes(note: NoteCreate):
global counter
title = note.title.strip()
content = note.content.strip()
if not title or not content:
raise HTTPException(status_code=422, detail="Title and content cannot be empty")
new_note = {"id": counter, "title": title, "content": content, "is_important": note.is_important}
notes_ls.append(new_note)
counter += 1
return {"message": "Note created!", "note": new_note}
@app.put("/notes/{note_id}")
def update_note(note_id: int, note: NoteUpdate):
for reqnote in notes_ls:
if reqnote["id"] == note_id:
if note.title is not None or note.content is not None or note.is_important is not None:
if note.title is not None:
reqnote["title"] = note.title
if note.content is not None:
reqnote["content"] = note.content
if note.is_important is not None:
reqnote["is_important"] = note.is_important
return{"msg": "Note is Updated Successfully. "}
else:
raise HTTPException(status_code=422, detail="Details are missing. ")
raise HTTPException(status_code=404, detail="Note does not exist. ")