diff --git a/src/main/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerController.java b/src/main/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerController.java index d0f1a40f..abc423c2 100644 --- a/src/main/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerController.java +++ b/src/main/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerController.java @@ -2,6 +2,7 @@ import com.pawwithu.connectdog.domain.volunteer.dto.request.AdditionalAuthRequest; import com.pawwithu.connectdog.domain.volunteer.dto.request.NicknameRequest; +import com.pawwithu.connectdog.domain.volunteer.dto.request.VolunteerMyProfileRequest; import com.pawwithu.connectdog.domain.volunteer.dto.response.NicknameResponse; import com.pawwithu.connectdog.domain.volunteer.dto.response.VolunteerGetMyBadgeResponse; import com.pawwithu.connectdog.domain.volunteer.dto.response.VolunteerGetMyBookmarkResponse; @@ -90,5 +91,17 @@ public ResponseEntity> getMyBadges(@Authentica return ResponseEntity.ok(response); } + @Operation(summary = "마이페이지 프로필 수정", description = "마이페이지 프로필을 수정합니다.", + security = { @SecurityRequirement(name = "bearer-key") }, + responses = {@ApiResponse(responseCode = "204", description = "마이페이지 프로필 수정 성공") + , @ApiResponse(responseCode = "400" + , description = "V1, 닉네임은 한글, 숫자만 사용 가능합니다. \t\n V1, 닉네임은 필수 입력 값입니다. \t\n V1, 닉네임은 2~10자로 입력해 주세요. \t\n A2, 이미 사용 중인 닉네임입니다. \t\n M1, 해당 이동봉사자를 찾을 수 없습니다." + , content = @Content(schema = @Schema(implementation = ErrorResponse.class))) + }) + @PatchMapping("/my/profile") + public ResponseEntity volunteerMyProfile(@AuthenticationPrincipal UserDetails loginUser, @RequestBody @Valid VolunteerMyProfileRequest volunteerMyProfileRequest) { + volunteerService.volunteerMyProfile(loginUser.getUsername(), volunteerMyProfileRequest); + return ResponseEntity.noContent().build(); + } } diff --git a/src/main/java/com/pawwithu/connectdog/domain/volunteer/dto/request/VolunteerMyProfileRequest.java b/src/main/java/com/pawwithu/connectdog/domain/volunteer/dto/request/VolunteerMyProfileRequest.java new file mode 100644 index 00000000..82b2a882 --- /dev/null +++ b/src/main/java/com/pawwithu/connectdog/domain/volunteer/dto/request/VolunteerMyProfileRequest.java @@ -0,0 +1,14 @@ +package com.pawwithu.connectdog.domain.volunteer.dto.request; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +public record VolunteerMyProfileRequest( + @Pattern(regexp = "^[가-힣0-9]*$", message = "닉네임은 한글, 숫자만 사용 가능합니다.") + @NotBlank(message = "닉네임은 필수 입력 값입니다.") + @Size(min=2, max=10, message = "닉네임은 2~10자로 입력해 주세요.") + String nickname, + Integer profileImageNum +) { +} diff --git a/src/main/java/com/pawwithu/connectdog/domain/volunteer/entity/Volunteer.java b/src/main/java/com/pawwithu/connectdog/domain/volunteer/entity/Volunteer.java index d07750f4..a90ae365 100644 --- a/src/main/java/com/pawwithu/connectdog/domain/volunteer/entity/Volunteer.java +++ b/src/main/java/com/pawwithu/connectdog/domain/volunteer/entity/Volunteer.java @@ -63,4 +63,9 @@ public void updateNameAndPhone(String name, String phone) { this.name = name; this.phone = phone; } + + public void updateMyProfile(String nickname, Integer profileImageNum) { + this.nickname = nickname; + this.profileImageNum = profileImageNum; + } } diff --git a/src/main/java/com/pawwithu/connectdog/domain/volunteer/service/VolunteerService.java b/src/main/java/com/pawwithu/connectdog/domain/volunteer/service/VolunteerService.java index 00d8e3c2..9f7b445f 100644 --- a/src/main/java/com/pawwithu/connectdog/domain/volunteer/service/VolunteerService.java +++ b/src/main/java/com/pawwithu/connectdog/domain/volunteer/service/VolunteerService.java @@ -7,6 +7,7 @@ import com.pawwithu.connectdog.domain.review.repository.CustomReviewRepository; import com.pawwithu.connectdog.domain.volunteer.dto.request.AdditionalAuthRequest; import com.pawwithu.connectdog.domain.volunteer.dto.request.NicknameRequest; +import com.pawwithu.connectdog.domain.volunteer.dto.request.VolunteerMyProfileRequest; import com.pawwithu.connectdog.domain.volunteer.dto.response.NicknameResponse; import com.pawwithu.connectdog.domain.volunteer.dto.response.VolunteerGetMyBadgeResponse; import com.pawwithu.connectdog.domain.volunteer.dto.response.VolunteerGetMyBookmarkResponse; @@ -22,6 +23,7 @@ import java.util.List; +import static com.pawwithu.connectdog.error.ErrorCode.ALREADY_EXIST_NICKNAME; import static com.pawwithu.connectdog.error.ErrorCode.VOLUNTEER_NOT_FOUND; @Slf4j @@ -81,4 +83,16 @@ public List getMyBadges(String email) { List badges = customVolunteerBadgeRepository.getMyBadges(volunteer.getId()); return badges; } + + public void volunteerMyProfile(String email, VolunteerMyProfileRequest volunteerMyProfileRequest) { + Volunteer volunteer = volunteerRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND)); + + if (volunteerRepository.existsByNickname(volunteerMyProfileRequest.nickname())) { + throw new BadRequestException(ALREADY_EXIST_NICKNAME); + } + + String nickname = volunteerMyProfileRequest.nickname(); + Integer profileImageNum = volunteerMyProfileRequest.profileImageNum(); + volunteer.updateMyProfile(nickname, profileImageNum); + } } diff --git a/src/test/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerControllerTest.java b/src/test/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerControllerTest.java index 0505567f..26cb2737 100644 --- a/src/test/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerControllerTest.java +++ b/src/test/java/com/pawwithu/connectdog/domain/volunteer/controller/VolunteerControllerTest.java @@ -1,9 +1,11 @@ package com.pawwithu.connectdog.domain.volunteer.controller; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.pawwithu.connectdog.domain.post.dto.response.PostGetHomeResponse; import com.pawwithu.connectdog.domain.volunteer.dto.request.AdditionalAuthRequest; import com.pawwithu.connectdog.domain.volunteer.dto.request.NicknameRequest; +import com.pawwithu.connectdog.domain.volunteer.dto.request.VolunteerMyProfileRequest; import com.pawwithu.connectdog.domain.volunteer.dto.response.NicknameResponse; import com.pawwithu.connectdog.domain.volunteer.dto.response.VolunteerGetMyBadgeResponse; import com.pawwithu.connectdog.domain.volunteer.dto.response.VolunteerGetMyBookmarkResponse; @@ -30,8 +32,7 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -145,4 +146,22 @@ void setUp() { result.andExpect(status().isOk()); verify(volunteerService, times(1)).getMyBadges(any()); } + + @Test + void 이동봉사자_마이페이지_프로필_수정() throws Exception { + // given + VolunteerMyProfileRequest request = new VolunteerMyProfileRequest("하노짱", 2); + + // when + ResultActions result = mockMvc.perform( + patch("/volunteers/my/profile") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request)) + ); + + // then + result.andExpect(status().isNoContent()); + verify(volunteerService, times(1)).volunteerMyProfile(anyString(), any()); + + } } \ No newline at end of file