-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPtService.java
More file actions
376 lines (293 loc) ยท 14.5 KB
/
PtService.java
File metadata and controls
376 lines (293 loc) ยท 14.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package com.tnt.pt.application;
import static com.tnt.common.error.model.ErrorMessage.DIET_DUPLICATE_TIME;
import static com.tnt.common.error.model.ErrorMessage.PT_LESSON_CREATE_BEFORE_START;
import static com.tnt.common.error.model.ErrorMessage.PT_LESSON_DUPLICATE_TIME;
import static com.tnt.common.error.model.ErrorMessage.PT_LESSON_MORE_THAN_ONE_A_DAY;
import static com.tnt.common.error.model.ErrorMessage.PT_LESSON_OVERFLOW;
import static com.tnt.common.error.model.ErrorMessage.PT_TRAINEE_ALREADY_EXIST;
import static com.tnt.common.error.model.ErrorMessage.PT_TRAINER_TRAINEE_ALREADY_EXIST;
import static com.tnt.common.error.model.ErrorMessage.PT_TRAINER_TRAINEE_NOT_FOUND;
import static java.util.stream.Collectors.groupingBy;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tnt.common.error.exception.BadRequestException;
import com.tnt.common.error.exception.ConflictException;
import com.tnt.common.error.exception.NotFoundException;
import com.tnt.member.domain.Member;
import com.tnt.pt.application.repository.PtLessonRepository;
import com.tnt.pt.application.repository.PtTrainerTraineeRepository;
import com.tnt.pt.domain.PtLesson;
import com.tnt.pt.domain.PtTrainerTrainee;
import com.tnt.pt.dto.PtTrainerTraineeProjection;
import com.tnt.trainee.application.DietService;
import com.tnt.trainee.application.TraineeService;
import com.tnt.trainee.domain.Diet;
import com.tnt.trainee.domain.PtGoal;
import com.tnt.trainee.domain.Trainee;
import com.tnt.trainee.dto.request.ConnectWithTrainerRequest;
import com.tnt.trainee.dto.response.GetTraineeCalendarPtLessonCountResponse;
import com.tnt.trainee.dto.response.GetTraineeDailyRecordsResponse;
import com.tnt.trainer.application.TrainerService;
import com.tnt.trainer.domain.Trainer;
import com.tnt.trainer.dto.ConnectWithTrainerDto;
import com.tnt.trainer.dto.request.CreatePtLessonRequest;
import com.tnt.trainer.dto.response.ConnectWithTraineeResponse;
import com.tnt.trainer.dto.response.ConnectWithTraineeResponse.ConnectTraineeInfo;
import com.tnt.trainer.dto.response.ConnectWithTraineeResponse.ConnectTrainerInfo;
import com.tnt.trainer.dto.response.GetActiveTraineesResponse;
import com.tnt.trainer.dto.response.GetActiveTraineesResponse.ActiveTraineeInfo;
import com.tnt.trainer.dto.response.GetCalendarPtLessonCountResponse;
import com.tnt.trainer.dto.response.GetCalendarPtLessonCountResponse.CalendarPtLessonCount;
import com.tnt.trainer.dto.response.GetPtLessonsOnDateResponse;
import com.tnt.trainer.dto.response.GetPtLessonsOnDateResponse.Lesson;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class PtService {
private final TrainerService trainerService;
private final TraineeService traineeService;
private final DietService dietService;
private final PtTrainerTraineeRepository ptTrainerTraineeRepository;
private final PtLessonRepository ptLessonRepository;
@Transactional
public ConnectWithTrainerDto connectWithTrainer(Long memberId, ConnectWithTrainerRequest request) {
Trainer trainer = trainerService.getByInvitationCode(request.invitationCode());
Trainee trainee = traineeService.getByMemberId(memberId);
validateNotAlreadyConnected(trainer.getId(), trainee.getId());
PtTrainerTrainee ptTrainerTrainee = PtTrainerTrainee.builder()
.trainer(trainer)
.trainee(trainee)
.startedAt(request.startDate())
.finishedPtCount(request.finishedPtCount())
.totalPtCount(request.totalPtCount())
.build();
ptTrainerTraineeRepository.save(ptTrainerTrainee);
Member trainerMember = trainer.getMember(); // fetch join ์ผ๋ก ๊ฐ์ ธ์จ member
Member traineeMember = trainee.getMember(); // fetch join ์ผ๋ก ๊ฐ์ ธ์จ member
return new ConnectWithTrainerDto(trainerMember.getFcmToken(), trainerMember.getName(), traineeMember.getName(),
trainerMember.getProfileImageUrl(), traineeMember.getProfileImageUrl(), trainer.getId(), trainee.getId());
}
@Transactional(readOnly = true)
public ConnectWithTraineeResponse getFirstTrainerTraineeConnect(Long memberId, Long trainerId, Long traineeId) {
validateIfNotConnected(trainerId, traineeId);
Trainer trainer = trainerService.getByMemberId(memberId);
Trainee trainee = traineeService.getByTraineeId(traineeId);
Member trainerMember = trainer.getMember(); // fetch join ์ผ๋ก ๊ฐ์ ธ์จ member
Member traineeMember = trainee.getMember(); // fetch join ์ผ๋ก ๊ฐ์ ธ์จ member
List<PtGoal> ptGoals = trainee.getPtGoals();
return new ConnectWithTraineeResponse(
new ConnectTrainerInfo(trainerMember.getName(), trainerMember.getProfileImageUrl()),
new ConnectTraineeInfo(traineeMember.getName(), traineeMember.getProfileImageUrl(),
traineeMember.getAge(), trainee.getHeight(), trainee.getWeight(), ptGoals, trainee.getCautionNote())
);
}
@Transactional(readOnly = true)
public GetPtLessonsOnDateResponse getPtLessonsOnDate(Long memberId, LocalDate date) {
Trainer trainer = trainerService.getByMemberId(memberId);
List<PtLesson> ptLessons = ptLessonRepository.findAllByTrainerIdAndDate(trainer.getId(), date);
List<Lesson> lessons = ptLessons.stream().map(ptLesson -> {
PtTrainerTrainee ptTrainerTrainee = ptLesson.getPtTrainerTrainee();
Trainee trainee = ptTrainerTrainee.getTrainee();
return new Lesson(String.valueOf(ptLesson.getId()),
String.valueOf(trainee.getId()), trainee.getMember().getName(),
trainee.getMember().getProfileImageUrl(), ptLesson.getSession(),
ptLesson.getLessonStart(), ptLesson.getLessonEnd(), ptLesson.getIsCompleted());
}).toList();
return new GetPtLessonsOnDateResponse(ptLessons.size(), date, lessons);
}
@Transactional(readOnly = true)
public GetCalendarPtLessonCountResponse getCalendarPtLessonCount(Long memberId, Integer year, Integer month) {
Trainer trainer = trainerService.getByMemberId(memberId);
List<PtLesson> ptLessons = ptLessonRepository.findAllByTraineeIdForTrainerCalendar(trainer.getId(), year,
month);
List<CalendarPtLessonCount> counts = ptLessons.stream()
.collect(groupingBy(
lesson -> lesson.getLessonStart().toLocalDate(),
LinkedHashMap::new,
Collectors.counting()
))
.entrySet().stream()
.map(entry -> new CalendarPtLessonCount(entry.getKey(), entry.getValue().intValue()))
.toList();
return new GetCalendarPtLessonCountResponse(counts);
}
@Transactional(readOnly = true)
public GetActiveTraineesResponse getActiveTrainees(Long memberId) {
Trainer trainer = trainerService.getByMemberId(memberId);
List<Trainee> trainees = ptTrainerTraineeRepository.findAllTrainees(trainer.getId());
List<ActiveTraineeInfo> activeTraineeInfo = trainees.stream().map(trainee -> {
PtTrainerTrainee ptTrainerTrainee = ptTrainerTraineeRepository.findByTraineeId(trainee.getId());
List<PtGoal> ptGoals = ptTrainerTrainee.getTrainee().getPtGoals();
// Memo ์ถ๊ฐ ๊ตฌํ ํ์
return new ActiveTraineeInfo(trainee.getId(), trainee.getMember().getName(),
trainee.getMember().getProfileImageUrl(), ptTrainerTrainee.getFinishedPtCount(),
ptTrainerTrainee.getTotalPtCount(), "", ptGoals);
}).toList();
return new GetActiveTraineesResponse(trainees.size(), activeTraineeInfo);
}
@Transactional
public void addPtLesson(Long memberId, CreatePtLessonRequest request) {
trainerService.validateTrainerRegistration(memberId);
PtTrainerTrainee ptTrainerTrainee = getPtTrainerTraineeWithTraineeId(request.traineeId());
// ํธ๋ ์ด๋์ ๊ธฐ์กด pt ์์
์ค์ ์ค๋ณต๋๋ ์๊ฐ๋๊ฐ ์๋์ง ํ์ธ
validateLessonTime(ptTrainerTrainee, request.start(), request.end());
int nextSession = validateAndGetNextSession(ptTrainerTrainee);
PtLesson ptLesson = PtLesson.builder()
.ptTrainerTrainee(ptTrainerTrainee)
.lessonStart(request.start())
.lessonEnd(request.end())
.memo(request.memo())
.session(nextSession)
.build();
ptLessonRepository.save(ptLesson);
}
@Transactional
public void completePtLesson(Long memberId, Long ptLessonId) {
trainerService.validateTrainerRegistration(memberId);
PtLesson ptLesson = getPtLessonWithId(ptLessonId);
PtTrainerTrainee ptTrainerTrainee = ptLesson.getPtTrainerTrainee();
ptTrainerTrainee.completeLesson();
ptLesson.complete(ptTrainerTrainee.getFinishedPtCount());
List<PtLesson> lessonsNotCompleted =
ptLessonRepository.findAllByPtTrainerTraineeAndIsCompletedIsFalseWithout(ptTrainerTrainee, ptLessonId);
lessonsNotCompleted.forEach(lesson -> {
if (!lesson.getId().equals(ptLessonId)) {
lesson.increaseSession();
}
});
ptLessonRepository.save(ptLesson);
ptLessonRepository.saveAll(lessonsNotCompleted);
ptTrainerTraineeRepository.save(ptTrainerTrainee);
}
@Transactional
public void cancelPtLesson(Long memberId, Long ptLessonId) {
trainerService.validateTrainerRegistration(memberId);
PtLesson ptLesson = getPtLessonWithId(ptLessonId);
PtTrainerTrainee ptTrainerTrainee = ptLesson.getPtTrainerTrainee();
List<PtLesson> lessonsNotCompleted =
ptLessonRepository.findAllByPtTrainerTraineeAndIsCompletedIsFalseWithout(ptTrainerTrainee, ptLessonId);
lessonsNotCompleted.forEach(lesson -> {
if (!lesson.getId().equals(ptLessonId) && lesson.getSession() > ptLesson.getSession()) {
lesson.decreaseSession();
}
});
ptTrainerTrainee.cancelLesson();
ptLesson.cancel(ptTrainerTrainee.getCurrentPtSession());
ptLessonRepository.save(ptLesson);
ptLessonRepository.saveAll(lessonsNotCompleted);
ptTrainerTraineeRepository.save(ptTrainerTrainee);
}
@Transactional(readOnly = true)
public GetTraineeCalendarPtLessonCountResponse getTraineeCalendarPtLessonCount(Long memberId, LocalDate startDate,
LocalDate endDate) {
Trainee trainee = traineeService.getByMemberId(memberId);
// ๊ธฐ๊ฐ ๋ด PT ์์
์กฐํ
List<PtLesson> ptLessons = ptLessonRepository.findAllByTraineeIdForTraineeCalendar(trainee.getId(),
startDate, endDate);
// ๊ธฐ๊ฐ ๋ด ์๋จ ์กฐํ
List<Diet> diets = dietService.getAllByTraineeIdForTraineeCalendar(trainee.getId(), startDate, endDate);
// Mapping
List<LocalDate> dates = Stream.concat(
ptLessons.stream()
.map(PtLesson::getLessonStart)
.map(LocalDateTime::toLocalDate),
diets.stream()
.map(Diet::getDate)
.map(LocalDateTime::toLocalDate)
)
.distinct()
.sorted()
.toList();
return new GetTraineeCalendarPtLessonCountResponse(dates);
}
@Transactional(readOnly = true)
public GetTraineeDailyRecordsResponse getDailyRecords(Long memberId, LocalDate date) {
Trainee trainee = traineeService.getByMemberIdNoFetch(memberId);
// PT ์ ๋ณด ์กฐํ
PtTrainerTraineeProjection.PtInfoDto ptResult = ptLessonRepository.findPtInfoByTraineeIdForDaily(
trainee.getId(),
date).orElse(new PtTrainerTraineeProjection.PtInfoDto(null, null, null, null, null));
// PT ์ ๋ณด Mapping to PtInfo
GetTraineeDailyRecordsResponse.PtInfo ptInfo =
ptResult.trainerName() == null ? null :
new GetTraineeDailyRecordsResponse.PtInfo(ptResult.trainerName(), ptResult.trainerProfileImage(),
ptResult.session(), ptResult.lessonStart(), ptResult.lessonEnd());
// ์๋จ ์ ๋ณด ์กฐํ
List<Diet> diets = dietService.getAllByTraineeIdForDaily(trainee.getId(), date);
// ์๋จ ์ ๋ณด Mapping to DietRecord
List<GetTraineeDailyRecordsResponse.DietRecord> dietRecords = diets.stream()
.map(diet -> new GetTraineeDailyRecordsResponse.DietRecord(diet.getId(), diet.getDate(),
diet.getDietImageUrl(), diet.getDietType(), diet.getMemo()))
.toList();
return new GetTraineeDailyRecordsResponse(date, ptInfo, dietRecords);
}
public Long validateDietDuplicateAndGetTraineeId(Long memberId, LocalDateTime date) {
Trainee trainee = traineeService.getByMemberId(memberId);
if (dietService.isDietExistByTraineeIdAndDate(trainee.getId(), date)) {
throw new ConflictException(DIET_DUPLICATE_TIME);
}
return trainee.getId();
}
public boolean isPtTrainerTraineeExistWithTrainerId(Long trainerId) {
return ptTrainerTraineeRepository.existsByTrainerId(trainerId);
}
public boolean isPtTrainerTraineeExistWithTraineeId(Long traineeId) {
return ptTrainerTraineeRepository.existsByTraineeId(traineeId);
}
public List<PtTrainerTrainee> getAllPtTrainerTraineeWithTrainerId(Long trainerId) {
return ptTrainerTraineeRepository.findAllByTrainerId(trainerId);
}
public List<PtTrainerTrainee> getAllPtTrainerTraineeWithTrainerIdWithDeleted(Long trainerId) {
return ptTrainerTraineeRepository.findAllByTrainerIdWithDeleted(trainerId);
}
public PtTrainerTrainee getPtTrainerTraineeWithTrainerId(Long trainerId) {
return ptTrainerTraineeRepository.findByTrainerId(trainerId);
}
public PtTrainerTrainee getPtTrainerTraineeWithTraineeId(Long traineeId) {
return ptTrainerTraineeRepository.findByTraineeId(traineeId);
}
public List<PtLesson> getPtLessonWithPtTrainerTrainee(PtTrainerTrainee ptTrainerTrainee) {
return ptLessonRepository.findAllByPtTrainerTrainee(ptTrainerTrainee);
}
public PtLesson getPtLessonWithId(Long ptLessonId) {
return ptLessonRepository.findById(ptLessonId);
}
private void validateNotAlreadyConnected(Long trainerId, Long traineeId) {
if (ptTrainerTraineeRepository.existsByTraineeId(traineeId)) {
throw new ConflictException(PT_TRAINEE_ALREADY_EXIST);
}
if (ptTrainerTraineeRepository.existsByTrainerIdAndTraineeId(trainerId, traineeId)) {
throw new ConflictException(PT_TRAINER_TRAINEE_ALREADY_EXIST);
}
}
private void validateIfNotConnected(Long trainerId, Long traineeId) {
if (!ptTrainerTraineeRepository.existsByTrainerIdAndTraineeId(trainerId, traineeId)) {
throw new NotFoundException(PT_TRAINER_TRAINEE_NOT_FOUND);
}
}
private void validateLessonTime(PtTrainerTrainee ptTrainerTrainee, LocalDateTime start, LocalDateTime end) {
if (ptTrainerTrainee.getStartedAt().isAfter(start.toLocalDate())) {
throw new BadRequestException(PT_LESSON_CREATE_BEFORE_START);
}
if (ptLessonRepository.existsByStartAndEnd(ptTrainerTrainee, start, end)) {
throw new ConflictException(PT_LESSON_DUPLICATE_TIME);
}
if (ptLessonRepository.existsByStart(ptTrainerTrainee, start)) {
throw new ConflictException(PT_LESSON_MORE_THAN_ONE_A_DAY);
}
}
private int validateAndGetNextSession(PtTrainerTrainee ptTrainerTrainee) {
List<PtLesson> lessonsForTrainee =
ptLessonRepository.findAllByPtTrainerTrainee(ptTrainerTrainee);
if (lessonsForTrainee.size() >= ptTrainerTrainee.getTotalPtCount()) {
throw new BadRequestException(PT_LESSON_OVERFLOW);
}
return ptTrainerTrainee.getCurrentPtSession();
}
}