Files
o2o-weather/backend/crud/reaction_crud.py
T
minaChoi a9ddbaab3c feat: 피드를 시간대별 카드로 다시 짜고 하늘 배경·내 캐릭터 추가, 다른 시안 정리
- 피드: 그날 카드 12장을 03:00에 한 번에 만들고, 시간대(아침·낮·저녁·밤, 저녁은 일몰 2시간 전부터)마다 순서를 바꾸고 조건이 맞는 카드만 켠다. 새 카드 우산·특보, 내일 미리보기, 한 문장·시(예시 seed). 흐린 날 노을은 한 줄 카드.
- 캐시를 feed:{구}:{시간대}로 나누고 시간대 경계·일몰·예보 갱신 때 만료해 새 카드가 늦게 보이던 문제를 고친다.
- 히어로는 오늘 1위 각만 보여 준다.
- 각 점수·노을 점수를 7일 지나면 지운다.
- 화면 전체 하늘 배경(시간대별 색, 느린 일렁임, 밤 별)과 화면 끝까지 이어지는 위·아래 바.
- 상단바 로그인 버튼, 내 정보 창(캐릭터 6종·배경색 6종·오늘 기분 8종·닉네임·로그아웃), PATCH /v1/auth/me, 댓글·상단바에 캐릭터·배경색·기분 표시. users.character·color·mood 칸 추가.
- 카드 슬라이드와 가로 줄을 Embla 카로셀로 바꿔 PC에서도 마우스로 끌어 넘기고 화살표로 넘긴다.
- 좋아요·저장을 받아 둔 피드 데이터에도 반영해 다른 탭에 다녀와도 유지되게 고치고, 좋아요 때 하트가 터지는 애니메이션을 넣는다.
- 보내기에서 게시물을 인스타 스토리 크기(1080×1920) 이미지로 공유하거나 내려받는다 (modern-screenshot).
- 창이 열려 있는 동안 뒤 페이지 스크롤을 잠근다.
- 시안 B 확정: 시안 A 아이콘(public/a)과 쓰지 않는 전역 스타일·글꼴 삭제.
- 문서: 데이터 흐름 지도(backend/docs/data-flow-map.html), 확정 시안(docs/시안B_확정.html), 피드 기획 v2, 호출량·DB 크기 추정, data-pipeline 갱신. 보고 폴더와 예전 시안 html 삭제.
2026-10-02 15:47:20 +09:00

82 lines
5.2 KiB
Python

import uuid
from typing import Optional
from sqlalchemy import delete, select, update
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.ext.asyncio import AsyncSession
from common.database.model.models import Comment, CommentLike, Post, PostLike, PostSave, User
class ReactionCRUD:
async def toggle_post_like(self, s: AsyncSession, post_id: int, user_id: uuid.UUID, on: bool) -> int:
"""좋아요를 켜거나 끄고, 실제로 바뀌었을 때만 수를 고친다. 바뀐 뒤 좋아요 수를 돌려준다."""
if on:
res = await s.execute(insert(PostLike).values(post_id=post_id, user_id=user_id).on_conflict_do_nothing().returning(PostLike.post_id))
else:
res = await s.execute(delete(PostLike).where(PostLike.post_id == post_id, PostLike.user_id == user_id).returning(PostLike.post_id))
if res.first() is not None:
await s.execute(update(Post).where(Post.post_id == post_id).values(like_count=Post.like_count + (1 if on else -1)))
return await s.scalar(select(Post.like_count).where(Post.post_id == post_id))
async def toggle_post_save(self, s: AsyncSession, post_id: int, user_id: uuid.UUID, on: bool) -> None:
if on:
await s.execute(insert(PostSave).values(post_id=post_id, user_id=user_id).on_conflict_do_nothing())
else:
await s.execute(delete(PostSave).where(PostSave.post_id == post_id, PostSave.user_id == user_id))
async def counts(self, s: AsyncSession, post_ids: list[int]) -> dict[int, tuple[int, int]]:
rows = await s.execute(select(Post.post_id, Post.like_count, Post.comment_count).where(Post.post_id.in_(post_ids)))
return {pid: (likes, comments) for pid, likes, comments in rows.all()}
async def my_marks(self, s: AsyncSession, user_id: uuid.UUID, post_ids: list[int]) -> tuple[set[int], set[int]]:
liked = set((await s.scalars(select(PostLike.post_id).where(PostLike.user_id == user_id, PostLike.post_id.in_(post_ids)))).all())
saved = set((await s.scalars(select(PostSave.post_id).where(PostSave.user_id == user_id, PostSave.post_id.in_(post_ids)))).all())
return liked, saved
async def saved_posts(self, s: AsyncSession, user_id: uuid.UUID, cursor: Optional[int], limit: int) -> list[tuple[Post, PostSave]]:
stmt = select(Post, PostSave).join(PostSave, PostSave.post_id == Post.post_id).where(PostSave.user_id == user_id, Post.deleted.is_(False))
if cursor is not None:
stmt = stmt.where(Post.post_id < cursor)
return list((await s.execute(stmt.order_by(Post.post_id.desc()).limit(limit))).all())
async def comments(self, s: AsyncSession, post_id: int, cursor: Optional[int], limit: int) -> list[tuple[Comment, str, str, str, Optional[str]]]:
stmt = select(Comment, User.nickname, User.character, User.color, User.mood).join(User, User.user_id == Comment.user_id).where(Comment.post_id == post_id, Comment.deleted.is_(False))
if cursor is not None:
stmt = stmt.where(Comment.comment_id > cursor)
return list((await s.execute(stmt.order_by(Comment.comment_id).limit(limit))).all())
async def liked_comments(self, s: AsyncSession, user_id: uuid.UUID, comment_ids: list[int]) -> set[int]:
stmt = select(CommentLike.comment_id).where(CommentLike.user_id == user_id, CommentLike.comment_id.in_(comment_ids))
return set((await s.scalars(stmt)).all())
async def add_comment(self, s: AsyncSession, post_id: int, user_id: uuid.UUID, body: str) -> Comment:
c = Comment(post_id=post_id, user_id=user_id, body=body)
s.add(c)
await s.execute(update(Post).where(Post.post_id == post_id).values(comment_count=Post.comment_count + 1))
await s.flush()
return c
async def delete_comment(self, s: AsyncSession, comment_id: int, user_id: uuid.UUID) -> bool:
res = await s.execute(
update(Comment).where(Comment.comment_id == comment_id, Comment.user_id == user_id, Comment.deleted.is_(False))
.values(deleted=True).returning(Comment.post_id)
)
post_id = res.scalar()
if post_id is None:
return False
await s.execute(update(Post).where(Post.post_id == post_id).values(comment_count=Post.comment_count - 1))
return True
async def toggle_comment_like(self, s: AsyncSession, comment_id: int, user_id: uuid.UUID, on: bool) -> Optional[int]:
exists = await s.scalar(select(Comment.comment_id).where(Comment.comment_id == comment_id, Comment.deleted.is_(False)))
if exists is None:
return None
if on:
res = await s.execute(insert(CommentLike).values(comment_id=comment_id, user_id=user_id).on_conflict_do_nothing().returning(CommentLike.comment_id))
else:
res = await s.execute(delete(CommentLike).where(CommentLike.comment_id == comment_id, CommentLike.user_id == user_id).returning(CommentLike.comment_id))
if res.first() is not None:
await s.execute(update(Comment).where(Comment.comment_id == comment_id).values(like_count=Comment.like_count + (1 if on else -1)))
return await s.scalar(select(Comment.like_count).where(Comment.comment_id == comment_id))