11package eatda .service .cheer ;
22
3- import eatda .client .file .FileClient ;
43import eatda .controller .cheer .CheerImageResponse ;
54import eatda .controller .cheer .CheerInStoreResponse ;
65import eatda .controller .cheer .CheerPreviewResponse ;
98import eatda .controller .cheer .CheerSearchParameters ;
109import eatda .controller .cheer .CheersInStoreResponse ;
1110import eatda .controller .cheer .CheersResponse ;
12- import eatda .domain .ImageDomain ;
1311import eatda .domain .cheer .Cheer ;
1412import eatda .domain .cheer .CheerImage ;
1513import eatda .domain .member .Member ;
1614import eatda .domain .store .Store ;
1715import eatda .domain .store .StoreSearchResult ;
1816import eatda .exception .BusinessErrorCode ;
1917import eatda .exception .BusinessException ;
18+ import eatda .facade .CheerCreationResult ;
2019import eatda .repository .cheer .CheerRepository ;
2120import eatda .repository .member .MemberRepository ;
2221import eatda .repository .store .StoreRepository ;
3736public class CheerService {
3837
3938 private static final int MAX_CHEER_SIZE = 10_000 ;
40-
39+ private static final String SORTED_PROPERTIES = "createdAt" ;
4140 private final MemberRepository memberRepository ;
4241 private final StoreRepository storeRepository ;
4342 private final CheerRepository cheerRepository ;
44- private final FileClient fileClient ;
4543
4644 @ Value ("${cdn.base-url}" )
4745 private String cdnBaseUrl ;
4846
4947 @ Transactional
50- public CheerResponse registerCheer (CheerRegisterRequest request ,
51- StoreSearchResult result ,
52- long memberId ,
53- ImageDomain domain
48+ public CheerCreationResult createCheer (CheerRegisterRequest request ,
49+ StoreSearchResult result ,
50+ long memberId
5451 ) {
5552 Member member = memberRepository .getById (memberId );
5653 validateRegisterCheer (member , request .storeKakaoId ());
@@ -59,15 +56,7 @@ public CheerResponse registerCheer(CheerRegisterRequest request,
5956 .orElseGet (() -> storeRepository .save (result .toStore ())); // TODO 상점 조회/저장 동시성 이슈 해결
6057 Cheer cheer = new Cheer (member , store , request .description ());
6158 cheer .setCheerTags (request .tags ());
62- Cheer savedCheer = cheerRepository .save (cheer );
63-
64- // TODO 트랜잭션 범위 축소
65- List <CheerRegisterRequest .UploadedImageDetail > sortedImages = sortImages (request .images ());
66- List <String > permanentKeys = moveImages (domain , cheer .getId (), sortedImages );
67-
68- saveCheerImages (cheer , sortedImages , permanentKeys );
69-
70- return new CheerResponse (savedCheer , store , cdnBaseUrl );
59+ return new CheerCreationResult (cheerRepository .save (cheer ), store );
7160 }
7261
7362 private void validateRegisterCheer (Member member , String storeKakaoId ) {
@@ -79,25 +68,14 @@ private void validateRegisterCheer(Member member, String storeKakaoId) {
7968 }
8069 }
8170
82- private List <CheerRegisterRequest .UploadedImageDetail > sortImages (
83- List <CheerRegisterRequest .UploadedImageDetail > images ) {
84- return images .stream ()
85- .sorted (Comparator .comparingLong (CheerRegisterRequest .UploadedImageDetail ::orderIndex ))
86- .toList ();
87- }
71+ @ Transactional
72+ public void saveCheerImages (Long cheerId ,
73+ List <CheerRegisterRequest .UploadedImageDetail > sortedImages ,
74+ List <String > permanentKeys ) {
8875
89- private List <String > moveImages (ImageDomain domain ,
90- long cheerId ,
91- List <CheerRegisterRequest .UploadedImageDetail > sortedImages ) {
92- List <String > tempKeys = sortedImages .stream ()
93- .map (CheerRegisterRequest .UploadedImageDetail ::imageKey )
94- .toList ();
95- return fileClient .moveTempFilesToPermanent (domain .getName (), cheerId , tempKeys );
96- }
76+ Cheer cheer = cheerRepository .findById (cheerId )
77+ .orElseThrow (() -> new BusinessException (BusinessErrorCode .CHEER_NOT_FOUND ));
9778
98- private void saveCheerImages (Cheer cheer ,
99- List <CheerRegisterRequest .UploadedImageDetail > sortedImages ,
100- List <String > permanentKeys ) {
10179 IntStream .range (0 , sortedImages .size ())
10280 .forEach (i -> {
10381 var detail = sortedImages .get (i );
@@ -108,10 +86,8 @@ private void saveCheerImages(Cheer cheer,
10886 detail .contentType (),
10987 detail .fileSize ()
11088 );
111- cheer .addImage (cheerImage ); // 여기서 양방향 동기화
89+ cheer .addImage (cheerImage );
11290 });
113-
114- cheerRepository .save (cheer );
11591 }
11692
11793 @ Transactional (readOnly = true )
@@ -121,7 +97,7 @@ public CheersResponse getCheers(CheerSearchParameters parameters) {
12197 parameters .getCheerTagNames (),
12298 parameters .getDistricts (),
12399 PageRequest .of (parameters .getPage (), parameters .getSize (),
124- Sort .by (Direction .DESC , "createdAt" ))
100+ Sort .by (Direction .DESC , SORTED_PROPERTIES ))
125101 );
126102
127103 List <Cheer > cheers = cheerPage .getContent ();
@@ -130,14 +106,11 @@ public CheersResponse getCheers(CheerSearchParameters parameters) {
130106
131107 private CheersResponse toCheersResponse (List <Cheer > cheers ) {
132108 return new CheersResponse (cheers .stream ()
133- .map (cheer -> {
134- Store store = cheer .getStore ();
135- return new CheerPreviewResponse (cheer ,
136- cheer .getImages ().stream ()
137- .map (img -> new CheerImageResponse (img , cdnBaseUrl ))
138- .sorted (Comparator .comparingLong (CheerImageResponse ::orderIndex ))
139- .toList ());
140- })
109+ .map (cheer -> new CheerPreviewResponse (cheer ,
110+ cheer .getImages ().stream ()
111+ .map (img -> new CheerImageResponse (img , cdnBaseUrl ))
112+ .sorted (Comparator .comparingLong (CheerImageResponse ::orderIndex ))
113+ .toList ()))
141114 .toList ());
142115 }
143116
@@ -152,4 +125,17 @@ public CheersInStoreResponse getCheersByStoreId(Long storeId, int page, int size
152125
153126 return new CheersInStoreResponse (cheersResponse );
154127 }
128+
129+ @ Transactional
130+ public void deleteCheer (Long cheerId ) {
131+ cheerRepository .deleteById (cheerId );
132+ }
133+
134+ @ Transactional (readOnly = true )
135+ public CheerResponse getCheerResponse (Long cheerId ) {
136+ Cheer cheer = cheerRepository .findById (cheerId )
137+ .orElseThrow (() -> new BusinessException (BusinessErrorCode .CHEER_NOT_FOUND ));
138+
139+ return new CheerResponse (cheer , cdnBaseUrl );
140+ }
155141}
0 commit comments