-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] 피드 저장 상태 변경 api 개발 #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c655f87
75d0106
a0d55eb
97e9b2a
2b1b30a
84073a9
8fe3999
559f40b
a1cf215
cc24e32
179ebb2
280eeaf
153b85a
e7d18a3
28a745f
9944eb0
8bab3a5
4f0b485
7e8ecbf
b2489cd
bd081c3
b81658f
e6f9e21
e8779bc
79219b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package konkuk.thip.feed.adapter.in.web.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
| import konkuk.thip.feed.application.port.in.dto.FeedIsSavedCommand; | ||
|
|
||
| public record FeedIsSavedRequest( | ||
| @NotNull(message = "type은 필수입니다.") | ||
| boolean type | ||
| ) { | ||
| public static FeedIsSavedCommand toCommand(Long userId, Long feedId, Boolean type) { | ||
| return new FeedIsSavedCommand(userId, feedId, type); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package konkuk.thip.feed.adapter.in.web.response; | ||
|
|
||
| import konkuk.thip.feed.application.port.in.dto.FeedIsSavedResult; | ||
|
|
||
| public record FeedIsSavedResponse( | ||
| Long feedId, | ||
| boolean isSaved | ||
| ) { | ||
| public static FeedIsSavedResponse of(FeedIsSavedResult feedIsSavedResult) { | ||
| return new FeedIsSavedResponse(feedIsSavedResult.feedId(), feedIsSavedResult.isSaved()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package konkuk.thip.feed.application.port.in; | ||
|
|
||
| import konkuk.thip.feed.application.port.in.dto.FeedIsSavedCommand; | ||
| import konkuk.thip.feed.application.port.in.dto.FeedIsSavedResult; | ||
|
|
||
| public interface FeedSavedUseCase { | ||
| FeedIsSavedResult changeSavedFeed(FeedIsSavedCommand feedIsSavedCommand); | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package konkuk.thip.feed.application.port.in.dto; | ||
|
|
||
| public record FeedIsSavedCommand( | ||
|
|
||
| Long userId, | ||
|
|
||
| Long feedId, | ||
|
|
||
| Boolean isSaved | ||
| ) | ||
| { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package konkuk.thip.feed.application.port.in.dto; | ||
|
|
||
| public record FeedIsSavedResult( | ||
| Long feedId, | ||
| boolean isSaved | ||
| ) | ||
| { | ||
| public static FeedIsSavedResult of(Long feedId, boolean isSaved) { | ||
| return new FeedIsSavedResult(feedId, isSaved); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| package konkuk.thip.feed.application.port.out; | ||
|
|
||
|
|
||
| import konkuk.thip.common.exception.EntityNotFoundException; | ||
| import konkuk.thip.feed.domain.Feed; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static konkuk.thip.common.exception.code.ErrorCode.FEED_NOT_FOUND; | ||
|
|
||
| public interface FeedCommandPort { | ||
| Long save(Feed feed); | ||
| Long update(Feed feed); | ||
| Feed findById(Long id); | ||
| Optional<Feed> findById(Long id); | ||
| default Feed getByIdOrThrow(Long id) { | ||
| return findById(id) | ||
| .orElseThrow(() -> new EntityNotFoundException(FEED_NOT_FOUND)); | ||
| } | ||
|
Comment on lines
+14
to
+18
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package konkuk.thip.feed.application.service; | ||
|
|
||
| import jakarta.transaction.Transactional; | ||
| import konkuk.thip.feed.application.port.in.FeedSavedUseCase; | ||
| import konkuk.thip.feed.application.port.in.dto.FeedIsSavedCommand; | ||
| import konkuk.thip.feed.application.port.in.dto.FeedIsSavedResult; | ||
| import konkuk.thip.feed.application.port.out.FeedCommandPort; | ||
| import konkuk.thip.feed.domain.Feed; | ||
| import konkuk.thip.feed.domain.SavedFeeds; | ||
| import konkuk.thip.saved.application.port.out.SavedCommandPort; | ||
| import konkuk.thip.saved.application.port.out.SavedQueryPort; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class FeedSavedService implements FeedSavedUseCase { | ||
|
|
||
| private final FeedCommandPort feedCommandPort; | ||
| private final SavedCommandPort savedCommandPort; | ||
| private final SavedQueryPort savedQueryPort; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public FeedIsSavedResult changeSavedFeed(FeedIsSavedCommand feedIsSavedCommand) { | ||
|
|
||
| // 1. 피드 검증 및 조회 | ||
| Feed feed = feedCommandPort.getByIdOrThrow(feedIsSavedCommand.feedId()); | ||
|
|
||
| // 2. 유저가 저장한 피드 목록 조회 | ||
| SavedFeeds savedFeeds = savedQueryPort.findSavedFeedsByUserId(feedIsSavedCommand.userId()); | ||
|
Comment on lines
+30
to
+31
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 Port 에서 바로 일급컬렉션을 반환하도록 하셨군요! |
||
|
|
||
| if (feedIsSavedCommand.isSaved()) { | ||
| // 저장 요청 시 이미 저장되어 있으면 예외 발생 | ||
| savedFeeds.validateNotAlreadySaved(feed); | ||
| savedCommandPort.saveFeed(feedIsSavedCommand.userId(), feed.getId()); | ||
| } else { | ||
| // 삭제 요청 시 저장되어 있지 않으면 예외 발생 | ||
| savedFeeds.validateCanDelete(feed); | ||
| savedCommandPort.deleteFeed(feedIsSavedCommand.userId(), feed.getId()); | ||
| } | ||
|
|
||
| return FeedIsSavedResult.of(feed.getId(), feedIsSavedCommand.isSaved()); | ||
| } | ||
|
Comment on lines
+23
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM 👍🏻 👍🏻 👍🏻 코드 깔쌈하네여 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package konkuk.thip.feed.domain; | ||
|
|
||
| import konkuk.thip.common.exception.InvalidStateException; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static konkuk.thip.common.exception.code.ErrorCode.*; | ||
|
|
||
| @Getter | ||
| public class SavedFeeds { | ||
|
|
||
| private final Set<Feed> feeds; | ||
|
|
||
| public SavedFeeds(List<Feed> feeds) { | ||
| Set<Feed> feedSet = new HashSet<>(feeds); | ||
| if (feedSet.size() != feeds.size()) { | ||
| throw new InvalidStateException(DUPLICATED_FEEDS_IN_COLLECTION); | ||
| } | ||
| this.feeds = Collections.unmodifiableSet(feedSet); | ||
| } | ||
|
|
||
| // 중복 저장 검증 | ||
| public void validateNotAlreadySaved(Feed feed) { | ||
| if (feeds.contains(feed)) { | ||
| throw new InvalidStateException(FEED_ALREADY_SAVED); | ||
| } | ||
| } | ||
|
|
||
| // 삭제 가능 여부 검증 | ||
| public void validateCanDelete(Feed feed) { | ||
| if (!feeds.contains(feed)) { | ||
| throw new InvalidStateException(FEED_NOT_SAVED_CANNOT_DELETE); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
Comment on lines
+13
to
+40
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.