-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] '응원 등록 API'에 응원 태그 추가 #159
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
4a5bbf5
48086b8
c6df6e4
7e727c1
fb898f6
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 |
|---|---|---|
| @@ -1,8 +1,21 @@ | ||
| package eatda.controller.cheer; | ||
|
|
||
| import eatda.domain.cheer.CheerTagName; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public record CheerRegisterRequest( | ||
| String storeKakaoId, | ||
| String storeName, | ||
| String description | ||
| String description, | ||
| List<CheerTagName> tags | ||
| ) { | ||
|
|
||
| @Override | ||
| public List<CheerTagName> tags() { // TODO : 클라이언트 태그 구현 완료 시 삭제 | ||
| if (tags == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
| return tags; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,32 @@ | ||
| package eatda.controller.cheer; | ||
|
|
||
| import eatda.domain.cheer.Cheer; | ||
| import eatda.domain.cheer.CheerTag; | ||
| import eatda.domain.cheer.CheerTagName; | ||
| import eatda.domain.store.Store; | ||
| import java.util.List; | ||
|
|
||
| public record CheerResponse( | ||
| long storeId, | ||
| long cheerId, | ||
| String imageUrl, | ||
| String cheerDescription | ||
| String cheerDescription, | ||
| List<CheerTagName> tags | ||
| ) { | ||
|
|
||
| public CheerResponse(Cheer cheer, String imageUrl, Store store) { | ||
| public CheerResponse(Cheer cheer, List<CheerTag> cheerTags, Store store, String imageUrl) { | ||
| this( | ||
| store.getId(), | ||
| cheer.getId(), | ||
| imageUrl, | ||
| cheer.getDescription() | ||
| cheer.getDescription(), | ||
| toTagNames(cheerTags) | ||
| ); | ||
| } | ||
|
|
||
| private static List<CheerTagName> toTagNames(List<CheerTag> cheerTags) { | ||
| return cheerTags.stream() | ||
| .map(CheerTag::getName) | ||
| .toList(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package eatda.repository.cheer; | ||
|
|
||
| import eatda.domain.cheer.CheerTag; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface CheerTagRepository extends JpaRepository<CheerTag, Long> { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,15 @@ | |
| import eatda.controller.cheer.CheersResponse; | ||
| import eatda.domain.ImageKey; | ||
| import eatda.domain.cheer.Cheer; | ||
| import eatda.domain.cheer.CheerTag; | ||
| import eatda.domain.cheer.CheerTagNames; | ||
| import eatda.domain.member.Member; | ||
| import eatda.domain.store.Store; | ||
| import eatda.domain.store.StoreSearchResult; | ||
| import eatda.exception.BusinessErrorCode; | ||
| import eatda.exception.BusinessException; | ||
| import eatda.repository.cheer.CheerRepository; | ||
| import eatda.repository.cheer.CheerTagRepository; | ||
| import eatda.repository.member.MemberRepository; | ||
| import eatda.repository.store.StoreRepository; | ||
| import eatda.storage.image.ImageStorage; | ||
|
|
@@ -32,20 +35,23 @@ public class CheerService { | |
| private final MemberRepository memberRepository; | ||
| private final StoreRepository storeRepository; | ||
| private final CheerRepository cheerRepository; | ||
| private final CheerTagRepository cheerTagRepository; | ||
| private final ImageStorage imageStorage; | ||
|
|
||
| @Transactional | ||
| public CheerResponse registerCheer(CheerRegisterRequest request, | ||
| StoreSearchResult result, | ||
| ImageKey imageKey, | ||
| long memberId) { | ||
| CheerTagNames cheerTagNames = new CheerTagNames(request.tags()); | ||
| Member member = memberRepository.getById(memberId); | ||
| validateRegisterCheer(member, request.storeKakaoId()); | ||
|
|
||
| Store store = storeRepository.findByKakaoId(result.kakaoId()) | ||
| .orElseGet(() -> storeRepository.save(result.toStore())); // TODO 상점 조회/저장 동시성 이슈 해결 | ||
| Cheer cheer = cheerRepository.save(new Cheer(member, store, request.description(), imageKey)); | ||
| return new CheerResponse(cheer, imageStorage.getPreSignedUrl(imageKey), store); | ||
| List<CheerTag> cheerTags = cheerTagRepository.saveAll(cheerTagNames.toCheerTags(cheer)); | ||
|
Member
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. [선택 및 질문]
Member
Author
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.
저도 코드를 간결하게 작성하는 것을 좋아하는 입장에서 선호합니다! 하지만 이것을 사용하려면 테스트를 진행하면서 SQL이 최적화된 상태로 나가는지 체크해야 되는 복잡함이 있다고 생각합니다. |
||
| return new CheerResponse(cheer, cheerTags, store, imageStorage.getPreSignedUrl(imageKey)); | ||
| } | ||
|
|
||
| private void validateRegisterCheer(Member member, String storeKakaoId) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[선택]
접근자 오버라이드도 좋지만 컴펙트 생성자를 만들어서 처리하는건 어떨까요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ObjectMapper를 이용해서 Request Body JSON을 CheerRegisterRequest로 파싱하는 것으로 알고 있습니다.
ObjectMapper의 구현 방식에 따라서 필도로 주입하기도 하고, 생성자를 이용해 주입하기도 해서 아마 내부 구현이 어떤지에 따라 의도한대로 작동할 수도 안할수도 있다고 알고 있습니다.
그래서 Record의 getter를 이용해서 오버라이딩하여 일시적으로 처리했습니다.