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

사용자 id 로 사용자의 정보를 받아오는 기능 추가 #811

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -39,9 +34,7 @@ public void checkUniquename(@RequestParam String name) {
}

@GetMapping("/members/{id}")
public ResponseEntity<FindMemberResponse> findMember(
@AuthenticationPrinciple Member member, @PathVariable Long id
) {
return ResponseEntity.ok(memberService.findMember(member, id));
public ResponseEntity<FindMemberResponse> findMember(@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> findMember(Long id);
}
13 changes: 2 additions & 11 deletions backend/src/main/java/codezap/member/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package codezap.member.service;

import java.util.Objects;

import jakarta.transaction.Transactional;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -43,15 +41,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 @@ -57,7 +58,7 @@ void findMemberSuccess() 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()))
.andDo(print())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,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