-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (94 loc) · 3.09 KB
/
main.py
File metadata and controls
110 lines (94 loc) · 3.09 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
from fastapi import FastAPI, HTTPException, status
from pydantic import BaseModel
from typing import Optional
import psycopg2
from psycopg2.extras import RealDictCursor
import time
from datetime import datetime
app = FastAPI()
class Post(BaseModel):
id: Optional[int] = None
title: str
content: str
created_at: Optional[datetime] = None
# Database connection area
while True:
try:
conn = psycopg2.connect(
host='localhost',
database='fastapi',
user='anubhav',
password='anubhav',
cursor_factory=RealDictCursor
)
cursor = conn.cursor()
print("Database connection was successful")
break
except psycopg2.Error as e:
print(f"Unable to connect to the database. Error: {e}")
print("Retrying...")
time.sleep(2)
# Function to get all posts from the database
def get_all_posts():
cursor.execute("SELECT * FROM posts")
return cursor.fetchall()
# Function to add a new post to the database
def add_post(new_post):
cursor.execute(
"INSERT INTO posts (title, content, created_at) VALUES (%s, %s, %s) RETURNING id",
(new_post.title, new_post.content, new_post.created_at)
)
new_post.id = cursor.fetchone()["id"]
conn.commit()
#update posts function
def update_posts(id, new_post: Post):
cursor.execute("UPDATE posts SET title = %s, content = %s WHERE id = %s", (new_post.title, new_post.content, id))
conn.commit()
# Function to find a post by ID
def find_post(id):
cursor.execute("SELECT * FROM posts WHERE id = %s", (id,))
return cursor.fetchone()
# Function to delete a post by ID
def delete_post(id):
cursor.execute("DELETE FROM posts WHERE id = %s", (id,))
conn.commit()
# Function to get the latest post
def get_latest_post():
cursor.execute("SELECT * FROM posts ORDER BY id DESC LIMIT 1")
return cursor.fetchone()
# Get all posts
@app.get("/posts")
async def posts():
return {"data": get_all_posts()}
# Add a new post
@app.post("/addposts", status_code=status.HTTP_201_CREATED)
async def postsimages(new_post: Post):
add_post(new_post)
return {"data": new_post}
# Get the latest post
@app.get("/posts/latest")
async def latest_post():
return {"data": get_latest_post()}
# Get a specific post by ID
@app.get("/posts/{id}")
async def post(id: int):
post = find_post(id)
if not post:
raise HTTPException(status_code=404, detail=f"Post with id {id} not found")
return {"data": post}
# Delete a specific post by ID
@app.delete("/posts/{id}")
async def delete_specific_post(id: int):
post = find_post(id)
if not post:
raise HTTPException(status_code=404, detail=f"Post with id {id} not found")
delete_post(id)
return {"data": f"Deleted post with id: {id}"}
# Update a specific post by ID
@app.put("/posts/{id}")
async def update_specific_post(id: int, new_post: Post):
post = find_post(id)
if not post:
raise HTTPException(status_code=404, detail=f"Post with id {id} not found")
update_posts(id, new_post)
return {"data": f"Updated post with id: {id}"}