- 각: 출석 10, 오늘 타로 10, 날씨 사주 10, 좋아요·댓글 5씩(하루 10), 동네 이야기 글 20, 저녁 체크인 10, 공유 10, 7일 연속 50. 같은 날 같은 대상은 한 번 (point_log) - 레벨: 모은 총량으로 이슬 0 → 햇살 30 → 산들바람 100 → 무지개 200 → 노을 400 → 은하수 700 → 별똥별 1000. 지금 있는 기능은 잠그지 않음 - API: GET /v1/level/me, POST /v1/level/attend, POST /v1/level/share - 화면: 헤더 아바타 레벨 배지, 내 정보 레벨 카드, 내 레벨 창(오늘 받을 각·레벨별로 열리는 것), 레벨이 오르면 축하 창 - 로그인한 사람만 쌓임 (기기 기록은 조작할 수 있어 받지 않음)
31 lines
1.8 KiB
Python
31 lines
1.8 KiB
Python
import uuid
|
|
from datetime import date
|
|
|
|
from sqlalchemy import func, select, update
|
|
from sqlalchemy.dialects.postgresql import insert
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from common.database.model.models import PointLog, User
|
|
|
|
|
|
class LevelCRUD:
|
|
async def earned(self, s: AsyncSession, user_id: uuid.UUID, day: date, action: str) -> int:
|
|
return await s.scalar(select(func.coalesce(func.sum(PointLog.points), 0)).where(PointLog.user_id == user_id, PointLog.day == day, PointLog.action == action))
|
|
|
|
async def add(self, s: AsyncSession, user_id: uuid.UUID, day: date, action: str, ref: str, points: int) -> int:
|
|
"""같은 날 같은 일·대상이면 넣지 않는다. 넣었으면 총량에 더하고 새 총량을 돌려준다 (안 넣었으면 0)"""
|
|
row = await s.scalar(insert(PointLog).values(user_id=user_id, day=day, action=action, ref=ref, points=points).on_conflict_do_nothing().returning(PointLog.point_id))
|
|
if row is None:
|
|
return 0
|
|
return await s.scalar(update(User).where(User.user_id == user_id).values(points=User.points + points).returning(User.points))
|
|
|
|
async def today(self, s: AsyncSession, user_id: uuid.UUID, day: date) -> dict[str, int]:
|
|
rows = (await s.execute(select(PointLog.action, func.sum(PointLog.points)).where(PointLog.user_id == user_id, PointLog.day == day).group_by(PointLog.action))).all()
|
|
return {a: int(p) for a, p in rows}
|
|
|
|
async def attend_days(self, s: AsyncSession, user_id: uuid.UUID, since: date) -> list[date]:
|
|
return list((await s.scalars(select(PointLog.day).where(PointLog.user_id == user_id, PointLog.action == "attend", PointLog.day >= since))).all())
|
|
|
|
async def points(self, s: AsyncSession, user_id: uuid.UUID) -> int:
|
|
return await s.scalar(select(User.points).where(User.user_id == user_id)) or 0
|