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

[BE] feat: 회원 정보 조회 API 구현 및 문서화 #182

Merged
merged 1 commit into from
Jul 23, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.woowacourse.friendogly.member.controller;

import com.woowacourse.friendogly.auth.Auth;
import com.woowacourse.friendogly.common.ApiResponse;
import com.woowacourse.friendogly.member.dto.request.SaveMemberRequest;
import com.woowacourse.friendogly.member.dto.response.FindMemberResponse;
import com.woowacourse.friendogly.member.dto.response.SaveMemberResponse;
import com.woowacourse.friendogly.member.service.MemberCommandService;
import com.woowacourse.friendogly.member.service.MemberQueryService;
import jakarta.validation.Valid;
import java.net.URI;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -16,9 +21,11 @@
@RequestMapping("/members")
public class MemberController {

private final MemberQueryService memberQueryService;
private final MemberCommandService memberCommandService;

public MemberController(MemberCommandService memberCommandService) {
public MemberController(MemberQueryService memberQueryService, MemberCommandService memberCommandService) {
this.memberQueryService = memberQueryService;
this.memberCommandService = memberCommandService;
}

Expand All @@ -28,4 +35,16 @@ public ResponseEntity<ApiResponse<SaveMemberResponse>> saveMember(@RequestBody @
return ResponseEntity.created(URI.create("/members/" + response.id()))
.body(ApiResponse.ofSuccess(response));
}

@GetMapping("/mine")
public ApiResponse<FindMemberResponse> findMine(@Auth Long memberId) {
FindMemberResponse response = memberQueryService.findById(memberId);
return ApiResponse.ofSuccess(response);
}

@GetMapping("/{id}")
public ApiResponse<FindMemberResponse> findById(@PathVariable Long id) {
FindMemberResponse response = memberQueryService.findById(id);
return ApiResponse.ofSuccess(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.woowacourse.friendogly.member.dto.response;

import com.woowacourse.friendogly.member.domain.Member;

public record FindMemberResponse(
Long id,
String name,
String tag,
String email
) {

public FindMemberResponse(Member member) {
this(
member.getId(),
member.getName().getValue(),
member.getTag(),
member.getEmail().getValue()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package com.woowacourse.friendogly.member.service;

import com.woowacourse.friendogly.exception.FriendoglyException;
import com.woowacourse.friendogly.member.domain.Member;
import com.woowacourse.friendogly.member.dto.response.FindMemberResponse;
import com.woowacourse.friendogly.member.repository.MemberRepository;
import org.springframework.stereotype.Service;

@Service
public class MemberQueryService {

private final MemberRepository memberRepository;

public MemberQueryService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

public FindMemberResponse findById(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new FriendoglyException("존재하지 않는 회원입니다."));
return new FindMemberResponse(member);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.woowacourse.friendogly.docs;

import static com.epages.restdocs.apispec.ResourceDocumentation.headerWithName;
import static com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName;
import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper;
import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.epages.restdocs.apispec.Schema;
import com.woowacourse.friendogly.auth.AuthArgumentResolver;
import com.woowacourse.friendogly.member.controller.MemberController;
import com.woowacourse.friendogly.member.dto.request.SaveMemberRequest;
import com.woowacourse.friendogly.member.dto.response.FindMemberResponse;
import com.woowacourse.friendogly.member.dto.response.SaveMemberResponse;
import com.woowacourse.friendogly.member.service.MemberCommandService;
import com.woowacourse.friendogly.member.service.MemberQueryService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.restdocs.payload.JsonFieldType;
Expand All @@ -30,6 +37,9 @@ public class MemberApiDocsTest extends RestDocsTest {
@MockBean
private MemberQueryService memberQueryService;

@Autowired
private AuthArgumentResolver authArgumentResolver;

Comment on lines +40 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

이건 뭔가요?! 궁금해서 ㅎㅎ,,

Copy link
Contributor

Choose a reason for hiding this comment

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

(이해 완)

@DisplayName("회원 생성 문서화")
@Test
void saveMember_Success() throws Exception {
Expand Down Expand Up @@ -57,17 +67,94 @@ void saveMember_Success() throws Exception {
fieldWithPath("isSuccess").type(JsonFieldType.BOOLEAN).description("요청 성공 여부"),
fieldWithPath("data.id").type(JsonFieldType.NUMBER).description("회원 id"),
fieldWithPath("data.name").type(JsonFieldType.STRING).description("회원 이름"),
fieldWithPath("data.tag").type(JsonFieldType.STRING)
.description("중복된 회원 이름을 식별하기 위한 고유한 문자열"),
fieldWithPath("data.tag").type(JsonFieldType.STRING).description("중복된 회원 이름을 식별하기 위한 고유한 문자열"),
fieldWithPath("data.email").type(JsonFieldType.STRING).description("회원 이메일"))
.requestSchema(Schema.schema("saveMemberRequest"))
.responseSchema(Schema.schema("응답DTO 이름"))
.build()))
);
}

@DisplayName("회원 단건 조회 문서화")
@Test
void findById_Success() throws Exception {
Long memberId = 1L;
FindMemberResponse response = new FindMemberResponse(
memberId,
"땡이",
"ugab3odb",
"ddang@email.com"
);

Mockito.when(memberQueryService.findById(any()))
.thenReturn(response);

mockMvc.perform(RestDocumentationRequestBuilders.get("/members/{id}", memberId)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(MockMvcRestDocumentationWrapper.document("member-findById-200",
getDocumentRequest(),
getDocumentResponse(),
resource(ResourceSnippetParameters.builder()
.tag("Member API")
.summary("회원 단건 조회 API")
.pathParameters(
parameterWithName("id").description("조회하려는 회원 id")
)
.responseFields(
fieldWithPath("isSuccess").type(JsonFieldType.BOOLEAN).description("요청 성공 여부"),
fieldWithPath("data.id").type(JsonFieldType.NUMBER).description("회원 id"),
fieldWithPath("data.name").type(JsonFieldType.STRING).description("회원 이름"),
fieldWithPath("data.tag").type(JsonFieldType.STRING).description("회원 고유 식별자"),
fieldWithPath("data.email").type(JsonFieldType.STRING).description("회원 이메일")
)
.requestSchema(Schema.schema("FindMemberByIdRequest"))
.responseSchema(Schema.schema("FindMemberResponse"))
.build()))
);
}

@DisplayName("내 회원 정보 조회 문서화")
@Test
void findMine_Success() throws Exception {
Long loginMemberId = 1L;
FindMemberResponse response = new FindMemberResponse(
loginMemberId,
"땡이",
"ugab3odb",
"ddang@email.com"
);

Mockito.when(memberQueryService.findById(any()))
.thenReturn(response);

mockMvc.perform(RestDocumentationRequestBuilders.get("/members/mine")
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, loginMemberId.toString()))
.andExpect(status().isOk())
.andDo(MockMvcRestDocumentationWrapper.document("member-findMine-200",
getDocumentRequest(),
getDocumentResponse(),
resource(ResourceSnippetParameters.builder()
.tag("Member API")
.summary("내 회원 정보 조회 API")
.requestHeaders(
headerWithName("Authorization").description("로그인한 회원 id")
)
.responseFields(
fieldWithPath("isSuccess").type(JsonFieldType.BOOLEAN).description("요청 성공 여부"),
fieldWithPath("data.id").type(JsonFieldType.NUMBER).description("회원 id"),
fieldWithPath("data.name").type(JsonFieldType.STRING).description("회원 이름"),
fieldWithPath("data.tag").type(JsonFieldType.STRING).description("회원 고유 식별자"),
fieldWithPath("data.email").type(JsonFieldType.STRING).description("회원 이메일")
)
.responseSchema(Schema.schema("FindMemberResponse"))
.build()))
);
}

@Override
protected Object controller() {
return new MemberController(memberCommandService);
return new MemberController(memberQueryService, memberCommandService);
}
}
Loading