File tree Expand file tree Collapse file tree 4 files changed +55
-4
lines changed
src/main/java/com/back/domain/profile Expand file tree Collapse file tree 4 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ dependencies {
3131
3232 implementation(" org.springframework.boot:spring-boot-starter-security" )
3333 implementation(" org.springframework.boot:spring-boot-starter-oauth2-client" )
34- implementation(" org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6 .0" )
34+ implementation(" org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7 .0" )
3535 implementation(" io.jsonwebtoken:jjwt-api:0.12.3" )
3636 runtimeOnly(" io.jsonwebtoken:jjwt-impl:0.12.3" )
3737 runtimeOnly(" io.jsonwebtoken:jjwt-jackson:0.12.3" )
Original file line number Diff line number Diff line change 11package com .back .domain .profile .controller ;
22
33import com .back .domain .profile .dto .ProfileResponseDto ;
4+ import com .back .domain .profile .dto .ProfileUpdateRequestDto ;
45import com .back .domain .profile .service .ProfileService ;
56import com .back .domain .user .service .UserService ;
67import com .back .global .rsData .RsData ;
8+ import jakarta .validation .Valid ;
79import lombok .RequiredArgsConstructor ;
810import org .springframework .security .core .annotation .AuthenticationPrincipal ;
9- import org .springframework .web .bind .annotation .GetMapping ;
10- import org .springframework .web .bind .annotation .RequestMapping ;
11- import org .springframework .web .bind .annotation .RestController ;
11+ import org .springframework .web .bind .annotation .*;
1212
1313@ RestController
1414@ RequestMapping ("/me/profile" )
@@ -23,4 +23,10 @@ public RsData<ProfileResponseDto> getProfile(@AuthenticationPrincipal(expression
2323 ProfileResponseDto body = profileService .getProfile (userId );
2424 return RsData .successOf (body ); // code=200, message="success"
2525 }
26+
27+ @ PutMapping
28+ public RsData <ProfileResponseDto > updateProfile (@ AuthenticationPrincipal (expression = "id" ) Long userId , @ Valid @ RequestBody ProfileUpdateRequestDto profileUpdateRequestDto ) {
29+ ProfileResponseDto body = profileService .updateProfile (userId , profileUpdateRequestDto );
30+ return RsData .successOf (body ); // code=200
31+ }
2632}
Original file line number Diff line number Diff line change 1+ package com .back .domain .profile .dto ;
2+
3+ import jakarta .validation .constraints .Email ;
4+ import jakarta .validation .constraints .Size ;
5+ import lombok .Getter ;
6+ import lombok .Setter ;
7+
8+ @ Getter
9+ @ Setter
10+ public class ProfileUpdateRequestDto {
11+
12+ @ Size (min = 1 , max = 10 , message = "닉네임은 1~10자" )
13+ private String nickname ;
14+
15+ @ Email (message = "이메일 형식이 아닙니다" )
16+ private String email ;
17+ }
Original file line number Diff line number Diff line change 11package com .back .domain .profile .service ;
22
33import com .back .domain .profile .dto .ProfileResponseDto ;
4+ import com .back .domain .profile .dto .ProfileUpdateRequestDto ;
45import com .back .domain .user .entity .User ;
56import com .back .domain .user .repository .UserRepository ;
67import com .back .domain .user .support .AbvView ;
@@ -31,4 +32,31 @@ public ProfileResponseDto getProfile(Long id) {
3132 .abvLabel (label )
3233 .build ();
3334 }
35+
36+ @ Transactional
37+ public ProfileResponseDto updateProfile (Long id , ProfileUpdateRequestDto profileUpdateRequestDto ) {
38+ User user = userRepository .findById (id ).orElseThrow (() -> new ServiceException (404 , "사용자를 찾을 수 없습니다." ));
39+
40+ if (profileUpdateRequestDto .getNickname () != null ) {
41+ String nickname = profileUpdateRequestDto .getNickname ().trim ();
42+ if (nickname .isEmpty () || nickname .length () > 10 ) {
43+ throw new ServiceException (400 , "닉네임은 1~10자" );
44+ }
45+
46+ if (userRepository .existsByNicknameAndIdNot (nickname , id )) {
47+ throw new ServiceException (409 , "이미 사용중인 닉네임" );
48+ }
49+
50+ user .setNickname (nickname );
51+ }
52+
53+ if (profileUpdateRequestDto .getEmail () != null ) {
54+ String email = profileUpdateRequestDto .getEmail ().trim ();
55+ user .setEmail (email .isEmpty () ? null : email );
56+ }
57+
58+ userRepository .save (user );
59+
60+ return getProfile (id );
61+ }
3462}
You can’t perform that action at this time.
0 commit comments