Skip to content

Commit

Permalink
Merge pull request #133 from Shimpyo-House/feature/favorite-refactor
Browse files Browse the repository at this point in the history
refactor: 즐겨찾기 도메인 리팩토링
  • Loading branch information
JeongUijeong authored Dec 14, 2023
2 parents 78eac9f + 47a94d0 commit 5474d98
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.fc.shimpyo_be.global.common.ResponseDto;
import com.fc.shimpyo_be.global.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
Expand All @@ -20,35 +19,33 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/favorites")
public class FavoriteRestController {

private final FavoriteService favoriteService;
private final SecurityUtil securityUtil;
private static final int DEFAULT_SIZE = 10;
private static final int DEFAULT_PAGE = 0;

@PostMapping("/{productId}")
public ResponseEntity<ResponseDto<FavoriteResponseDto>> register(@PathVariable long productId) {
log.debug("memberId: {}, productId: {}", securityUtil.getCurrentMemberId(), productId);
return ResponseEntity.status(HttpStatus.CREATED).body(ResponseDto.res(HttpStatus.CREATED,
favoriteService.register(securityUtil.getCurrentMemberId(), productId),
"성공적으로 즐겨찾기를 등록했습니다."));
}

@GetMapping
public ResponseEntity<ResponseDto<FavoritesResponseDto>> getFavorites(
@PageableConstraint(Favorite.class) @PageableDefault(size = 10, page = 0) Pageable pageable) {
log.debug("memberId: {}", securityUtil.getCurrentMemberId());
@PageableConstraint(Favorite.class) @PageableDefault(size = DEFAULT_SIZE, page = DEFAULT_PAGE) Pageable pageable) {
return ResponseEntity.status(HttpStatus.OK)
.body(ResponseDto.res(HttpStatus.OK, favoriteService.getFavorites(
securityUtil.getCurrentMemberId(), pageable), "성공적으로 즐겨찾기 목록을 조회했습니다."));
}

@DeleteMapping("/{productId}")
public ResponseEntity<ResponseDto<FavoriteResponseDto>> cancel(@PathVariable long productId) {
log.debug("memberId: {}, productId: {}", securityUtil.getCurrentMemberId(), productId);
return ResponseEntity.status(HttpStatus.OK).body(ResponseDto.res(HttpStatus.OK,
favoriteService.delete(securityUtil.getCurrentMemberId(), productId),
"성공적으로 즐겨찾기를 취소했습니다."));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.fc.shimpyo_be.domain.favorite.dto;

import com.fc.shimpyo_be.domain.favorite.entity.Favorite;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FavoriteResponseDto {

private Long favoriteId;
private Long memberId;
private Long productId;

@Builder
public FavoriteResponseDto(Long favoriteId, Long memberId, Long productId) {
private FavoriteResponseDto(Long favoriteId, Long memberId, Long productId) {
this.favoriteId = favoriteId;
this.memberId = memberId;
this.productId = productId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import com.fc.shimpyo_be.domain.product.dto.response.ProductResponse;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FavoritesResponseDto {

private int pageCount;
private List<ProductResponse> products;

@Builder
public FavoritesResponseDto(int pageCount, List<ProductResponse> products) {
private FavoritesResponseDto(int pageCount, List<ProductResponse> products) {
this.pageCount = pageCount;
this.products = products;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Favorite {
private Product product;

@Builder
public Favorite(Long id, Member member, Product product) {
private Favorite(Long id, Member member, Product product) {
this.id = id;
this.member = member;
this.product = product;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void register() throws Exception {
.build();

given(securityUtil.getCurrentMemberId()).willReturn(1L);
given(favoriteService.register(any(Long.TYPE), any(Long.TYPE))).willReturn(
favoriteResponseDto);
given(favoriteService.register(any(Long.TYPE), any(Long.TYPE)))
.willReturn(favoriteResponseDto);

// when then
mockMvc.perform(post("/api/favorites/{productId}", 1L))
Expand Down Expand Up @@ -141,8 +141,8 @@ void cancel() throws Exception {
.build();

given(securityUtil.getCurrentMemberId()).willReturn(1L);
given(favoriteService.delete(any(Long.TYPE), any(Long.TYPE))).willReturn(
favoriteResponseDto);
given(favoriteService.delete(any(Long.TYPE), any(Long.TYPE)))
.willReturn(favoriteResponseDto);

// when then
mockMvc.perform(delete("/api/favorites/{productId}", 1L))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fc.shimpyo_be.config.AbstractContainersSupport;
import com.fc.shimpyo_be.domain.favorite.dto.FavoriteResponseDto;
import com.fc.shimpyo_be.domain.favorite.dto.FavoritesResponseDto;
Expand Down Expand Up @@ -49,8 +48,6 @@ public class FavoriteRestControllerTest extends AbstractContainersSupport {
@MockBean
SecurityUtil securityUtil;

ObjectMapper objectMapper = new ObjectMapper();

@BeforeEach
public void setup() {
mockMvc = MockMvcBuilders
Expand All @@ -75,8 +72,8 @@ void _willSuccess() throws Exception {
.build();

given(securityUtil.getCurrentMemberId()).willReturn(1L);
given(favoriteService.register(any(Long.TYPE), any(Long.TYPE))).willReturn(
favoriteResponseDto);
given(favoriteService.register(any(Long.TYPE), any(Long.TYPE)))
.willReturn(favoriteResponseDto);

// when then
mockMvc.perform(post("/api/favorites/{productId}", 1L))
Expand Down Expand Up @@ -117,8 +114,8 @@ void _willSuccess() throws Exception {
.build();

given(securityUtil.getCurrentMemberId()).willReturn(1L);
given(favoriteService.getFavorites(any(Long.TYPE), any(Pageable.class))).willReturn(
favoritesResponseDto);
given(favoriteService.getFavorites(any(Long.TYPE), any(Pageable.class)))
.willReturn(favoritesResponseDto);

// when then
mockMvc.perform(get("/api/favorites")
Expand Down Expand Up @@ -158,8 +155,8 @@ void _willSuccess() throws Exception {
.build();

given(securityUtil.getCurrentMemberId()).willReturn(1L);
given(favoriteService.delete(any(Long.TYPE), any(Long.TYPE))).willReturn(
favoriteResponseDto);
given(favoriteService.delete(any(Long.TYPE), any(Long.TYPE)))
.willReturn(favoriteResponseDto);

// when then
mockMvc.perform(delete("/api/favorites/{productId}", 1L))
Expand Down

0 comments on commit 5474d98

Please sign in to comment.