크레딧 기능 추가
This commit is contained in:
1 parent
7202376123
commit
6492d23bc1
8 files changed
+103
-1
No files matched your search
@@ -23,6 +23,7 @@ logger = logging.getLogger(__name__)
|
||||
from app.user.dependencies import get_current_user
|
||||
from app.user.models import RefreshToken, User
|
||||
from app.user.schemas.user_schema import (
|
||||
CreditResponse,
|
||||
KakaoCodeRequest,
|
||||
KakaoLoginResponse,
|
||||
LoginResponse,
|
||||
@@ -353,6 +354,22 @@ async def get_me(
|
||||
return UserResponse.model_validate(current_user)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/me/credits",
|
||||
response_model=CreditResponse,
|
||||
summary="잔여 크레딧 조회",
|
||||
description="현재 로그인한 사용자의 잔여 영상 생성 크레딧을 반환합니다.",
|
||||
responses={
|
||||
200: {"description": "조회 성공"},
|
||||
401: {"description": "인증 실패 (토큰 없음/만료)"},
|
||||
},
|
||||
)
|
||||
async def get_my_credits(
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> CreditResponse:
|
||||
return CreditResponse(credits=current_user.credits)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 테스트용 엔드포인트 (DEBUG 모드에서만 main.py에서 라우터가 등록됨)
|
||||
# =============================================================================
|
||||
|
||||
@@ -216,6 +216,14 @@ class User(Base):
|
||||
comment="마지막 로그인 일시",
|
||||
)
|
||||
|
||||
credits: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
nullable=False,
|
||||
default=3,
|
||||
server_default="3",
|
||||
comment="잔여 영상 생성 크레딧",
|
||||
)
|
||||
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
nullable=False,
|
||||
|
||||
@@ -160,6 +160,22 @@ class LoginResponse(BaseModel):
|
||||
}
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# 크레딧 스키마
|
||||
# =============================================================================
|
||||
class CreditResponse(BaseModel):
|
||||
"""잔여 크레딧 응답"""
|
||||
|
||||
credits: int = Field(..., description="영상 생성 크레딧")
|
||||
|
||||
model_config = {
|
||||
"json_schema_extra": {
|
||||
"example": {
|
||||
"credits": 3
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 내부 사용 스키마 (카카오 API 응답 파싱)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
from sqlalchemy import update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.user.models import User
|
||||
|
||||
|
||||
async def consume_credit(user_uuid, session: AsyncSession) -> bool:
|
||||
"""atomic UPDATE로 1크레딧 차감.
|
||||
|
||||
WHERE credits > 0 조건으로 음수 차감 방지 + PostgreSQL 행 락.
|
||||
차감 성공 여부 반환.
|
||||
"""
|
||||
result = await session.execute(
|
||||
update(User)
|
||||
.where(User.user_uuid == user_uuid, User.credits > 0)
|
||||
.values(credits=User.credits - 1)
|
||||
)
|
||||
return result.rowcount > 0
|
||||
Reference in new issue
Block a user