Spaces:
Sleeping
Sleeping
Create app/api/v1/endpoints/suggestions.py
Browse files
app/api/v1/endpoints/suggestions.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
from app.db.session import SessionLocal
|
| 6 |
+
from app.schemas.suggestion import SuggestionCreate, Suggestion
|
| 7 |
+
from app.services.suggestion_service import (
|
| 8 |
+
create_suggestion,
|
| 9 |
+
get_suggestions_by_user,
|
| 10 |
+
vote_on_suggestion,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
router = APIRouter()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_db():
|
| 17 |
+
db = SessionLocal()
|
| 18 |
+
try:
|
| 19 |
+
yield db
|
| 20 |
+
finally:
|
| 21 |
+
db.close()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@router.post("", response_model=Suggestion, status_code=201)
|
| 25 |
+
def submit_suggestion(data: SuggestionCreate, db: Session = Depends(get_db)):
|
| 26 |
+
return create_suggestion(db, data)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@router.get("/user/{user_id}", response_model=List[Suggestion])
|
| 30 |
+
def user_suggestions(user_id: str, db: Session = Depends(get_db)):
|
| 31 |
+
return get_suggestions_by_user(db, user_id)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@router.post("/{suggestion_id}/vote")
|
| 35 |
+
def vote(
|
| 36 |
+
suggestion_id: int,
|
| 37 |
+
isUpvote: bool,
|
| 38 |
+
db: Session = Depends(get_db),
|
| 39 |
+
):
|
| 40 |
+
suggestion = vote_on_suggestion(db, suggestion_id, is_upvote=isUpvote)
|
| 41 |
+
if not suggestion:
|
| 42 |
+
raise HTTPException(status_code=404, detail="Suggestion not found")
|
| 43 |
+
return {"success": True}
|