Skip to content

Commit

Permalink
Merge pull request #124 from PawWithU/feat/119-patch-mypage-profile-api
Browse files Browse the repository at this point in the history
[Feature] 이동봉사자 마이페이지 프로필 수정 API 구현
  • Loading branch information
hojeong2747 authored Nov 19, 2023
2 parents c4594b6 + c0810a3 commit 1446da5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,5 +91,17 @@ public ResponseEntity<List<VolunteerGetMyBadgeResponse>> 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<Void> volunteerMyProfile(@AuthenticationPrincipal UserDetails loginUser, @RequestBody @Valid VolunteerMyProfileRequest volunteerMyProfileRequest) {
volunteerService.volunteerMyProfile(loginUser.getUsername(), volunteerMyProfileRequest);
return ResponseEntity.noContent().build();
}

}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -81,4 +83,16 @@ public List<VolunteerGetMyBadgeResponse> getMyBadges(String email) {
List<VolunteerGetMyBadgeResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;


Expand Down Expand Up @@ -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());

}
}

0 comments on commit 1446da5

Please sign in to comment.