136 lines
4.3 KiB
Python
136 lines
4.3 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from app.db.session import get_db
|
|
from app.api.deps import get_current_active_user
|
|
from app.models.user import User
|
|
from app.schemas.reward import Reward, RewardCreate, RewardUpdate
|
|
from app.crud.base import CRUDBase
|
|
from app.models.reward import Reward as RewardModel
|
|
|
|
router = APIRouter()
|
|
reward_crud = CRUDBase(RewardModel)
|
|
|
|
|
|
@router.get("", response_model=List[Reward])
|
|
async def get_rewards(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(100, ge=1, le=100),
|
|
is_claimed: bool = Query(None),
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Получить список наград пользователя"""
|
|
filters = {"user_id": current_user.id}
|
|
if is_claimed is not None:
|
|
filters["is_claimed"] = is_claimed
|
|
|
|
rewards = await reward_crud.get_multi(db, skip=skip, limit=limit, filters=filters)
|
|
return rewards
|
|
|
|
|
|
@router.get("/{reward_id}", response_model=Reward)
|
|
async def get_reward(
|
|
reward_id: str,
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Получить награду по ID"""
|
|
reward = await reward_crud.get(db, reward_id)
|
|
if not reward:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Reward not found"
|
|
)
|
|
if reward.user_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
return reward
|
|
|
|
|
|
@router.post("", response_model=Reward, status_code=status.HTTP_201_CREATED)
|
|
async def create_reward(
|
|
reward_in: RewardCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Создать новую награду"""
|
|
reward_data = reward_in.model_dump()
|
|
reward_data["user_id"] = current_user.id
|
|
reward = await reward_crud.create(db, reward_data)
|
|
return reward
|
|
|
|
|
|
@router.put("/{reward_id}", response_model=Reward)
|
|
async def update_reward(
|
|
reward_id: str,
|
|
reward_in: RewardUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Обновить награду"""
|
|
reward = await reward_crud.get(db, reward_id)
|
|
if not reward:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Reward not found"
|
|
)
|
|
if reward.user_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
|
|
update_data = reward_in.model_dump(exclude_unset=True)
|
|
reward = await reward_crud.update(db, reward, update_data)
|
|
return reward
|
|
|
|
|
|
@router.delete("/{reward_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_reward(
|
|
reward_id: str,
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Удалить награду"""
|
|
reward = await reward_crud.get(db, reward_id)
|
|
if not reward:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Reward not found"
|
|
)
|
|
if reward.user_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
|
|
await reward_crud.delete(db, reward_id)
|
|
return None
|
|
|
|
|
|
@router.patch("/{reward_id}/claim", response_model=Reward)
|
|
async def claim_reward(
|
|
reward_id: str,
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Получить награду (отметить как полученную)"""
|
|
reward = await reward_crud.get(db, reward_id)
|
|
if not reward:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Reward not found"
|
|
)
|
|
if reward.user_id != current_user.id:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough permissions"
|
|
)
|
|
|
|
reward = await reward_crud.update(db, reward, {"is_claimed": True})
|
|
return reward
|
|
|