88
99
1010router = APIRouter (
11- prefix = "/posts" , # prefix for all routes
11+ prefix = "/posts" ,
1212 tags = ['Posts' ]
1313)
1414
15-
16- # @router.get("/", response_model=List[schemas.Post])
1715@router .get ("/" , response_model = List [schemas .PostOut ])
1816def get_posts (db : Session = Depends (get_db ), current_user : int = Depends (oauth2 .get_current_user ), limit : int = 10 , skip : int = 0 , search : Optional [str ] = "" ):
19- # results = db.query(models.Post, func.count(models.Vote.post_id).label("votes")).join(
20- # models.Vote, models.Vote.post_id == models.Post.id, isouter=True).group_by(models.Post.id)
21-
22- # cursor.execute("""SELECT * FROM posts """)
23- # posts = cursor.fetchall()
24-
25- # posts = db.execute(
26- # 'select posts.*, COUNT(votes.post_id) as votes from posts LEFT JOIN votes ON posts.id=votes.post_id group by posts.id')
27- # results = []
28- # for post in posts:
29- # results.append(dict(post))
30- # print(results)
31- # posts = db.query(models.Post).filter(
32- # models.Post.title.contains(search)).limit(limit).offset(skip).all()
3317
3418 posts = db .query (models .Post , func .count (models .Vote .post_id ).label ("votes" )).join (
3519 models .Vote , models .Vote .post_id == models .Post .id , isouter = True ).group_by (models .Post .id ).filter (models .Post .title .contains (search )).limit (limit ).offset (skip ).all ()
@@ -38,11 +22,6 @@ def get_posts(db: Session = Depends(get_db), current_user: int = Depends(oauth2.
3822
3923@router .post ("/" , status_code = status .HTTP_201_CREATED , response_model = schemas .Post )
4024def create_posts (post : schemas .PostCreate , db : Session = Depends (get_db ), current_user : int = Depends (oauth2 .get_current_user )):
41- # cursor.execute("""INSERT INTO posts (title, content, published) VALUES (%s, %s, %s) RETURNING * """,
42- # (post.title, post.content, post.published))
43- # new_post = cursor.fetchone()
44-
45- # conn.commit()
4625
4726 new_post = models .Post (owner_id = current_user .id , ** post .dict ())
4827 db .add (new_post )
@@ -54,9 +33,6 @@ def create_posts(post: schemas.PostCreate, db: Session = Depends(get_db), curren
5433
5534@router .get ("/{id}" , response_model = schemas .PostOut )
5635def get_post (id : int , db : Session = Depends (get_db ), current_user : int = Depends (oauth2 .get_current_user )):
57- # cursor.execute("""SELECT * from posts WHERE id = %s """, (str(id),))
58- # post = cursor.fetchone()
59- # post = db.query(models.Post).filter(models.Post.id == id).first()
6036
6137 post = db .query (models .Post , func .count (models .Vote .post_id ).label ("votes" )).join (
6238 models .Vote , models .Vote .post_id == models .Post .id , isouter = True ).group_by (models .Post .id ).filter (models .Post .id == id ).first ()
@@ -66,16 +42,11 @@ def get_post(id: int, db: Session = Depends(get_db), current_user: int = Depends
6642 detail = f"post with id: { id } was not found" )
6743
6844 return post
69- # return { 'status': "success", "data": post }
7045
7146
7247@router .delete ("/{id}" , status_code = status .HTTP_204_NO_CONTENT )
7348def delete_post (id : int , db : Session = Depends (get_db ), current_user : int = Depends (oauth2 .get_current_user )):
7449
75- # cursor.execute(
76- # """DELETE FROM posts WHERE id = %s returning *""", (str(id),))
77- # deleted_post = cursor.fetchone()
78- # conn.commit()
7950 post_query = db .query (models .Post ).filter (models .Post .id == id )
8051
8152 post = post_query .first ()
@@ -97,12 +68,6 @@ def delete_post(id: int, db: Session = Depends(get_db), current_user: int = Depe
9768@router .put ("/{id}" , response_model = schemas .Post )
9869def update_post (id : int , updated_post : schemas .PostCreate , db : Session = Depends (get_db ), current_user : int = Depends (oauth2 .get_current_user )):
9970
100- # cursor.execute("""UPDATE posts SET title = %s, content = %s, published = %s WHERE id = %s RETURNING *""",
101- # (post.title, post.content, post.published, str(id)))
102-
103- # updated_post = cursor.fetchone()
104- # conn.commit()
105-
10671 post_query = db .query (models .Post ).filter (models .Post .id == id )
10772
10873 post = post_query .first ()
@@ -120,3 +85,8 @@ def update_post(id: int, updated_post: schemas.PostCreate, db: Session = Depends
12085 db .commit ()
12186
12287 return post_query .first ()
88+
89+
90+ @router .get ('/create_reply' )
91+ def create_reply ():
92+ return { "status" :"pending" , "message" : "Functionality Under Construction." }
0 commit comments