Skip to content

Commit

Permalink
(회원) 전체 카테고리 목록 조회 기능 구현 (#89)
Browse files Browse the repository at this point in the history
* feat: (#67) (회원) 선호 카테고리 삭제 API 기능 구현

* feat: (#67) Swagger 어노테이션 추가

* test: (#67) Controller 단위테스트, Service 통합테스트 추가

- 이전에 누락된 테스트 코드까지 추가함

* style: (#67) final 키워드 추가

* refactor: (#68) CategoryResponse 파라미터 값 수정

* feat: (#68) (회원) 카테고리 목록 전체 조회 API 추가

* teat: (#68) (회원) 레파지토리 테스트 추가

* teat: (#67) 선호하는 카테고리에 없는 카테고리를 삭제하는 경우 예외 테스트 추가

* refactor: (#67) 개행 및 스태틱 임포트 리펙터링

* feat: (#67) Swagger 어노테이션 에러 응답 설명 추가

* fix: (#68) 파라미터 반환값 수정

* test: (#68) 서비스, 컨트롤러 테스트 추가

* refactor: (#67) url 오타 수정

* refactor: (#67) 개행 및 컨벤션 수정

* feat: (#68) Swagger 어노테이션 추가

* style: (#68) 개행 삭제

* style: (#68) 개행 수정

* refactor: (#68) @nested 삭제

- 카테고리를 조회하는 기능에서 보면 같으나 멤버, 비회원으로 나누어진 메서드므로 해당 어노테이션을 제거함

* refactor: (#68) 들여쓰기 제거

* refactor: (#68) @param 어노테이션 추가

- 다즐로컬에서는 테스트가 잘 작동했는데 루쿠로컬에서는 테스트가 작동하지 않는 오류가 발생했음. 안전하게 어노테이션 추가함.

* refactor: (#68) 시크릿 키 디코딩 제거
  • Loading branch information
aiaiaiai1 authored Jul 20, 2023
1 parent 78750b5 commit 179f202
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CategoryController {

private final CategoryService categoryService;

@Operation(summary = "카테고리 조회하기", description = "전체 카테고리 목록을 조회한다.")
@Operation(summary = "카테고리 목록 조회하기", description = "전체 카테고리 목록을 조회한다.")
@ApiResponse(responseCode = "200", description = "조회 성공")
@GetMapping("/guest")
public ResponseEntity<List<CategoryResponse>> getAllCategories() {
Expand Down Expand Up @@ -58,10 +58,12 @@ public ResponseEntity<Void> removeFavoriteCategory(final Member member, @PathVar
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@Operation(summary = "회원으로 모든 카테고리 목록 조회하기", description = "회원의 선호하는 카테고리와 전체 카테고리 목록을 조회한다.")
@ApiResponse(responseCode = "200", description = "조회 성공")
@GetMapping
public ResponseEntity<Void> getAllCategories(final Member member) {
categoryService.getAllCategories(member);
return ResponseEntity.status(HttpStatus.OK).build();
public ResponseEntity<List<CategoryResponse>> getAllCategories(final Member member) {
final List<CategoryResponse> categories = categoryService.getAllCategories(member);
return ResponseEntity.status(HttpStatus.OK).body(categories);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface VoteRepository extends JpaRepository<Vote, Long> {

Expand All @@ -17,7 +18,7 @@ public interface VoteRepository extends JpaRepository<Vote, Long> {
" WHERE v.postOption.id = :postOptionId" +
" GROUP BY m.ageRange, m.gender"
)
List<VoteStatus> findVoteCountByPostOptionIdGroupByAgeRangeAndGender(Long postOptionId);
List<VoteStatus> findVoteCountByPostOptionIdGroupByAgeRangeAndGender(@Param("postOptionId") Long postOptionId);

Optional<Vote> findByMemberAndPostOption(final Member member, final PostOption postOption);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -36,7 +38,7 @@ public TokenProcessor(
@Value("${jwt.token.expiration-time}") final int tokenExpirationTime,
final ObjectMapper objectMapper
) {
this.key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretKey));
this.key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));
this.tokenExpirationTime = tokenExpirationTime;
this.objectMapper = objectMapper;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.votogether.domain.category.contorller;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;

import com.votogether.domain.category.dto.response.CategoryResponse;
import com.votogether.domain.category.entity.Category;
import com.votogether.domain.category.service.CategoryService;
import com.votogether.domain.member.service.MemberService;
import com.votogether.global.jwt.TokenProcessor;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatus;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;

@WebMvcTest(CategoryController.class)
class CategoryControllerTest {

Expand Down Expand Up @@ -57,6 +60,39 @@ void getAllCategories() {
.body("[0].isFavorite", equalTo(false));
}

@Test
@DisplayName("회원으로 전체 카테고리 목록을 조회한다.")
void getAllCategoriesFromMember() {
// given
Category category = Category.builder()
.name("개발")
.build();

Category category1 = Category.builder()
.name("음식")
.build();

List<CategoryResponse> categoryResponses = List.of(
new CategoryResponse(category, false),
new CategoryResponse(category1, true)
);

given(categoryService.getAllCategories(any())).willReturn(categoryResponses);

// when
List<CategoryResponse> results = RestAssuredMockMvc
.given().log().all()
.when().get("/categories")
.then().log().all()
.status(HttpStatus.OK)
.extract()
.as(new ParameterizedTypeReference<List<CategoryResponse>>() {
}.getType());

// then
assertThat(results).usingRecursiveComparison().isEqualTo(categoryResponses);
}

@Test
@DisplayName("선호하는 카테고리를 선호 카테고리 목록에 추가한다.")
void addFavoriteCategory() {
Expand Down

0 comments on commit 179f202

Please sign in to comment.