33import com .demo .pteam .external .kakao .dto .KakaoGeoResponse ;
44import com .demo .pteam .external .kakao .service .KakaoMapService ;
55import com .demo .pteam .global .exception .ApiException ;
6+ import com .demo .pteam .trainer .address .domain .Coordinates ;
67import com .demo .pteam .trainer .address .domain .TrainerAddress ;
78import com .demo .pteam .trainer .address .exception .TrainerAddressErrorCode ;
89import com .demo .pteam .trainer .address .mapper .TrainerAddressMapper ;
910import com .demo .pteam .trainer .address .repository .TrainerAddressRepository ;
11+ import com .demo .pteam .trainer .address .repository .entity .TrainerAddressEntity ;
1012import com .demo .pteam .trainer .profile .controller .dto .TrainerProfileRequest ;
1113import com .demo .pteam .trainer .profile .domain .TrainerProfile ;
1214import com .demo .pteam .trainer .profile .exception .TrainerProfileErrorCode ;
15+ import com .demo .pteam .trainer .profile .mapper .TrainerProfileMapper ;
1316import com .demo .pteam .trainer .profile .repository .TrainerProfileRepository ;
17+ import com .demo .pteam .trainer .profile .repository .entity .TrainerProfileEntity ;
18+ import jakarta .validation .Valid ;
1419import lombok .RequiredArgsConstructor ;
1520import org .springframework .stereotype .Service ;
1621import org .springframework .transaction .annotation .Transactional ;
1722
1823@ Service
1924@ RequiredArgsConstructor
25+ @ Transactional
2026public class TrainerProfileService {
2127
2228 private final TrainerProfileRepository trainerProfileRepository ;
2329 private final TrainerAddressRepository trainerAddressRepository ;
2430 private final KakaoMapService kakaoMapService ;
2531
26- @ Transactional
32+ /**
33+ * 트레이너 프로필 등록
34+ * @param request 트레이너 프로필 요청 DTO
35+ * @param userId 로그인 사용자 id
36+ */
2737 public void createProfile (TrainerProfileRequest request , Long userId ) {
38+ // TODO: '회원'이 아닌 '트레이너' 확인 여부
2839
2940 TrainerAddress newAddress = TrainerAddressMapper .toDomain (request .getAddress ());
3041
42+ Coordinates coordinates = newAddress .getCoordinates ();
43+
44+ if (coordinates .isNull ()) {
45+ throw new ApiException (TrainerAddressErrorCode .COORDINATES_NULL );
46+ }
47+ if (coordinates .isInvalidLatitude ()) {
48+ throw new ApiException (TrainerAddressErrorCode .INVALID_LATITUDE );
49+ }
50+ if (coordinates .isInvalidLongitude ()) {
51+ throw new ApiException (TrainerAddressErrorCode .INVALID_LONGITUDE );
52+ }
53+
3154 KakaoGeoResponse response = kakaoMapService .requestCoordToAddress (
3255 newAddress .getCoordinates ().getLatitude (),
3356 newAddress .getCoordinates ().getLongitude ()
@@ -43,37 +66,89 @@ public void createProfile(TrainerProfileRequest request, Long userId) {
4366 throw new ApiException (TrainerAddressErrorCode .ADDRESS_COORDINATE_MISMATCH );
4467 }
4568
46- newAddress .completeAddress (
69+ TrainerAddress completedAddress = newAddress .withCompletedAddress (
4770 document .getAddress ().getAddressName (),
4871 document .getRoadAddress ().getZoneNo ()
4972 );
5073
51- TrainerAddress savedAddress = trainerAddressRepository .save (newAddress );
52-
53- TrainerProfile profile = TrainerProfile .of (
54- userId ,
55- savedAddress .getId (),
56- request .getProfileImg (),
57- request .getIntro (),
58- request .getCredit (),
59- request .getContactStartTime (),
60- request .getContactEndTime (),
61- request .getIsNamePublic ()
62- );
74+ TrainerAddress savedAddress = trainerAddressRepository .save (completedAddress );
6375
64- if (!profile .isProfileComplete ()) {
65- throw new ApiException (TrainerProfileErrorCode .PROFILE_INCOMPLETE );
66- }
76+ // name, nickname 임시
77+ TrainerProfile profile = TrainerProfileMapper .toDomain (request , userId , savedAddress .getId ());
6778
68- if (! profile .isContactTimePairValid ()) {
79+ if (profile .isInvalidContactTimePair ()) {
6980 throw new ApiException (TrainerProfileErrorCode .INVALID_CONTACT_TIME_PAIR );
7081 }
7182
72- if (! profile .isValidContatTimeRange ()) {
83+ if (profile .isInvalidContactTimeRange ()) {
7384 throw new ApiException (TrainerProfileErrorCode .INVALID_CONTACT_TIME_RANGE );
7485 }
7586
7687 trainerProfileRepository .save (profile );
7788 }
7889
90+ /**
91+ * 트레이너 프로필 수정
92+ * @param request 트레이너 프로필 요청 DTO
93+ * @param userId 로그인 사용자 id
94+ */
95+ public void updateProfile (@ Valid TrainerProfileRequest request , Long userId ) {
96+ TrainerProfileEntity entity = trainerProfileRepository .findEntityByUserId (userId )
97+ .orElseThrow (() -> new ApiException (TrainerProfileErrorCode .PROFILE_NOT_FOUND ));
98+
99+ // DB에 저장되어 있는 주소와 요청 주소가 같은지 확인
100+ TrainerAddress currentAddress = TrainerAddressMapper .toDomain (entity .getAddress ()); // 기존 주소
101+ TrainerAddress newAddress = TrainerAddressMapper .toDomain (request .getAddress ()); // 요청 주소
102+
103+ TrainerAddressEntity newAddressEntity = entity .getAddress ();
104+
105+ boolean isAddressChanged = !newAddress .equals (currentAddress );
106+
107+ if (isAddressChanged ) {
108+ KakaoGeoResponse response = kakaoMapService .requestCoordToAddress (
109+ newAddress .getCoordinates ().getLatitude (),
110+ newAddress .getCoordinates ().getLongitude ()
111+ );
112+
113+ KakaoGeoResponse .Document document = response .getDocuments ().get (0 );
114+
115+ if (document .getRoadAddress () == null ) {
116+ throw new ApiException (TrainerAddressErrorCode .ROAD_ADDRESS_NOT_FOUND );
117+ }
118+
119+ if (!newAddress .matchesRoadAddress (document .getRoadAddress ().getAddressName ())) {
120+ throw new ApiException (TrainerAddressErrorCode .ADDRESS_COORDINATE_MISMATCH );
121+ }
122+
123+ TrainerAddress completedAddress = newAddress .withCompletedAddress (
124+ document .getAddress ().getAddressName (),
125+ document .getRoadAddress ().getZoneNo ()
126+ );
127+
128+ newAddressEntity = trainerAddressRepository .saveForEntityReference (completedAddress ); // 영속 상태 반환
129+ }
130+
131+ // 나머지 트레이너 프로필 값 업데이트
132+ entity .updateIntro (request .getIntro ());
133+ entity .updateProfileImg (request .getProfileImg ());
134+ entity .updateCredit (request .getCredit ());
135+ entity .updateContactTime (request .getContactStartTime (), request .getContactEndTime ());
136+ entity .updateNamePublic (request .getIsNamePublic ());
137+
138+ if (isAddressChanged ) {
139+ entity .updateAddress (newAddressEntity );
140+ }
141+
142+ TrainerProfile domain = TrainerProfileMapper .toDomain (entity );
143+
144+ if (domain .isInvalidContactTimePair ()) {
145+ throw new ApiException (TrainerProfileErrorCode .INVALID_CONTACT_TIME_PAIR );
146+ }
147+
148+ if (domain .isInvalidContactTimeRange ()) {
149+ throw new ApiException (TrainerProfileErrorCode .INVALID_CONTACT_TIME_RANGE );
150+ }
151+
152+ }
153+
79154}
0 commit comments