-
Notifications
You must be signed in to change notification settings - Fork 4
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
(회원) 선호 카테고리 삭제 기능 구현 #79
Changes from 4 commits
f3e8263
f4b1f67
383519f
ee2f013
07b3288
e3a022d
f57dd51
b6949bb
4a82237
03dcd00
c6e04fe
7d3c827
8694859
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ public class CategoryService { | |
|
||
@Transactional(readOnly = true) | ||
public List<CategoryResponse> getAllCategories() { | ||
List<Category> categories = categoryRepository.findAll(); | ||
final List<Category> categories = categoryRepository.findAll(); | ||
|
||
return categories.stream() | ||
.map(CategoryResponse::new) | ||
|
@@ -29,18 +29,29 @@ public List<CategoryResponse> getAllCategories() { | |
|
||
@Transactional | ||
public void addFavoriteCategory(final Member member, final Long categoryId) { | ||
Category category = categoryRepository.findById(categoryId) | ||
final Category category = categoryRepository.findById(categoryId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다.")); | ||
|
||
memberCategoryRepository.findByMemberAndCategory(member, category) | ||
.ifPresent(ignore -> new IllegalStateException("이미 선호 카테고리에 등록되어 있습니다.")); | ||
|
||
MemberCategory memberCategory = MemberCategory.builder() | ||
final MemberCategory memberCategory = MemberCategory.builder() | ||
.member(member) | ||
.category(category) | ||
.build(); | ||
|
||
memberCategoryRepository.save(memberCategory); | ||
} | ||
|
||
@Transactional | ||
public void removeFavoriteCategory(final Member member, final Long categoryId) { | ||
final Category category = categoryRepository.findById(categoryId) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다.")); | ||
|
||
final MemberCategory memberCategory = memberCategoryRepository.findByMemberAndCategory(member, category) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리는 선호 카테고리가 아닙니다.")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어 좋아요~ 수정하겠습니다 |
||
|
||
memberCategoryRepository.delete(memberCategory); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,79 @@ | ||||||
package com.votogether.domain.category.contorller; | ||||||
|
||||||
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 io.restassured.module.mockmvc.RestAssuredMockMvc; | ||||||
import java.util.List; | ||||||
import org.hamcrest.Matchers; | ||||||
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.http.HttpStatus; | ||||||
|
||||||
@WebMvcTest(CategoryController.class) | ||||||
class CategoryControllerTest { | ||||||
|
||||||
@MockBean | ||||||
CategoryService categoryService; | ||||||
|
||||||
@BeforeEach | ||||||
void setUp() { | ||||||
RestAssuredMockMvc.standaloneSetup(new CategoryController(categoryService)); | ||||||
} | ||||||
|
||||||
@Test | ||||||
@DisplayName("전체 카테고리 목록을 조회한다.") | ||||||
void getAllCategories() { | ||||||
// given | ||||||
Category category = Category.builder() | ||||||
.name("개발") | ||||||
.build(); | ||||||
given(categoryService.getAllCategories()).willReturn(List.of(new CategoryResponse(category))); | ||||||
|
||||||
// when | ||||||
RestAssuredMockMvc. | ||||||
given().log().all() | ||||||
.when().get("/categories/guest") | ||||||
.then().log().all() | ||||||
.status(HttpStatus.OK) | ||||||
.body("[0].id", Matchers.nullValue()) | ||||||
.body("[0].name", Matchers.equalTo("개발")) | ||||||
.body("[0].isFavorite", Matchers.equalTo(false)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
} | ||||||
|
||||||
@Test | ||||||
@DisplayName("선호하는 카테고리를 선호 카테고리 목록에 추가한다.") | ||||||
void addFavoriteCategory() { | ||||||
// given | ||||||
doNothing().when(categoryService).addFavoriteCategory(any(), any()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 목 객체 서비스가 해당 메서드를 실행하면 아무것도 응답하지 않는 역할을 합니다 |
||||||
|
||||||
// when & then | ||||||
RestAssuredMockMvc. | ||||||
given().log().all() | ||||||
.when().post("/categories/1/like") | ||||||
.then().log().all() | ||||||
.status(HttpStatus.CREATED); | ||||||
} | ||||||
|
||||||
@Test | ||||||
@DisplayName("선호 카테고리를 삭제한다.") | ||||||
void removeFavoriteCategory() { | ||||||
// given | ||||||
doNothing().when(categoryService).removeFavoriteCategory(any(), any()); | ||||||
|
||||||
// when & then | ||||||
RestAssuredMockMvc. | ||||||
given().log().all() | ||||||
.when().delete("/categories/1/like") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.then().log().all() | ||||||
.status(HttpStatus.NO_CONTENT); | ||||||
} | ||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,133 @@ | ||||||||||
package com.votogether.domain.category.service; | ||||||||||
|
||||||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||||||
import static org.junit.jupiter.api.Assertions.assertAll; | ||||||||||
|
||||||||||
import com.votogether.domain.category.dto.response.CategoryResponse; | ||||||||||
import com.votogether.domain.category.entity.Category; | ||||||||||
import com.votogether.domain.category.repository.CategoryRepository; | ||||||||||
import com.votogether.domain.member.entity.Gender; | ||||||||||
import com.votogether.domain.member.entity.Member; | ||||||||||
import com.votogether.domain.member.entity.MemberCategory; | ||||||||||
import com.votogether.domain.member.entity.SocialType; | ||||||||||
import com.votogether.domain.member.repository.MemberCategoryRepository; | ||||||||||
import com.votogether.domain.member.repository.MemberRepository; | ||||||||||
import java.time.LocalDateTime; | ||||||||||
import java.util.List; | ||||||||||
import org.junit.jupiter.api.Assertions; | ||||||||||
import org.junit.jupiter.api.DisplayName; | ||||||||||
import org.junit.jupiter.api.Test; | ||||||||||
import org.springframework.beans.factory.annotation.Autowired; | ||||||||||
import org.springframework.boot.test.context.SpringBootTest; | ||||||||||
import org.springframework.transaction.annotation.Transactional; | ||||||||||
|
||||||||||
@SpringBootTest | ||||||||||
@Transactional | ||||||||||
class CategoryServiceTest { | ||||||||||
|
||||||||||
@Autowired | ||||||||||
CategoryService categoryService; | ||||||||||
|
||||||||||
@Autowired | ||||||||||
CategoryRepository categoryRepository; | ||||||||||
|
||||||||||
@Autowired | ||||||||||
MemberCategoryRepository memberCategoryRepository; | ||||||||||
|
||||||||||
@Autowired | ||||||||||
MemberRepository memberRepository; | ||||||||||
|
||||||||||
@Test | ||||||||||
@DisplayName("모든 카테고리를 가져온다.") | ||||||||||
void getAllCategories() { | ||||||||||
// given | ||||||||||
Category category = Category.builder() | ||||||||||
.name("개발") | ||||||||||
.build(); | ||||||||||
|
||||||||||
categoryRepository.save(category); | ||||||||||
|
||||||||||
// when | ||||||||||
List<CategoryResponse> categories = categoryService.getAllCategories(); | ||||||||||
|
||||||||||
// then | ||||||||||
assertAll( | ||||||||||
() -> assertThat(categories.get(0).id()).isNotNull(), | ||||||||||
() -> assertThat(categories.get(0).name()).isEqualTo("개발"), | ||||||||||
() -> assertThat(categories.get(0).isFavorite()).isFalse() | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
@DisplayName("선호하는 카테고리를 선호 카테고리 목록에 추가한다.") | ||||||||||
void addFavoriteCategory() { | ||||||||||
// given | ||||||||||
Category category = Category.builder() | ||||||||||
.name("개발") | ||||||||||
.build(); | ||||||||||
|
||||||||||
Member member = Member.builder() | ||||||||||
.gender(Gender.MALE) | ||||||||||
.point(0) | ||||||||||
.socialType(SocialType.GOOGLE) | ||||||||||
.nickname("user1") | ||||||||||
.socialId("kakao@gmail.com") | ||||||||||
.birthDate( | ||||||||||
LocalDateTime.of(1995, 7, 12, 0, 0)) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
.build(); | ||||||||||
|
||||||||||
categoryRepository.save(category); | ||||||||||
memberRepository.save(member); | ||||||||||
|
||||||||||
Long categoryId = category.getId(); | ||||||||||
|
||||||||||
// when | ||||||||||
categoryService.addFavoriteCategory(member, categoryId); | ||||||||||
|
||||||||||
// then | ||||||||||
MemberCategory memberCategory = memberCategoryRepository.findByMemberAndCategory(member, category).get(); | ||||||||||
|
||||||||||
Assertions.assertAll( | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
() -> assertThat(memberCategory.getMember()).isSameAs(member), | ||||||||||
() -> assertThat(memberCategory.getCategory()).isSameAs(category) | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
@DisplayName("선호하는 카테고리를 선호 카테고리 목록에 삭제한다.") | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
void removeFavoriteCategory() { | ||||||||||
// given | ||||||||||
Category category = Category.builder() | ||||||||||
.name("개발") | ||||||||||
.build(); | ||||||||||
|
||||||||||
Member member = Member.builder() | ||||||||||
.gender(Gender.MALE) | ||||||||||
.point(0) | ||||||||||
.socialType(SocialType.GOOGLE) | ||||||||||
.nickname("user1") | ||||||||||
.socialId("kakao@gmail.com") | ||||||||||
.birthDate( | ||||||||||
LocalDateTime.of(1995, 7, 12, 0, 0)) | ||||||||||
.build(); | ||||||||||
|
||||||||||
MemberCategory memberCategory = MemberCategory.builder() | ||||||||||
.member(member) | ||||||||||
.category(category) | ||||||||||
.build(); | ||||||||||
|
||||||||||
categoryRepository.save(category); | ||||||||||
memberRepository.save(member); | ||||||||||
memberCategoryRepository.save(memberCategory); | ||||||||||
|
||||||||||
Long categoryId = category.getId(); | ||||||||||
|
||||||||||
// when | ||||||||||
categoryService.removeFavoriteCategory(member, categoryId); | ||||||||||
|
||||||||||
// then | ||||||||||
boolean isEmpty = memberCategoryRepository.findByMemberAndCategory(member, category).isEmpty(); | ||||||||||
assertThat(isEmpty).isTrue(); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2
: 해당 카테고리가 존재하지 않을 때처럼 예외 상황에 대한 응답도 설명하는게 좋을 것 같아요 :)