1
1
package com .tnt .application .pt ;
2
2
3
3
import static com .tnt .common .error .model .ErrorMessage .DIET_DUPLICATE_TIME ;
4
+ import static com .tnt .common .error .model .ErrorMessage .PT_LESSON_CREATE_BEFORE_START ;
4
5
import static com .tnt .common .error .model .ErrorMessage .PT_LESSON_DUPLICATE_TIME ;
5
6
import static com .tnt .common .error .model .ErrorMessage .PT_LESSON_MORE_THAN_ONE_A_DAY ;
6
7
import static com .tnt .common .error .model .ErrorMessage .PT_LESSON_NOT_FOUND ;
37
38
import com .tnt .domain .trainer .Trainer ;
38
39
import com .tnt .dto .trainee .TraineeProjection ;
39
40
import com .tnt .dto .trainee .request .ConnectWithTrainerRequest ;
40
- import com .tnt .dto .trainee .request .CreateDietRequest ;
41
- import com .tnt .dto .trainee .response .CreateDietResponse ;
42
- import com .tnt .dto .trainee .response .GetDietResponse ;
43
41
import com .tnt .dto .trainee .response .GetTraineeCalendarPtLessonCountResponse ;
44
42
import com .tnt .dto .trainee .response .GetTraineeDailyRecordsResponse ;
45
43
import com .tnt .dto .trainer .ConnectWithTrainerDto ;
57
55
import com .tnt .infrastructure .mysql .repository .pt .PtLessonSearchRepository ;
58
56
import com .tnt .infrastructure .mysql .repository .pt .PtTrainerTraineeRepository ;
59
57
import com .tnt .infrastructure .mysql .repository .pt .PtTrainerTraineeSearchRepository ;
58
+ import com .tnt .infrastructure .mysql .repository .trainee .DietRepository ;
60
59
61
60
import lombok .RequiredArgsConstructor ;
62
61
@@ -73,11 +72,12 @@ public class PtService {
73
72
private final PtTrainerTraineeSearchRepository ptTrainerTraineeSearchRepository ;
74
73
private final PtLessonRepository ptLessonRepository ;
75
74
private final PtLessonSearchRepository ptLessonSearchRepository ;
75
+ private final DietRepository dietRepository ;
76
76
77
77
@ Transactional
78
78
public ConnectWithTrainerDto connectWithTrainer (Long memberId , ConnectWithTrainerRequest request ) {
79
- Trainer trainer = trainerService .getTrainerWithInvitationCode (request .invitationCode ());
80
- Trainee trainee = traineeService .getTraineeWithMemberId (memberId );
79
+ Trainer trainer = trainerService .getByInvitationCode (request .invitationCode ());
80
+ Trainee trainee = traineeService .getByMemberId (memberId );
81
81
82
82
validateNotAlreadyConnected (trainer .getId (), trainee .getId ());
83
83
@@ -103,13 +103,13 @@ public ConnectWithTraineeResponse getFirstTrainerTraineeConnect(Long memberId, L
103
103
Long traineeId ) {
104
104
validateIfNotConnected (trainerId , traineeId );
105
105
106
- Trainer trainer = trainerService .getTrainerWithMemberId (memberId );
107
- Trainee trainee = traineeService .getTraineeWithId (traineeId );
106
+ Trainer trainer = trainerService .getByMemberId (memberId );
107
+ Trainee trainee = traineeService .getByTraineeId (traineeId );
108
108
109
109
Member trainerMember = trainer .getMember (); // fetch join 으로 가져온 member
110
110
Member traineeMember = trainee .getMember (); // fetch join 으로 가져온 member
111
111
112
- List <PtGoal > ptGoals = ptGoalService .getAllPtGoalsWithTraineeId (traineeId );
112
+ List <PtGoal > ptGoals = ptGoalService .getAllByTraineeId (traineeId );
113
113
String ptGoal = ptGoals .stream ().map (PtGoal ::getContent ).collect (Collectors .joining (", " ));
114
114
115
115
return new ConnectWithTraineeResponse (
@@ -121,7 +121,7 @@ public ConnectWithTraineeResponse getFirstTrainerTraineeConnect(Long memberId, L
121
121
122
122
@ Transactional (readOnly = true )
123
123
public GetPtLessonsOnDateResponse getPtLessonsOnDate (Long memberId , LocalDate date ) {
124
- Trainer trainer = trainerService .getTrainerWithMemberId (memberId );
124
+ Trainer trainer = trainerService .getByMemberId (memberId );
125
125
126
126
List <PtLesson > ptLessons = ptLessonSearchRepository .findAllByTrainerIdAndDate (trainer .getId (), date );
127
127
@@ -140,7 +140,7 @@ public GetPtLessonsOnDateResponse getPtLessonsOnDate(Long memberId, LocalDate da
140
140
141
141
@ Transactional (readOnly = true )
142
142
public GetCalendarPtLessonCountResponse getCalendarPtLessonCount (Long memberId , Integer year , Integer month ) {
143
- Trainer trainer = trainerService .getTrainerWithMemberId (memberId );
143
+ Trainer trainer = trainerService .getByMemberId (memberId );
144
144
145
145
List <PtLesson > ptLessons = ptLessonSearchRepository .findAllByTraineeIdForTrainerCalendar (trainer .getId (), year ,
146
146
month );
@@ -160,7 +160,7 @@ public GetCalendarPtLessonCountResponse getCalendarPtLessonCount(Long memberId,
160
160
161
161
@ Transactional (readOnly = true )
162
162
public GetActiveTraineesResponse getActiveTrainees (Long memberId ) {
163
- Trainer trainer = trainerService .getTrainerWithMemberId (memberId );
163
+ Trainer trainer = trainerService .getByMemberId (memberId );
164
164
165
165
List <Trainee > trainees = ptTrainerTraineeSearchRepository .findAllTrainees (trainer .getId ());
166
166
@@ -169,7 +169,7 @@ public GetActiveTraineesResponse getActiveTrainees(Long memberId) {
169
169
trainee .getId ())
170
170
.orElseThrow (() -> new NotFoundException (TRAINEE_NOT_FOUND ));
171
171
172
- List <String > ptGoals = ptGoalService .getAllPtGoalsWithTraineeId (trainee .getId ())
172
+ List <String > ptGoals = ptGoalService .getAllByTraineeId (trainee .getId ())
173
173
.stream ()
174
174
.map (PtGoal ::getContent )
175
175
.toList ();
@@ -216,43 +216,17 @@ public void completePtLesson(Long memberId, Long ptLessonId) {
216
216
ptLesson .completeLesson (ptTrainerTrainee .getFinishedPtCount ());
217
217
}
218
218
219
- @ Transactional
220
- public CreateDietResponse createDiet (Long traineeId , CreateDietRequest request , String dietImageUrl ) {
221
- Diet diet = Diet .builder ()
222
- .traineeId (traineeId )
223
- .date (request .date ())
224
- .dietImageUrl (dietImageUrl )
225
- .memo (request .memo ())
226
- .dietType (request .dietType ())
227
- .build ();
228
-
229
- Diet saveDiet = dietService .save (diet );
230
-
231
- return new CreateDietResponse (saveDiet .getId (), saveDiet .getDate (), saveDiet .getDietImageUrl (),
232
- saveDiet .getDietType (), saveDiet .getMemo ());
233
- }
234
-
235
- @ Transactional (readOnly = true )
236
- public GetDietResponse getDiet (Long memberId , Long dietId ) {
237
- Trainee trainee = traineeService .getTraineeWithMemberId (memberId );
238
-
239
- Diet diet = dietService .getDietWithTraineeIdAndDietId (dietId , trainee .getId ());
240
-
241
- return new GetDietResponse (diet .getId (), diet .getDate (), diet .getDietImageUrl (), diet .getDietType (),
242
- diet .getMemo ());
243
- }
244
-
245
219
@ Transactional (readOnly = true )
246
220
public GetTraineeCalendarPtLessonCountResponse getTraineeCalendarPtLessonCount (Long memberId , LocalDate startDate ,
247
221
LocalDate endDate ) {
248
- Trainee trainee = traineeService .getTraineeWithMemberId (memberId );
222
+ Trainee trainee = traineeService .getByMemberId (memberId );
249
223
250
224
// 기간 내 PT 수업 조회
251
225
List <PtLesson > ptLessons = ptLessonSearchRepository .findAllByTraineeIdForTraineeCalendar (trainee .getId (),
252
226
startDate , endDate );
253
227
254
228
// 기간 내 식단 조회
255
- List <Diet > diets = dietService .getDietsWithTraineeIdForTraineeCalendar (trainee .getId (), startDate , endDate );
229
+ List <Diet > diets = dietService .getAllByTraineeIdForTraineeCalendar (trainee .getId (), startDate , endDate );
256
230
257
231
// Mapping
258
232
List <LocalDate > dates = Stream .concat (
@@ -272,7 +246,7 @@ public GetTraineeCalendarPtLessonCountResponse getTraineeCalendarPtLessonCount(L
272
246
273
247
@ Transactional (readOnly = true )
274
248
public GetTraineeDailyRecordsResponse getDailyRecords (Long memberId , LocalDate date ) {
275
- Trainee trainee = traineeService .getTraineeWithMemberIdNoFetch (memberId );
249
+ Trainee trainee = traineeService .getByMemberIdNoFetch (memberId );
276
250
277
251
// PT 정보 조회
278
252
TraineeProjection .PtInfoDto ptResult = ptLessonSearchRepository .findAllByTraineeIdForDaily (trainee .getId (),
@@ -285,7 +259,7 @@ public GetTraineeDailyRecordsResponse getDailyRecords(Long memberId, LocalDate d
285
259
ptResult .session (), ptResult .lessonStart (), ptResult .lessonEnd ());
286
260
287
261
// 식단 정보 조회
288
- List <Diet > diets = dietService .getDietsWithTraineeIdForDaily (trainee .getId (), date );
262
+ List <Diet > diets = dietService .getAllByTraineeIdForDaily (trainee .getId (), date );
289
263
290
264
// 식단 정보 Mapping to DietRecord
291
265
List <GetTraineeDailyRecordsResponse .DietRecord > dietRecords = diets .stream ()
@@ -296,10 +270,10 @@ public GetTraineeDailyRecordsResponse getDailyRecords(Long memberId, LocalDate d
296
270
return new GetTraineeDailyRecordsResponse (date , ptInfo , dietRecords );
297
271
}
298
272
299
- public Long validateDuplicationDiet (Long memberId , LocalDateTime date ) {
300
- Trainee trainee = traineeService .getTraineeWithMemberId (memberId );
273
+ public Long validateDietDuplicateAndGetTraineeId (Long memberId , LocalDateTime date ) {
274
+ Trainee trainee = traineeService .getByMemberId (memberId );
301
275
302
- if (dietService .isDietExistWithTraineeIdAndDate (trainee .getId (), date )) {
276
+ if (dietService .isDietExistByTraineeIdAndDate (trainee .getId (), date )) {
303
277
throw new ConflictException (DIET_DUPLICATE_TIME );
304
278
}
305
279
@@ -358,6 +332,10 @@ private void validateIfNotConnected(Long trainerId, Long traineeId) {
358
332
}
359
333
360
334
private void validateLessonTime (PtTrainerTrainee ptTrainerTrainee , LocalDateTime start , LocalDateTime end ) {
335
+ if (ptTrainerTrainee .getStartedAt ().isAfter (start .toLocalDate ())) {
336
+ throw new BadRequestException (PT_LESSON_CREATE_BEFORE_START );
337
+ }
338
+
361
339
if (ptLessonSearchRepository .existsByStartAndEnd (ptTrainerTrainee , start , end )) {
362
340
throw new ConflictException (PT_LESSON_DUPLICATE_TIME );
363
341
}
0 commit comments