diff --git a/app/posts/repository.py b/app/posts/repository.py index 9dad756..fc8704e 100644 --- a/app/posts/repository.py +++ b/app/posts/repository.py @@ -1,5 +1,3 @@ -from dataclasses import dataclass - from sqlalchemy import String, and_, case, delete, exists, func, or_, select, update from sqlalchemy.dialects.postgresql import insert from sqlalchemy.orm import joinedload @@ -14,12 +12,6 @@ from .models import Post, PostImage, post_grad_2025_table -@dataclass -class SaveResult: - post: Post - new_univ_major: str | None = None # 새로 생성된 univ_major 이름 (없으면 None) - - @dependency class PostRepository: session: SessionDep @@ -134,22 +126,19 @@ async def feed( result = await self.session.scalars(stmt) return list(result.unique().all()) - async def save(self, *, post: Post, univ_major: str | None = None) -> SaveResult: + async def save(self, *, post: Post, univ_major: str | None = None) -> Post: self.session.add(post) await self.session.flush() - new_univ_major: str | None = None - if univ_major: grad_2025 = await self.session.scalar( select(Grad2025).where(Grad2025.name == univ_major) ) - if grad_2025 is None: + if not grad_2025: grad_2025 = Grad2025(name=univ_major) self.session.add(grad_2025) await self.session.flush() - new_univ_major = univ_major await self.session.execute( insert(post_grad_2025_table) @@ -163,7 +152,7 @@ async def save(self, *, post: Post, univ_major: str | None = None) -> SaveResult ) await self.session.flush() - return SaveResult(post=post, new_univ_major=new_univ_major) + return post async def update( self, @@ -175,9 +164,8 @@ async def update( tags: list[str], images: list[PostImage], univ_major: str | None = None, - ) -> SaveResult: + ) -> Post: post_id = post.id - new_univ_major: str | None = None # SQL UPDATE로 Post 필드 업데이트 await self.session.execute( @@ -220,11 +208,10 @@ async def update( select(Grad2025).where(Grad2025.name == univ_major) ) - if grad_2025 is None: + if not grad_2025: grad_2025 = Grad2025(name=univ_major) self.session.add(grad_2025) await self.session.flush() - new_univ_major = univ_major await self.session.execute( insert(post_grad_2025_table).values( @@ -234,8 +221,7 @@ async def update( # 세션 캐시 무효화 후 다시 조회 self.session.expire_all() - updated_post = await self.find_by_id(post_id=post_id) - return SaveResult(post=updated_post, new_univ_major=new_univ_major) # type: ignore + return await self.find_by_id(post_id=post_id) # type: ignore async def count_by_user_handle(self, *, user_handle: str) -> int: result = await self.session.scalar( diff --git a/app/posts/service.py b/app/posts/service.py index 3539db4..6d5385f 100644 --- a/app/posts/service.py +++ b/app/posts/service.py @@ -1,14 +1,13 @@ import asyncio import uuid -from fastapi import BackgroundTasks, HTTPException +from fastapi import HTTPException from app.category.enums import Category from app.comments.repository import CommentRepository from app.common.schemas import FeedCursor, Page, PageWithCount from app.database.deps import SessionDep from app.like.repository import PostLikeRepository -from app.slack.service import SlackService from app.storage.deps import S3ClientDep from app.storage.service import get_image_metadata from app.users.models import User @@ -26,8 +25,6 @@ class PostService: post_repository: PostRepository post_like_repository: PostLikeRepository comment_repository: CommentRepository - slack_service: SlackService - background_tasks: BackgroundTasks async def get_post_by_id(self, *, post_id: int, current_user: User) -> PostRead: post = await self.post_repository.find_by_id(post_id=post_id) @@ -166,17 +163,12 @@ async def create_post( ) ) - result = await self.post_repository.save( + created_post = await self.post_repository.save( post=created_post, univ_major=post.univ_major ) - if result.new_univ_major: - self.background_tasks.add_task( - self.slack_service.notify_new_univ_major, result.new_univ_major - ) - return PostRead.from_post( - result.post, + created_post, like_count=0, comment_count=0, current_user_id=current_user.id, @@ -213,7 +205,7 @@ async def update_post( ) ] - result = await self.post_repository.update( + updated_post = await self.post_repository.update( post=saved_post, title=post.title, description=post.description, @@ -223,11 +215,6 @@ async def update_post( univ_major=post.univ_major, ) - if result.new_univ_major: - self.background_tasks.add_task( - self.slack_service.notify_new_univ_major, result.new_univ_major - ) - [like_count, comment_count, is_liked, univ_major] = await asyncio.gather( self.post_like_repository.count_by_id(post_id=post_id), self.comment_repository.count_by_post_id(post_id=post_id), @@ -239,7 +226,7 @@ async def update_post( ) return PostRead.from_post( - result.post, + updated_post, like_count=like_count, comment_count=comment_count, is_liked=is_liked, diff --git a/app/slack/__init__.py b/app/slack/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/app/slack/config.py b/app/slack/config.py deleted file mode 100644 index 8a92053..0000000 --- a/app/slack/config.py +++ /dev/null @@ -1,8 +0,0 @@ -from app.config import BaseConfig - - -class SlackConfig(BaseConfig): - SLACK_WEBHOOK_URL: str | None = None - - -slack_settings = SlackConfig.create() diff --git a/app/slack/service.py b/app/slack/service.py deleted file mode 100644 index fd4da81..0000000 --- a/app/slack/service.py +++ /dev/null @@ -1,37 +0,0 @@ -import httpx - -from app.core.config import Environment, core_settings -from app.utils.dependency import dependency - -from .config import slack_settings - - -@dependency -class SlackService: - async def _send_message(self, text: str) -> None: - """슬랙으로 메시지를 전송합니다.""" - webhook_url = slack_settings.SLACK_WEBHOOK_URL - - if not webhook_url: - return - - # 로컬 환경에서는 알림 비활성화 - if core_settings.ENVIRONMENT == Environment.LOCAL: - return - - try: - async with httpx.AsyncClient() as client: - await client.post( - webhook_url, - json={"text": text}, - timeout=5.0, - ) - except Exception: - # 슬랙 알림 실패가 메인 로직에 영향 주지 않도록 무시 - pass - - async def notify_new_univ_major(self, univ_major: str) -> None: - """새로운 학교/전공이 등록되었을 때 슬랙 알림을 보냅니다.""" - phase = core_settings.ENVIRONMENT.value.upper() - message = f"[{phase}] 🎓 새로운 학교/전공 검수 요청: *{univ_major}*" - await self._send_message(message)