Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions app/posts/repository.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
23 changes: 5 additions & 18 deletions app/posts/service.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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,
Expand Down
Empty file removed app/slack/__init__.py
Empty file.
8 changes: 0 additions & 8 deletions app/slack/config.py

This file was deleted.

37 changes: 0 additions & 37 deletions app/slack/service.py

This file was deleted.

Loading