-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCakeService.java
More file actions
333 lines (289 loc) Β· 14.8 KB
/
CakeService.java
File metadata and controls
333 lines (289 loc) Β· 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.cakey.cake.service;
import com.cakey.cake.domain.DayCategory;
import com.cakey.cake.dto.*;
import com.cakey.cake.exception.CakeErrorCode;
import com.cakey.cake.exception.CakeyNotFoundException;
import com.cakey.cake.facade.CakeFacade;
import com.cakey.caketheme.domain.ThemeName;
import com.cakey.common.exception.NotFoundBaseException;
import com.cakey.store.domain.Station;
import com.cakey.store.dto.StoreBySelectedCakeDto;
import com.cakey.store.exception.StoreErrorCode;
import com.cakey.store.exception.StoreNotfoundException;
import com.cakey.store.facade.StoreFacade;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.actuate.cache.CachesEndpoint;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class CakeService {
private final CakeFacade cakeFacade;
private final StoreFacade storeFacade;
private final CachesEndpoint cachesEndpoint;
//ν΄λΉμ μ€ν μ΄μ μΌμ΄ν¬λ€ μ‘°ν(μ΅μ μ)
@Transactional(readOnly = true)
public CakesLatestListRes getLatestCakesByStationStore(final Long userId,
final Station station,
final Long cakeIdCursor,
final int size) {
final List<CakeInfoDto> cakeInfoDtos;
///컀μ νμ΄μ§λ€μ΄μ
λ°μ΄ν°μ‘°ν
try {
cakeInfoDtos = cakeFacade.findLatestCakesByStation(userId, station, cakeIdCursor, size);
} catch (NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
///ν΄λΉμ μΌμ΄ν¬ κ°μ
final int cakeCountByStation = cakeFacade.countCakesByStation(station);
///λ§μ§λ§ λ°μ΄ν° μ¬λΆ
final int lastCakeInfoDtosIndex = cakeInfoDtos.size() - 1;
final boolean isLastData = cakeInfoDtos.get(lastCakeInfoDtosIndex).isLastData();
final Long nextCakeIdCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeIdCursor();
final List<CakeInfo> cakes = cakeInfoDtos.stream()
.map(cakeInfoDto -> CakeInfo.of(
cakeInfoDto.getCakeId(),
cakeInfoDto.getStoreId(),
cakeInfoDto.getStoreName(),
cakeInfoDto.getStation(),
cakeInfoDto.isLiked(),
cakeInfoDto.getImageUrl(),
cakeInfoDto.getCakeLikeCount()
))
.collect(Collectors.toList());
return CakesLatestListRes.from(nextCakeIdCursor, cakeCountByStation, isLastData, cakes);
}
//ν΄λΉμ λμμΈ(μΌμ΄ν¬) μ‘°ν(μΈκΈ°μ)
@Transactional(readOnly = true)
public CakesPopularListRes getPopularCakesByStationStore(final Long userId,
final Station station,
final Integer cakeLikesCursor,
final Long cakeIdCursor,
final int size) {
List<CakeInfoDto> cakeInfoDtos;
//컀μνμ΄μ§λ€μ΄μ
μΌλ‘ μΌμ΄ν¬ μ‘°ν
try {
cakeInfoDtos = cakeFacade.findPopularCakesByStation(userId, station, cakeLikesCursor, cakeIdCursor, size);
} catch (NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
///컀μ μ
λ°μ΄νΈ
final int lastCakeInfoDtosIndex = cakeInfoDtos.size() - 1;
final int nextLikesCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeLikeCount();
final Long nextCakeIdCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeIdCursor();
final boolean isLastData = cakeInfoDtos.get(lastCakeInfoDtosIndex).isLastData();
/// μ 체 μΌμ΄ν¬ μ κ³μ°
final int totalCakeCount = cakeFacade.countCakesByStation(station);
final List<CakeInfo> cakes = cakeInfoDtos.stream()
.map(dto -> CakeInfo.of(
dto.getCakeId(),
dto.getStoreId(),
dto.getStoreName(),
dto.getStation(),
dto.isLiked(),
dto.getImageUrl(),
dto.getCakeLikeCount()
))
.toList();
return CakesPopularListRes.from(nextLikesCursor, nextCakeIdCursor, totalCakeCount, isLastData, cakes);
}
public CakeListByPopularityRes getCakeByRank(final Long userId) {
final List<CakeByPopularityDto> cakeByPopularityDtos;
try {
cakeByPopularityDtos = cakeFacade.findCakeByRank(userId);
} catch (NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
return new CakeListByPopularityRes(cakeByPopularityDtos);
}
//μ νν λμμΈ(μΌμ΄ν¬) μ‘°ν
@Transactional(readOnly = true)
public CakeSelectedRes getSelectedCakes(final Long cakeId,
final DayCategory dayCategory,
final ThemeName theme,
final Long userId) {
final StoreBySelectedCakeDto storeInfoDto;
final List<CakeSelectedInfoDto> cakeSelectedInfoDtos;
///μ€ν μ΄ μ 보 μ‘°ν
try {
storeInfoDto = storeFacade.findStoreBySelectedCakeId(cakeId);
} catch (NotFoundBaseException e) {
throw new StoreNotfoundException(StoreErrorCode.STORE_NOT_FOUND_ENTITY);
}
///μΌμ΄ν¬ μ 보 μ‘°ν
try {
cakeSelectedInfoDtos = cakeFacade.findCakesByStoreAndConditions(
storeInfoDto.storeId(),
dayCategory,
theme,
userId,
cakeId
);
} catch (NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
final List<CakeSelectedInfo> cakeSelectedInfoList = cakeSelectedInfoDtos.stream()
.map(cake -> CakeSelectedInfo.of(
cake.cakeId(),
cake.isLiked(),
cake.imageUrl()
))
.toList();
return CakeSelectedRes.of(
storeInfoDto.storeId(),
storeInfoDto.storeName(),
storeInfoDto.station(),
cakeSelectedInfoList
);
}
//λμμΈ λλ¬λ³΄κΈ° μ‘°ν(μ΅μ μ)
@Transactional(readOnly = true)
public CakesLatestListRes findCakesByCategoryAndTheme(final DayCategory dayCategory,
final ThemeName theme,
final Long userId,
final Long cakeIdCursor,
final int limit) {
final List<CakeInfoDto> cakeInfoDtos;
///νμ΄μ§λ€μ΄μ
try {
cakeInfoDtos = cakeFacade.findCakesByCategoryAndTheme(dayCategory, theme, userId, cakeIdCursor, limit);
} catch (NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
///μΌμ΄ν¬ μ 체 κ°μ
final int totalCakeCount = cakeFacade.countCakesByCategoryAndTheme(dayCategory,theme);
///λ§μ§λ§ λ°μ΄ν° μ¬λΆ
final int lastCakeInfoDtosIndex = cakeInfoDtos.size() - 1;
final boolean isLastData = cakeInfoDtos.get(lastCakeInfoDtosIndex).isLastData();
final Long nextCakeIdCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeIdCursor();
final List<CakeInfo> cakes = cakeInfoDtos.stream()
.map(cakeInfoDto -> CakeInfo.of(
cakeInfoDto.getCakeId(),
cakeInfoDto.getStoreId(),
cakeInfoDto.getStoreName(),
cakeInfoDto.getStation(),
cakeInfoDto.isLiked(),
cakeInfoDto.getImageUrl(),
cakeInfoDto.getCakeLikeCount()
))
.collect(Collectors.toList());
return CakesLatestListRes.from(nextCakeIdCursor, totalCakeCount, isLastData, cakes);
}
//λμμΈ λλ¬λ³΄κΈ° μ‘°ν(μΈκΈ°μ)
@Transactional(readOnly = true)
public CakesPopularListRes getPopularCakesByCategoryAndTheme(final DayCategory dayCategory,
final ThemeName themeName,
final Long userId,
final Long cakeIdCursor,
final Integer cakeLikesCursor,
final int size) {
final List<CakeInfoDto> cakeInfoDtos;
///νμ΄μ§λ€μ΄μ
try {
cakeInfoDtos = cakeFacade.findPopularCakesByCategoryAndTheme(dayCategory, themeName, userId, cakeIdCursor, cakeLikesCursor, size);
} catch (NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
///μΌμ΄ν¬ μ 체κ°μ
final int totalCakeCount = cakeFacade.countCakesByCategoryAndTheme(dayCategory,themeName);
///컀μ μ
λ°μ΄νΈ
final int lastCakeInfoDtosIndex = cakeInfoDtos.size() - 1;
final int nextLikesCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeLikeCount();
final Long nextCakeIdCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeIdCursor();
final boolean isLastData = cakeInfoDtos.get(lastCakeInfoDtosIndex).isLastData();
final List<CakeInfo> cakes = cakeInfoDtos.stream()
.map(dto -> CakeInfo.of(
dto.getCakeId(),
dto.getStoreId(),
dto.getStoreName(),
dto.getStation(),
dto.isLiked(),
dto.getImageUrl(),
dto.getCakeLikeCount()
))
.toList();
return CakesPopularListRes.from(nextLikesCursor, nextCakeIdCursor, totalCakeCount, isLastData, cakes);
}
//μ°ν μ€ν μ΄λ€ λμμΈ μ‘°ν(μ΅μ μ)
@Transactional(readOnly = true)
public CakesLatestListRes getLatestCakeByStoreLiked(final long userId,
final Long cakeIdCursor,
final int size) {
final List<CakeInfoDto> cakeInfoDtos;
///νμ΄μ§λ€μ΄μ
try {
cakeInfoDtos = cakeFacade.findCakesLikedByUser(userId, cakeIdCursor, size);
} catch (final NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
///μ 체 μΌμ΄ν¬ κ°μ
final int totalCakeCount = cakeFacade.countAllDesignsLikedByUser(userId);
///컀μ μ
λ°μ΄νΈ
final int lastCakeInfoDtosIndex = cakeInfoDtos.size() - 1;
final Long nextCakeIdCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeIdCursor();
final boolean isLastData = cakeInfoDtos.get(lastCakeInfoDtosIndex).isLastData();
final List<CakeInfo> cakes = cakeInfoDtos.stream()
.map(cakeInfoDto -> CakeInfo.of(
cakeInfoDto.getCakeId(),
cakeInfoDto.getStoreId(),
cakeInfoDto.getStoreName(),
cakeInfoDto.getStation(),
cakeInfoDto.isLiked(),
cakeInfoDto.getImageUrl(),
cakeInfoDto.getCakeLikeCount()
))
.collect(Collectors.toList());
return CakesLatestListRes.from(nextCakeIdCursor, totalCakeCount, isLastData, cakes);
}
//μ°ν μ€ν μ΄λ€ λμμΈ μ‘°ν(μΈκΈ°μ)
@Transactional(readOnly = true)
public CakesPopularListRes getPopularCakeByStoreLiked(final long userId,
final Long cakeIdCursor,
final Integer cakeLikesCursor,
final int size) {
final List<CakeInfoDto> cakeInfoDtos;
///νμ΄μ§λ€μ΄μ
try {
cakeInfoDtos = cakeFacade.findPopularCakesLikedByUser(userId, cakeIdCursor, cakeLikesCursor, size);
} catch (NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
///μ 체 μΌμ΄ν¬ κ°μ
final int totalCakeCount = cakeFacade.countAllDesignsLikedByUser(userId);
///컀μ μ
λ°μ΄νΈ
final int lastCakeInfoDtosIndex = cakeInfoDtos.size() - 1;
final int nextLikesCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeLikeCount();
final Long nextCakeIdCursor = cakeInfoDtos.get(lastCakeInfoDtosIndex).getCakeIdCursor();
final boolean isLastData = cakeInfoDtos.get(lastCakeInfoDtosIndex).isLastData();
final List<CakeInfo> cakes = cakeInfoDtos.stream()
.map(dto -> CakeInfo.of(
dto.getCakeId(),
dto.getStoreId(),
dto.getStoreName(),
dto.getStation(),
dto.isLiked(),
dto.getImageUrl(),
dto.getCakeLikeCount()
))
.toList();
return CakesPopularListRes.from(nextLikesCursor, nextCakeIdCursor, totalCakeCount, isLastData, cakes);
}
//μ§λλ·° μ ν λμμΈ μ‘°ν
public CakeSelectedMapRes getMapSelectCake(final Long userId, final long cakeId) {
final CakeSelectedMapDto cakeSelectedMapDto;
try {
cakeSelectedMapDto = cakeFacade.getCakeSelectedMap(userId, cakeId);
} catch (final NotFoundBaseException e) {
throw new CakeyNotFoundException(CakeErrorCode.CAKE_NOT_FOUND_ENTITY);
}
return CakeSelectedMapRes.from(
cakeSelectedMapDto.getStoreId(),
cakeSelectedMapDto.getStoreName(),
cakeSelectedMapDto.getAddress(),
cakeSelectedMapDto.getStation(),
cakeSelectedMapDto.getIsLiked(),
cakeSelectedMapDto.getImageUrl());
}
}