Skip to content

Commit

Permalink
사용자 id 로 사용자의 정보를 받아오는 기능 추가 (#811)
Browse files Browse the repository at this point in the history
* docs: 문서 수정

* 충돌 해결

* style: 사용되지 않는 import 문 제거

* refactor: 엔드포인트를 명확하게 변경
  • Loading branch information
zangsu authored Oct 22, 2024
1 parent 412a045 commit f5a208e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package codezap.member.controller;

import java.net.URI;

import codezap.member.dto.request.SignupRequest;
import codezap.member.dto.response.FindMemberResponse;
import codezap.member.service.MemberService;
import jakarta.validation.Valid;

import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -14,13 +16,6 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import codezap.auth.configuration.AuthenticationPrinciple;
import codezap.member.domain.Member;
import codezap.member.dto.request.SignupRequest;
import codezap.member.dto.response.FindMemberResponse;
import codezap.member.service.MemberService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class MemberController implements SpringDocMemberController {
Expand All @@ -38,10 +33,8 @@ public void checkUniquename(@RequestParam String name) {
memberService.assertUniqueName(name);
}

@GetMapping("/members/{id}")
public ResponseEntity<FindMemberResponse> findMember(
@AuthenticationPrinciple Member member, @PathVariable Long id
) {
return ResponseEntity.ok(memberService.findMember(member, id));
@GetMapping("/members/{id}/name")
public ResponseEntity<FindMemberResponse> findMemberName(@PathVariable Long id) {
return ResponseEntity.ok(memberService.findMember(id));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package codezap.member.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import codezap.global.swagger.error.ApiErrorResponse;
import codezap.global.swagger.error.ErrorCase;
import codezap.member.domain.Member;
import codezap.member.dto.request.SignupRequest;
import codezap.member.dto.response.FindMemberResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@Tag(name = "회원 API", description = "회원 API")
public interface SpringDocMemberController {
Expand All @@ -39,14 +36,10 @@ public interface SpringDocMemberController {
})
void checkUniquename(@RequestParam String name);

@SecurityRequirement(name = "쿠키 인증 토큰")
@Operation(summary = "회원 정보 조회", description = "회원의 정보(아이디일, 닉네임)을 조회합니다.")
@Operation(summary = "회원 정보 조회", description = "회원의 정보(아이디)를 조회합니다.")
@ApiResponse(responseCode = "200", description = "회원 정보 조회 성공")
@ApiErrorResponse(status = HttpStatus.FORBIDDEN, instance = "/members/1", errorCases = {
@ErrorCase(description = "로그인된 회원이 조회하려는 회원이 아닌 경우", exampleMessage = "본인의 정보만 조회할 수 있습니다.")
})
@ApiErrorResponse(status = HttpStatus.NOT_FOUND, instance = "/members/1", errorCases = {
@ErrorCase(description = "조회하려는 id 값인 회원이 없는 경우", exampleMessage = "식별자 1에 해당하는 회원이 존재하지 않습니다.")
})
ResponseEntity<FindMemberResponse> findMember(Member member, Long id);
ResponseEntity<FindMemberResponse> findMemberName(Long id);
}
11 changes: 2 additions & 9 deletions backend/src/main/java/codezap/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ public void assertUniqueName(String name) {
}
}

public FindMemberResponse findMember(Member member, Long id) {
checkSameMember(member, id);
return FindMemberResponse.from(member);
}

private void checkSameMember(Member member, Long id) {
if (!Objects.equals(member.getId(), id)) {
throw new CodeZapException(ErrorCode.FORBIDDEN_ACCESS, "본인의 정보만 조회할 수 있습니다.");
}
public FindMemberResponse findMember(Long id) {
return FindMemberResponse.from(memberRepository.fetchById(id));
}

public Member getByTemplateId(Long templateId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codezap.member.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -53,13 +54,13 @@ void checkUniqueNameSuccess() throws Exception {

@Test
@DisplayName("회원 조회 성공")
void findMemberSuccess() throws Exception {
void findMemberNameSuccess() throws Exception {
Member member = MemberFixture.memberFixture();
FindMemberResponse response = FindMemberResponse.from(member);

when(memberService.findMember(any(), any())).thenReturn(response);
when(memberService.findMember(anyLong())).thenReturn(response);

mvc.perform(get("/members/" + member.getId()))
mvc.perform(get("/members/" + member.getId() + "/name"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value(member.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,10 @@ class FindMember {
void findMember() {
Member member = memberRepository.save(MemberFixture.memberFixture());

FindMemberResponse actual = memberService.findMember(member, member.getId());
FindMemberResponse actual = memberService.findMember(member.getId());

assertThat(actual).isEqualTo(FindMemberResponse.from(member));
}

@Test
@DisplayName("회원 ID로 멤버 조회 실패: 본인 ID가 아닌 경우")
void findMember_Throw() {
Member member = memberRepository.save(MemberFixture.memberFixture());
Long otherId = member.getId() + 1;

assertThatThrownBy(() -> memberService.findMember(member, otherId))
.isInstanceOf(CodeZapException.class)
.hasMessage("본인의 정보만 조회할 수 있습니다.");
}
}

@Nested
Expand Down

0 comments on commit f5a208e

Please sign in to comment.