-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson3query.py
More file actions
132 lines (89 loc) · 3.97 KB
/
Copy pathLesson3query.py
File metadata and controls
132 lines (89 loc) · 3.97 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
from fastapi import FastAPI, Response, status, HTTPException, Depends
from fastapi.params import Body
from pydantic import BaseModel
from typing import Optional
from random import randrange
import psycopg2
from psycopg2.extras import RealDictCursor
import time
from sqlalchemy.orm import Session
from . import models
from .database import engine, get_db
models.Base.metadata.create_all(bind=engine)
app = FastAPI() # creates your API application instance.
class Post(BaseModel):
title: str
content: str
published: bool = True
# rating: Optional[int] = None
while True:
try:
conn = psycopg2.connect(host ='localhost', database = 'fastapi', user = 'postgres', password = 'postgres123', cursor_factory= RealDictCursor)
cursor = conn.cursor()
print("Succeful database connection")
break
except Exception as error:
print("Failed to connect to database")
print("Error was: ", error)
time.sleep(5)
class UpdatePost(BaseModel):
title: str
content: str
published: bool = True
rating: Optional[int] = None
my_posts = [{"title": "title of post1", "content": "content of post 1", "id": 1},
{"title": "favorite foods", "content": "I like Tofu", "id": 2},
{"classes": "javascript", "lessons":"Lesson 1 to 5", "id": 5}]
@app.get("/sqlalchemy")
def test_posts(db: Session = Depends (get_db)):
post = db.query (models.Post).all()
return{"data":post}
# //////////////////////////////////////////////
@app.get("/posts")
def get_posts(db: Session = Depends (get_db)):
posts = db.query (models.Post).all()
print(posts)
return{"data": posts }
# ///////////////CREATE APOST USING ORM///////////////
@app.post("/posts", status_code=status.HTTP_201_CREATED)
def createpost(newpost:Post, db:Session = Depends(get_db)):
# cursor.execute ("""INSERT INTO nposts (title, content, published) values(%s,%s,%s) RETURNING *""",
# (newpost.title, newpost.content, newpost.published))
# created_post = models.Post(title=newpost.title, content=newpost.content, published=newpost.published)
# Instead of this long appraoach, we use the ** model.
# print(newpost.dict())
created_post = models.Post(**newpost.dict())
db.add(created_post)
db.commit()
db.refresh(created_post)
return{"data":created_post}
# ///////////////////////////////////
@app.get("/posts/{id}", status_code=status.HTTP_200_OK)
def get_post(id:int, db:Session = Depends(get_db)):
test_post = db.query(models.Post).filter(models.Post.id == id).first()
print(test_post)
if not test_post:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"post with id {id} was not found")
return{"post_detail": test_post}
# DELETE POST
@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id:int, db:Session = Depends(get_db)):
post = db.query(models.Post).filter(models.Post.id == id)
if post.first()== None:
raise HTTPException (status_code=status.HTTP_404_NOT_FOUND, detail=f"post with id: {id} does not exist")
post.delete (synchronize_session=False)
db.commit()
return Response (status_code=status.HTTP_204_NO_CONTENT)
# ///////////////////////////////////////////
# UPDATE POST
@app.put("/posts/{id}", status_code=status.HTTP_200_OK)
def update_post(id:int, post:Post,db:Session = Depends(get_db) ):
post_query = db.query(models.Post).filter(models.Post.id == id)
updated_post = post_query.first()
if updated_post == None:
raise HTTPException (status_code=status.HTTP_404_NOT_FOUND, detail=f"post with id: {id} does not exist")
post_query.update ({'title': 'California', 'content': 'this is my updated content by Jossy_Julius'}, synchronize_session= False)
db.commit()
# return {"data": 'updated_post'}
return{"data": post_query.all()}
# SQLalchemy does not know how to talk to database. It needs a driver, which is psycopg2