Update backend/post_router.py
Browse files- backend/post_router.py +119 -119
backend/post_router.py
CHANGED
|
@@ -1,119 +1,119 @@
|
|
| 1 |
-
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
-
from sqlalchemy.orm import Session
|
| 3 |
-
from models import Post, User
|
| 4 |
-
from db import get_db
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
from typing import Optional
|
| 7 |
-
from fastapi.security import OAuth2PasswordBearer
|
| 8 |
-
import jwt
|
| 9 |
-
from auth.routes import get_current_user
|
| 10 |
-
|
| 11 |
-
class PostCreate(BaseModel):
|
| 12 |
-
title: str
|
| 13 |
-
content: Optional[str] = None
|
| 14 |
-
|
| 15 |
-
class PostUpdate(BaseModel):
|
| 16 |
-
post_id: int
|
| 17 |
-
title: str
|
| 18 |
-
content: Optional[str] = None
|
| 19 |
-
|
| 20 |
-
SECRET_KEY = "52a6206f34a1c479da043cdeee17fd859a35e54978a6733a6a7ebadcbd11f0ca"
|
| 21 |
-
ALGORITHM = "HS256"
|
| 22 |
-
|
| 23 |
-
router = APIRouter()
|
| 24 |
-
|
| 25 |
-
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login") # ๋ก๊ทธ์ธ ๊ฒฝ๋ก ์ค์
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def get_auth_user(
|
| 29 |
-
token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)
|
| 30 |
-
) -> User:
|
| 31 |
-
try:
|
| 32 |
-
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 33 |
-
email: str = payload.get("sub")
|
| 34 |
-
if email is None:
|
| 35 |
-
raise HTTPException(status_code=401, detail="Token payload invalid")
|
| 36 |
-
except jwt.PyJWTError:
|
| 37 |
-
raise HTTPException(status_code=401, detail="Invalid token")
|
| 38 |
-
|
| 39 |
-
user = db.query(User).filter(User.user_email == email).first()
|
| 40 |
-
if user is None:
|
| 41 |
-
raise HTTPException(status_code=401, detail="User not found")
|
| 42 |
-
|
| 43 |
-
return user
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
@router.post("/posts")
|
| 47 |
-
def create_post(
|
| 48 |
-
post: PostCreate,
|
| 49 |
-
db: Session = Depends(get_db),
|
| 50 |
-
current_user: User = Depends(get_auth_user),
|
| 51 |
-
):
|
| 52 |
-
new_post = Post(
|
| 53 |
-
title=post.title, content=post.content, user_id=current_user.user_id
|
| 54 |
-
)
|
| 55 |
-
db.add(new_post)
|
| 56 |
-
db.commit()
|
| 57 |
-
db.refresh(new_post)
|
| 58 |
-
return {"message": "Post created", "post_id": new_post.post_id}
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
@router.get("/list")
|
| 62 |
-
def get_my_posts(
|
| 63 |
-
current_user: User = Depends(get_auth_user), db: Session = Depends(get_db)
|
| 64 |
-
):
|
| 65 |
-
posts = db.query(Post).filter(Post.user_id == current_user.user_id).all()
|
| 66 |
-
return [{"post_id": post.post_id, "title": post.title} for post in posts]
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
@router.get("/{post_id}")
|
| 70 |
-
def read_user_post(
|
| 71 |
-
post_id: int,
|
| 72 |
-
current_user: User = Depends(get_auth_user),
|
| 73 |
-
db: Session = Depends(get_db),
|
| 74 |
-
):
|
| 75 |
-
post = (
|
| 76 |
-
db.query(Post)
|
| 77 |
-
.filter(Post.post_id == post_id, Post.user_id == current_user.user_id)
|
| 78 |
-
.first()
|
| 79 |
-
)
|
| 80 |
-
if post is None:
|
| 81 |
-
raise HTTPException(status_code=404, detail="ํฌ์คํธ๊ฐ ์๊ฑฐ๋ ๊ถํ์ด ์์ต๋๋ค.")
|
| 82 |
-
return {"post_id": post.post_id, "title": post.title, "content": post.content}
|
| 83 |
-
|
| 84 |
-
@router.put("/save")
|
| 85 |
-
def update_post(
|
| 86 |
-
post: PostUpdate,
|
| 87 |
-
db: Session = Depends(get_db),
|
| 88 |
-
current_user: User = Depends(get_auth_user),
|
| 89 |
-
):
|
| 90 |
-
db_post = (
|
| 91 |
-
db.query(Post)
|
| 92 |
-
.filter(Post.post_id == post.post_id, Post.user_id == current_user.user_id)
|
| 93 |
-
.first()
|
| 94 |
-
)
|
| 95 |
-
if not db_post:
|
| 96 |
-
raise HTTPException(status_code=404, detail="Post not found or access denied")
|
| 97 |
-
|
| 98 |
-
db_post.title = post.title
|
| 99 |
-
db_post.content = post.content
|
| 100 |
-
db.commit()
|
| 101 |
-
db.refresh(db_post)
|
| 102 |
-
|
| 103 |
-
return {"message": "Post updated successfully"}
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
@router.delete("/{post_id}")
|
| 107 |
-
def delete_post(
|
| 108 |
-
post_id: int, db: Session = Depends(get_db), user=Depends(get_auth_user)
|
| 109 |
-
):
|
| 110 |
-
post = (
|
| 111 |
-
db.query(Post)
|
| 112 |
-
.filter(Post.post_id == post_id, Post.user_id == user.user_id)
|
| 113 |
-
.first()
|
| 114 |
-
)
|
| 115 |
-
if not post:
|
| 116 |
-
raise HTTPException(status_code=404, detail="Post not found")
|
| 117 |
-
db.delete(post)
|
| 118 |
-
db.commit()
|
| 119 |
-
return {"message": "Post deleted"}
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from backend.models import Post, User
|
| 4 |
+
from backend.db import get_db
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from typing import Optional
|
| 7 |
+
from fastapi.security import OAuth2PasswordBearer
|
| 8 |
+
import jwt
|
| 9 |
+
from backend.auth.routes import get_current_user
|
| 10 |
+
|
| 11 |
+
class PostCreate(BaseModel):
|
| 12 |
+
title: str
|
| 13 |
+
content: Optional[str] = None
|
| 14 |
+
|
| 15 |
+
class PostUpdate(BaseModel):
|
| 16 |
+
post_id: int
|
| 17 |
+
title: str
|
| 18 |
+
content: Optional[str] = None
|
| 19 |
+
|
| 20 |
+
SECRET_KEY = "52a6206f34a1c479da043cdeee17fd859a35e54978a6733a6a7ebadcbd11f0ca"
|
| 21 |
+
ALGORITHM = "HS256"
|
| 22 |
+
|
| 23 |
+
router = APIRouter()
|
| 24 |
+
|
| 25 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login") # ๋ก๊ทธ์ธ ๊ฒฝ๋ก ์ค์
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def get_auth_user(
|
| 29 |
+
token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)
|
| 30 |
+
) -> User:
|
| 31 |
+
try:
|
| 32 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 33 |
+
email: str = payload.get("sub")
|
| 34 |
+
if email is None:
|
| 35 |
+
raise HTTPException(status_code=401, detail="Token payload invalid")
|
| 36 |
+
except jwt.PyJWTError:
|
| 37 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
| 38 |
+
|
| 39 |
+
user = db.query(User).filter(User.user_email == email).first()
|
| 40 |
+
if user is None:
|
| 41 |
+
raise HTTPException(status_code=401, detail="User not found")
|
| 42 |
+
|
| 43 |
+
return user
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@router.post("/posts")
|
| 47 |
+
def create_post(
|
| 48 |
+
post: PostCreate,
|
| 49 |
+
db: Session = Depends(get_db),
|
| 50 |
+
current_user: User = Depends(get_auth_user),
|
| 51 |
+
):
|
| 52 |
+
new_post = Post(
|
| 53 |
+
title=post.title, content=post.content, user_id=current_user.user_id
|
| 54 |
+
)
|
| 55 |
+
db.add(new_post)
|
| 56 |
+
db.commit()
|
| 57 |
+
db.refresh(new_post)
|
| 58 |
+
return {"message": "Post created", "post_id": new_post.post_id}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@router.get("/list")
|
| 62 |
+
def get_my_posts(
|
| 63 |
+
current_user: User = Depends(get_auth_user), db: Session = Depends(get_db)
|
| 64 |
+
):
|
| 65 |
+
posts = db.query(Post).filter(Post.user_id == current_user.user_id).all()
|
| 66 |
+
return [{"post_id": post.post_id, "title": post.title} for post in posts]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
@router.get("/{post_id}")
|
| 70 |
+
def read_user_post(
|
| 71 |
+
post_id: int,
|
| 72 |
+
current_user: User = Depends(get_auth_user),
|
| 73 |
+
db: Session = Depends(get_db),
|
| 74 |
+
):
|
| 75 |
+
post = (
|
| 76 |
+
db.query(Post)
|
| 77 |
+
.filter(Post.post_id == post_id, Post.user_id == current_user.user_id)
|
| 78 |
+
.first()
|
| 79 |
+
)
|
| 80 |
+
if post is None:
|
| 81 |
+
raise HTTPException(status_code=404, detail="ํฌ์คํธ๊ฐ ์๊ฑฐ๋ ๊ถํ์ด ์์ต๋๋ค.")
|
| 82 |
+
return {"post_id": post.post_id, "title": post.title, "content": post.content}
|
| 83 |
+
|
| 84 |
+
@router.put("/save")
|
| 85 |
+
def update_post(
|
| 86 |
+
post: PostUpdate,
|
| 87 |
+
db: Session = Depends(get_db),
|
| 88 |
+
current_user: User = Depends(get_auth_user),
|
| 89 |
+
):
|
| 90 |
+
db_post = (
|
| 91 |
+
db.query(Post)
|
| 92 |
+
.filter(Post.post_id == post.post_id, Post.user_id == current_user.user_id)
|
| 93 |
+
.first()
|
| 94 |
+
)
|
| 95 |
+
if not db_post:
|
| 96 |
+
raise HTTPException(status_code=404, detail="Post not found or access denied")
|
| 97 |
+
|
| 98 |
+
db_post.title = post.title
|
| 99 |
+
db_post.content = post.content
|
| 100 |
+
db.commit()
|
| 101 |
+
db.refresh(db_post)
|
| 102 |
+
|
| 103 |
+
return {"message": "Post updated successfully"}
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@router.delete("/{post_id}")
|
| 107 |
+
def delete_post(
|
| 108 |
+
post_id: int, db: Session = Depends(get_db), user=Depends(get_auth_user)
|
| 109 |
+
):
|
| 110 |
+
post = (
|
| 111 |
+
db.query(Post)
|
| 112 |
+
.filter(Post.post_id == post_id, Post.user_id == user.user_id)
|
| 113 |
+
.first()
|
| 114 |
+
)
|
| 115 |
+
if not post:
|
| 116 |
+
raise HTTPException(status_code=404, detail="Post not found")
|
| 117 |
+
db.delete(post)
|
| 118 |
+
db.commit()
|
| 119 |
+
return {"message": "Post deleted"}
|