-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (42 loc) · 1.46 KB
/
main.py
File metadata and controls
50 lines (42 loc) · 1.46 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
from fastapi import FastAPI, HTTPException
from datetime import datetime
from models import Note, NoteResponse
from database import notes_db
app = FastAPI(title="Notes API")
@app.post("/notes", response_model=NoteResponse)
def create_note(note: Note):
new_note = {
"id": len(notes_db) + 1,
"title": note.title,
"content": note.content,
"created_at": datetime.now()
}
notes_db.append(new_note)
return new_note
@app.get("/notes", response_model=list[NoteResponse])
def get_notes():
return notes_db
@app.get("/")
def root():
return {"message": "Notes API is running 🚀"}
@app.get("/notes/{note_id}", response_model=NoteResponse)
def get_note(note_id: int):
for note in notes_db:
if note["id"] == note_id:
return note
raise HTTPException(status_code=404, detail="Note not found")
@app.put("/notes/{note_id}", response_model=NoteResponse)
def update_note(note_id: int, updated: Note):
for note in notes_db:
if note["id"] == note_id:
note["title"] = updated.title
note["content"] = updated.content
return note
raise HTTPException(status_code=404, detail="Note not found")
@app.delete("/notes/{note_id}")
def delete_note(note_id: int):
for i, note in enumerate(notes_db):
if note["id"] == note_id:
notes_db.pop(i)
return {"message": "Note deleted"}
raise HTTPException(status_code=404, detail="Note not found")