2
2
3
3
import static com .tnt .common .error .model .ErrorMessage .PT_TRAINEE_ALREADY_EXIST ;
4
4
import static com .tnt .common .error .model .ErrorMessage .PT_TRAINER_TRAINEE_ALREADY_EXIST ;
5
- import static java .util .Objects .isNull ;
6
5
7
6
import java .time .LocalDate ;
8
7
import java .util .List ;
8
+ import java .util .stream .Collectors ;
9
9
10
10
import org .springframework .stereotype .Service ;
11
11
import org .springframework .transaction .annotation .Transactional ;
16
16
import com .tnt .common .error .exception .NotFoundException ;
17
17
import com .tnt .common .error .model .ErrorMessage ;
18
18
import com .tnt .domain .member .Member ;
19
+ import com .tnt .domain .pt .PtLesson ;
19
20
import com .tnt .domain .pt .PtTrainerTrainee ;
20
21
import com .tnt .domain .trainee .PtGoal ;
21
22
import com .tnt .domain .trainee .Trainee ;
22
23
import com .tnt .domain .trainer .Trainer ;
23
24
import com .tnt .dto .trainer .ConnectWithTrainerDto ;
24
25
import com .tnt .dto .trainer .request .ConnectWithTrainerRequest ;
25
26
import com .tnt .dto .trainer .response .ConnectWithTraineeResponse ;
27
+ import com .tnt .dto .trainer .response .GetPtLessonsOnDateResponse ;
28
+ import com .tnt .dto .trainer .response .GetPtLessonsOnDateResponse .Lesson ;
29
+ import com .tnt .infrastructure .mysql .repository .pt .PtGoalRepository ;
30
+ import com .tnt .infrastructure .mysql .repository .pt .PtLessonSearchRepository ;
26
31
import com .tnt .infrastructure .mysql .repository .pt .PtTrainerTraineeRepository ;
27
- import com .tnt .infrastructure .mysql .repository .trainee .PtGoalRepository ;
28
32
29
33
import lombok .RequiredArgsConstructor ;
30
34
@@ -37,17 +41,18 @@ public class PtService {
37
41
private final TrainerService trainerService ;
38
42
private final PtTrainerTraineeRepository ptTrainerTraineeRepository ;
39
43
private final PtGoalRepository ptGoalRepository ;
44
+ private final PtLessonSearchRepository ptLessonSearchRepository ;
40
45
41
46
@ Transactional
42
- public ConnectWithTrainerDto connectWithTrainer (String memberId , ConnectWithTrainerRequest request ) {
47
+ public ConnectWithTrainerDto connectWithTrainer (Long memberId , ConnectWithTrainerRequest request ) {
43
48
Trainer trainer = trainerService .getTrainerWithInvitationCode (request .invitationCode ());
44
49
Trainee trainee = traineeService .getTraineeWithMemberId (memberId );
45
50
46
51
validateNotAlreadyConnected (trainer .getId (), trainee .getId ());
47
52
48
53
PtTrainerTrainee ptTrainerTrainee = PtTrainerTrainee .builder ()
49
- .trainerId ( trainer . getId () )
50
- .traineeId ( trainee . getId () )
54
+ .trainer ( trainer )
55
+ .trainee ( trainee )
51
56
.startedAt (request .startDate ())
52
57
.finishedPtCount (request .finishedPtCount ())
53
58
.totalPtCount (request .totalPtCount ())
@@ -62,8 +67,8 @@ public ConnectWithTrainerDto connectWithTrainer(String memberId, ConnectWithTrai
62
67
trainerMember .getProfileImageUrl (), traineeMember .getProfileImageUrl (), trainer .getId (), trainee .getId ());
63
68
}
64
69
65
- public ConnectWithTraineeResponse getFirstTrainerTraineeConnect (String memberId , String trainerId ,
66
- String traineeId ) {
70
+ public ConnectWithTraineeResponse getFirstTrainerTraineeConnect (Long memberId , Long trainerId ,
71
+ Long traineeId ) {
67
72
validateIfNotConnected (trainerId , traineeId );
68
73
69
74
Trainer trainer = trainerService .getTrainerWithMemberId (memberId );
@@ -72,61 +77,44 @@ public ConnectWithTraineeResponse getFirstTrainerTraineeConnect(String memberId,
72
77
Member trainerMember = trainer .getMember (); // fetch join 으로 가져온 member
73
78
Member traineeMember = trainee .getMember (); // fetch join 으로 가져온 member
74
79
75
- String traineeAge = calculateCurrentAge (traineeMember .getBirthday ());
76
-
77
- List <PtGoal > ptGoals = ptGoalRepository .findAllByTraineeId (Long .valueOf (traineeId ));
78
- String ptGoal = getPtGoals (ptGoals );
80
+ List <PtGoal > ptGoals = ptGoalRepository .findAllByTraineeId (traineeId );
81
+ String ptGoal = ptGoals .stream ().map (PtGoal ::getContent ).collect (Collectors .joining (", " ));
79
82
80
83
return new ConnectWithTraineeResponse (trainerMember .getName (), traineeMember .getName (),
81
- trainerMember .getProfileImageUrl (), traineeMember .getProfileImageUrl (), traineeAge , trainee . getHeight (),
82
- trainee .getWeight (), ptGoal , trainee .getCautionNote ());
84
+ trainerMember .getProfileImageUrl (), traineeMember .getProfileImageUrl (), traineeMember . getAge (),
85
+ trainee .getHeight (), trainee . getWeight (), ptGoal , trainee .getCautionNote ());
83
86
}
84
87
85
- private void validateNotAlreadyConnected (Long trainerId , Long traineeId ) {
86
- ptTrainerTraineeRepository .findByTraineeIdAndDeletedAtIsNull (traineeId )
87
- .ifPresent (pt -> { // 이미 다른 트레이너와 연결 중인지 확인
88
- throw new ConflictException (PT_TRAINEE_ALREADY_EXIST );
89
- });
90
-
91
- ptTrainerTraineeRepository .findByTrainerIdAndTraineeIdAndDeletedAtIsNull (trainerId , traineeId )
92
- .ifPresent (pt -> { // 이미 해당 트레이너와 연결 중인지 확인
93
- throw new ConflictException (PT_TRAINER_TRAINEE_ALREADY_EXIST );
94
- });
95
- }
88
+ public GetPtLessonsOnDateResponse getPtLessonsOnDate (Long memberId , LocalDate date ) {
89
+ Trainer trainer = trainerService .getTrainerWithMemberId (memberId );
96
90
97
- private void validateIfNotConnected (String trainerId , String traineeId ) {
98
- ptTrainerTraineeRepository .findByTrainerIdAndTraineeIdAndDeletedAtIsNull (Long .valueOf (trainerId ),
99
- Long .valueOf (traineeId ))
100
- .orElseThrow (() -> new NotFoundException (ErrorMessage .PT_TRAINER_TRAINEE_NOT_FOUND ));
101
- }
91
+ List <PtLesson > ptLessons = ptLessonSearchRepository .findAllByTrainerIdAndDate (trainer .getId (), date );
102
92
103
- private String calculateCurrentAge (LocalDate birthDay ) {
104
- if (isNull (birthDay )) {
105
- return "비공개" ;
106
- }
93
+ List <Lesson > lessons = ptLessons .stream ().map (ptLesson -> {
94
+ PtTrainerTrainee ptTrainerTrainee = ptLesson .getPtTrainerTrainee ();
95
+ Trainee trainee = ptTrainerTrainee .getTrainee ();
107
96
108
- LocalDate currentDate = LocalDate .now ();
109
- int age = currentDate .getYear () - birthDay .getYear ();
97
+ return new Lesson (String .valueOf (ptLesson .getId ()),
98
+ String .valueOf (trainee .getId ()), trainee .getMember ().getName (), ptTrainerTrainee .getCurrentSession (),
99
+ ptLesson .getLessonStart (), ptLesson .getLessonEnd (), ptLesson .getIsCompleted ());
100
+ }).toList ();
110
101
111
- // 생일이 아직 지나지 않았으면 나이를 1 줄임
112
- if (currentDate .isBefore (birthDay .withYear (currentDate .getYear ()))) {
113
- age --;
114
- }
115
-
116
- return String .valueOf (age );
102
+ return new GetPtLessonsOnDateResponse (ptLessons .size (), date , lessons );
117
103
}
118
104
119
- private String getPtGoals (List <PtGoal > ptGoals ) {
120
- StringBuilder sb = new StringBuilder ();
121
-
122
- for (int i = 0 ; i < ptGoals .size (); i ++) {
123
- sb .append (ptGoals .get (i ).getContent ());
105
+ private void validateNotAlreadyConnected (Long trainerId , Long traineeId ) {
106
+ if (ptTrainerTraineeRepository .existsByTraineeIdAndDeletedAtIsNull (traineeId )) {
107
+ throw new ConflictException (PT_TRAINEE_ALREADY_EXIST );
108
+ }
124
109
125
- if (i != ptGoals .size () - 1 ) {
126
- sb .append (", " );
127
- }
110
+ if (ptTrainerTraineeRepository .existsByTrainerIdAndTraineeIdAndDeletedAtIsNull (trainerId , traineeId )) {
111
+ throw new ConflictException (PT_TRAINER_TRAINEE_ALREADY_EXIST );
128
112
}
113
+ }
129
114
130
- return sb .toString ();
115
+ private void validateIfNotConnected (Long trainerId , Long traineeId ) {
116
+ if (!ptTrainerTraineeRepository .existsByTrainerIdAndTraineeIdAndDeletedAtIsNull (trainerId , traineeId )) {
117
+ throw new NotFoundException (ErrorMessage .PT_TRAINER_TRAINEE_NOT_FOUND );
118
+ }
131
119
}
132
120
}
0 commit comments