-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
166 lines (138 loc) · 5.99 KB
/
main.py
File metadata and controls
166 lines (138 loc) · 5.99 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from fastapi import HTTPException
from fastapi import status, Response
from typing import Optional
from fastapi import FastAPI
from fastapi import Body
from pydantic import BaseModel
import psycopg2
from psycopg2.extras import RealDictCursor
import time
app = FastAPI()
# Define a Pydantic model for the request body
class Note(BaseModel):
note: str
category: str
bookmarked: bool = False # Optional field with a default value
saved: Optional[bool] = None # Optional field without a default value
while True:
try:
conn = psycopg2.connect(host='localhost', database='fastapidb', user='postgres', password='Himanshu@2019', cursor_factory=RealDictCursor)
cursor = conn.cursor()
print("Database connection was successful!")
break;
except Exception as error:
print("Connecting to database failed")
print("Error:", error)
time.sleep(2)
# Simple GET endpoint to check if the API is running
@app.get("/")
async def read_root():
return {"data": "Welecome to FastAPI! This is a simple API built with FastAPI."}
# Simple GET endpoint to fetch notes
@app.get("/notes")
async def read_notes():
return {"notes": ["Note 1", "Note 2", "Note 3"]}
# Method 1: Using path parameters
@app.post("/create_note")
async def create_note(note: str, category: str):
print(f"Creating note: {note} with category: {category}")
return {"message": f"Note '{note}' with category '{category}' created successfully!"}
# Method 2: Using request body
@app.post("/create_notes")
async def create_note(payload: dict = Body(...)):
print(payload)
return {"message": f"Note with params '{payload}' created successfully!"}
# Method 3: Using Pydantic model
@app.post("/create_notes_model")
async def create_note(note: Note):
print(note)
print(note.dict())
return {"message": f"Note '{note.note}' with category '{note.category}' '{note.bookmarked}' {note.saved} created successfully!"}
# Method 4: Using only one parameter in the body
@app.post("/create_category")
async def create_category(category: str):
print(f"Creating category: {category}")
return {"message": f"Category '{category}' created successfully!"}
# CRUD Operations
# In-memory storage for notes
notes_db = [{"id": 1, "note": "Sample Note", "category": "General", "bookmarked": False, "saved": None},
{"id": 2, "note": "Another Note", "category": "Work", "bookmarked": True, "saved": True}]
# Create a new note
#satus code 201 means resource created successfully & status code will be shown in the postman response
@app.post("/notes/create", status_code=status.HTTP_201_CREATED)
async def create_note_db(note: Note):
new_id = len(notes_db) + 1
new_note = note.dict()
new_note["id"] = new_id
notes_db.append(new_note)
print(notes_db)
return {"message": "Note created successfully", "note": new_note}
# Read all notes
@app.get("/notes/all")
async def get_all_notes():
return {"notes": notes_db}
# Read a specific note by ID
@app.get("/notes/{id}")
async def get_note_by_id(id: int):
for note in notes_db:
if note["id"] == id:
return {"note": note}
raise HTTPException(status_code=404, detail= f"note with note_Id = {id} not found")
# return {"error": "Note not found"}
# Update a note by ID
@app.put("/notes/update/{id}")
async def update_note(id: int, updated_note: Note):
for index, note in enumerate(notes_db):
if note["id"] == id:
notes_db[index] = updated_note.dict()
notes_db[index]["id"] = id
return {"message": "Note updated successfully", "note": notes_db[index]}
return {"error": "Note not found"}
# Delete a note by ID
@app.delete("/notes/delete/{id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_note(id: int):
for index, note in enumerate(notes_db):
if note["id"] == id:
deleted_note = notes_db.pop(index)
return Response(status_code=status.HTTP_204_NO_CONTENT)
#return {"message": "Note deleted successfully", "note": deleted_note}
raise HTTPException(status_code=404, detail= f"note with note_Id = {id} not found")
# return {"error": "Note not found"}
#DB related operations Using PostgresSQL
# Read all notes from the database
@app.get("/fetch_notes/all")
async def get_all_notes():
cursor.execute("SELECT * FROM notes")
notes = cursor.fetchall()
return {"notes": notes}
@app.post("/add_notes", status_code=status.HTTP_201_CREATED)
async def add_note_db(note: Note):
cursor.execute("INSERT INTO notes (id, title, description, is_bookmarked) VALUES (%s, %s, %s, %s) RETURNING *",
('101', note.note, note.category, note.bookmarked))
new_note = cursor.fetchone()
conn.commit()
return {"message": "Note added successfully", "note": new_note}
@app.get("/fetch_notes_db/{id}")
async def get_note_by_id(id: int):
cursor.execute("SELECT * FROM notes WHERE ID = %s", (str(id)))
note = cursor.fetchone()
if not note:
raise HTTPException(status_code=404, detail= f"note with note_Id = {id} not found")
return {"note": note}
@app.delete("/delete_notes_db/{id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_note_db(id: int):
cursor.execute("DELETE FROM notes WHERE ID = %s RETURNING *", (str(id),))
deleted_note = cursor.fetchone()
conn.commit()
if not deleted_note:
raise HTTPException(status_code=404, detail= f"note with note_Id = {id} not found")
return Response(status_code=status.HTTP_204_NO_CONTENT)
@app.put("/update_notes_db/{id}")
async def update_note_db(id: int, updated_note: Note):
cursor.execute("UPDATE notes SET title = %s, description = %s, is_bookmarked = %s WHERE ID = %s RETURNING *",
(updated_note.note, updated_note.category, updated_note.bookmarked, str(id)))
note = cursor.fetchone()
conn.commit()
if not note:
raise HTTPException(status_code=404, detail= f"note with note_Id = {id} not found")
return {"message": "Note updated successfully", "note": note}