Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#133-마이페이지_회원_정보_수정_기능을_제작한다 #159

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/main/java/doore/DooreApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ public class DooreApplication {
public static void main(String[] args) {
SpringApplication.run(DooreApplication.class, args);
}

}
12 changes: 11 additions & 1 deletion src/main/java/doore/member/api/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import doore.member.application.MemberCommandService;
import doore.member.application.MemberQueryService;
import doore.member.application.dto.request.MemberUpdateRequest;
import doore.member.application.dto.response.MemberAndMyTeamsAndStudiesResponse;
import doore.member.domain.Member;
import doore.resolver.LoginMember;
Expand All @@ -12,6 +13,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Validated
Expand Down Expand Up @@ -44,9 +46,17 @@ public ResponseEntity<Void> deleteMember(@LoginMember final Member member) {
return ResponseEntity.noContent().build();
}

@PatchMapping("/profile/members")
public ResponseEntity<Void> updateMyPage(
Comment on lines +49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

윗 코멘트 참고해주세요!

@RequestBody final MemberUpdateRequest memberUpdateRequest,
@LoginMember final Member member) {
memberCommandService.updateMyPage(member.getId(), memberUpdateRequest);
return ResponseEntity.noContent().build();
}

@GetMapping("/members/{memberId}")
public ResponseEntity<MemberAndMyTeamsAndStudiesResponse> getSideBarInfo(@PathVariable final Long memberId,
@LoginMember final Member member) {
@LoginMember final Member member) {
return ResponseEntity.ok(memberQueryService.getSideBarInfo(memberId, member.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static doore.team.exception.TeamExceptionType.NOT_FOUND_TEAM;

import doore.login.application.dto.response.GoogleAccountProfileResponse;
import doore.member.application.dto.request.MemberUpdateRequest;
import doore.member.domain.Member;
import doore.member.domain.StudyRole;
import doore.member.domain.TeamRole;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void deleteMember(final Long memberId) {
memberRepository.delete(member);
}

private Member validateExistMember(final Long memberId) {
public Member validateExistMember(final Long memberId) {
Comment on lines -92 to +93
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 때문에 public으로 바꾸신 것 같은데 저희는 각 service에서만 사용되는 메서드의 접근제어자는 private으로 설정합니다! 테스트는 테스트일 뿐인데 테스트를 위해 실제 코드의 접근을 public으로 여는 것은 위험하다고 생각해요~!

return memberRepository.findById(memberId).orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER));
}

Expand All @@ -100,4 +101,10 @@ private Team validateExistTeam(final Long teamId) {
private Study validateExistStudy(final Long studyId) {
return studyRepository.findById(studyId).orElseThrow(() -> new StudyException(NOT_FOUND_STUDY));
}

public void updateMyPage(final Long memberId, final MemberUpdateRequest memberUpdateRequest) {
Member member = validateExistMember(memberId);
Comment on lines +105 to +106
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분이 질문 주셨던 부분 같은데 이렇게 된다면 API를 요청한 사람이 본인인지는 어떻게 알까요??

member.updateName(memberUpdateRequest.newName());
memberRepository.save(member);
Comment on lines +107 to +108
Copy link
Collaborator

@JJimini JJimini Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굿굿! 추가로 member를 따로 저장해 줄 필요 없이 자동으로 변경감지가 된답니다~ 108번 줄은 삭제해도 될 것 같아요~!

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package doore.member.application.dto.request;

import lombok.Builder;

@Builder
public record MemberUpdateRequest(
String newName
Comment on lines +6 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 newName은 Null이 들어오면 어떻게 될까요?? Null이 들어올 수 없도록 처리해야 할 것 같아요~

) {
}
4 changes: 4 additions & 0 deletions src/main/java/doore/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ private Member(final Long id, final String name, final String googleId, final St
this.imageUrl = imageUrl;
this.isDeleted = false;
}

public void updateName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import doore.helper.IntegrationTest;
import doore.login.application.dto.response.GoogleAccountProfileResponse;
import doore.member.application.dto.request.MemberUpdateRequest;
import doore.member.domain.Member;
import doore.member.domain.StudyRole;
import doore.member.domain.TeamRole;
Expand Down Expand Up @@ -229,4 +230,35 @@ void init() {
memberCommandService.transferStudyLeader(study.getId(), member.getId(), notStudyLeaderMember.getId());
});
}

@Test
@DisplayName("[성공] 멤버 조회에 성공한다.")
void validateExistMember_멤버_조회에_성공한다() {
Member savedMember = memberCommandService.validateExistMember(
member.getId()
);

assertThat(savedMember).isEqualTo(member);
}
Comment on lines +234 to +242
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vallidateExistMember와 같은 메서드는 성공 테스트를 하지 않고, 실패 시 커스텀 오류 메시지가 나타나는 지만 테스트합니다~


@Test
@DisplayName("[실패] 멤버 조회에 실패한다.")
void validateExistMember_멤버_조회에_실패한다() {
assertThatThrownBy(() -> memberCommandService.validateExistMember(member.getId() + 1))
.isInstanceOf(MemberException.class)
.hasMessage(NOT_FOUND_MEMBER.errorMessage());
}
Comment on lines +244 to +250
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validatExistMember를 호출해서 테스트하는 것이 아니라, updateMyPage를 호출할 때 잘못된 memberId라면 멤버 조회 에러가 나는지 확인하면 될 것 같아요~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가로 코드의 가독성을 위해 잘못된 멤버 아이디는 invalidMemberId 와 같이 선언해서 표현합니다!


@Test
@DisplayName("[성공] 프로필 이름 수정에 성공한다.")
void updateMyPage_프로필_이름_수정에_성공한다() {
MemberUpdateRequest request = MemberUpdateRequest.builder()
.newName("요시")
.build();

memberCommandService.updateMyPage(member.getId(), request);

Member findMember = memberRepository.findById(member.getId()).orElseThrow();
assertThat(findMember.getName()).isEqualTo(request.newName());
}
Comment on lines +252 to +263
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

}
19 changes: 19 additions & 0 deletions src/test/java/doore/restdocs/docs/MemberApiDocsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import doore.member.application.dto.request.MemberUpdateRequest;
import doore.member.application.dto.response.MemberAndMyTeamsAndStudiesResponse;
import doore.restdocs.RestDocsTest;
import doore.study.application.dto.response.StudyNameResponse;
Expand Down Expand Up @@ -107,4 +108,22 @@ void setUp() {
.andExpect(status().isOk())
.andDo(document("get-sidebar-info", pathParameters, responseFieldsSnippet));
}

@Test
@DisplayName("[성공] 프로필 이름 수정에 성공한다.")
void updateMyPageName_프로필_이름_수정에_성공한다() throws Exception {

Comment on lines +114 to +115
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: 개행이 없어도 될 것 같아요~

MemberUpdateRequest request = new MemberUpdateRequest("요시");
String requestJson = objectMapper.writeValueAsString(request);

Comment on lines +117 to +118
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저희 asJsonString 이라는 메서드에 정의가 되어 있어서 따로 안쓰셔도 될 것 같아요!

doNothing().when(memberCommandService)
.updateMyPage(any(Long.class), any(MemberUpdateRequest.class));
Comment on lines +119 to +120
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatter가 깨진 것 같아요~ 그리고 any는 해당 방식처럼 사용하는 것보다 그냥 any()를 사용해주셔도 됩니다! anyLong을 했을 때 오류가 나서 이렇게 하신 것 같은데, anyLong은 문자 그대로 보면 래퍼클래스를 매칭할 것 같지만 기본 자료 타입만 매칭해서 그렇습니다!


mockMvc.perform(RestDocumentationRequestBuilders.patch("/profile/members", 1)
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson)
.header(HttpHeaders.AUTHORIZATION, accessToken))
.andExpect(status().isNoContent())
.andDo(document("update-my-page-name"));
}
}