116 lines
4.3 KiB
Python
116 lines
4.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query, Header, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from sqlalchemy.orm import Session
|
|
from typing import Optional, List
|
|
#
|
|
from database import get_db
|
|
from app.core.auth import get_current_user
|
|
from app.modules.users.models import Users # Modelo base de usuario
|
|
from app.modules.feed.models import Feed
|
|
from app.modules.feed.schema import FeedUpdate, FeedCreate, FeedResponse, messageResponse
|
|
from app.modules.feed.service import FeedService
|
|
|
|
router = APIRouter(prefix="/feed", tags=["feed"])
|
|
security = HTTPBearer()
|
|
|
|
#================ Create ====================
|
|
@router.post("/create", response_model=FeedResponse, status_code=status.HTTP_201_CREATED)
|
|
async def create_feed(
|
|
data: FeedCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: Users = Depends(get_current_user)
|
|
):
|
|
"""Create a feed - Requires authentication"""
|
|
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="User not found - invalid token")
|
|
|
|
if current_user.tipo_usuario not in ["root", "admin"]:
|
|
raise HTTPException(status_code=403, detail="You don't have permission to create feed")
|
|
|
|
try:
|
|
result = FeedService.create_feed(db=db, data=data)
|
|
return result
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
#================ Get ====================
|
|
@router.get("/", response_model=List[FeedResponse])
|
|
def get_feeds(
|
|
db: Session = Depends(get_db),
|
|
current_user: Users = Depends(get_current_user),
|
|
skip: int = Query(0, ge=0, description="Number of records to skip"),
|
|
limit: int = Query(100, ge=1, le=1000, description="Max number of records to return")
|
|
):
|
|
"""Get list with pagination - requires authentication"""
|
|
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="Authentication required")
|
|
|
|
return FeedService.get_feeds(db=db, skip=skip, limit=limit)
|
|
|
|
#================ Get by ID ====================
|
|
@router.get("/{{{entity_name}}_id}", response_model=FeedResponse)
|
|
def get_feed_by_id(
|
|
feed_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: Users = Depends(get_current_user)
|
|
):
|
|
"""Get feed by ID - requires authentication"""
|
|
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="Authentication required")
|
|
|
|
try:
|
|
result = FeedService.get_feed(db=db, feed_id=feed_id, current_user=current_user)
|
|
return result
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
|
|
#================ Update ====================
|
|
@router.patch("/update/{{{entity_name}}_id}", response_model=FeedResponse)
|
|
async def update_feed(
|
|
feed_id: int,
|
|
data: FeedUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: Users = Depends(get_current_user)
|
|
):
|
|
"""Update existing feed - requires authentication"""
|
|
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="Authentication required")
|
|
|
|
if current_user.tipo_usuario not in ["root", "admin"]:
|
|
raise HTTPException(status_code=403, detail="You don't have permission to update feed")
|
|
|
|
try:
|
|
result = FeedService.update_feed(
|
|
db=db,
|
|
feed_id=feed_id,
|
|
data=data,
|
|
current_user=current_user
|
|
)
|
|
return result
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
|
|
#================ Delete ====================
|
|
@router.delete("/delete/{{{entity_name}}_id}", response_model= messageResponse, status_code=status.HTTP_200_OK)
|
|
async def delete_feed(
|
|
feed_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: Users = Depends(get_current_user)
|
|
):
|
|
"""Delete feed by ID - requires authentication"""
|
|
|
|
if not current_user:
|
|
raise HTTPException(status_code=401, detail="Authentication required")
|
|
|
|
if current_user.tipo_usuario not in ["root", "admin"]:
|
|
raise HTTPException(status_code=403, detail="You don't have permission to delete feed")
|
|
|
|
try:
|
|
result = FeedService.delete_feed(db=db, feed_id=feed_id, current_user=current_user)
|
|
return {"message": "feed deleted successfully"}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e)) |