Skip to content

Commit

Permalink
refactor: 커서 방식 부분에 정렬 기준 클릭률 순 추가 (#220)
Browse files Browse the repository at this point in the history
* refactor: statistic 네이밍 수정

* test: statistics service Test

* test: statistics service Test

* feat: 정렬 기준에 추천 수 추가

* refactor : 테스트 변경

* refactor: statistic 원상복구

* refactor: 클릭률은 통계테이블이 아닌 클릭률 테이블에서 가져오는 걸로 변경

* refactor: test failed

* refactor: 커서 네이밍 변경

* refactor: 잘못 들어간 커밋 삭제

* refactor: 잘못 들어간 커밋 삭제

* refactor: 네이밍 수정

* Update src/main/java/com/woowacamp/soolsool/core/liquor/service/LiquorService.java

Co-authored-by: Jeonggyu Choi <[email protected]>

* test: 커서, 클릭 수 not null일 때 추가

* refactor: 병합

* refactor: 피드백 반영

* refactor : 패키지 변경

* refactor : click Response 따로 생성

* refactor : click Response 따로 생성하여 관련 메서드들 수정

* refactor : PageClickResponse 메서드명 수정

* refactor : click, latest 순 나눔

* fix: test failed

* test : 커서 방식 테스트 추가

* refactor: 피드백 반영

* Update src/test/java/com/woowacamp/soolsool/core/liquor/service/LiquorServiceIntegrationTest.java

Co-authored-by: Jeonggyu Choi <[email protected]>

* refactor: dto->response 분리

* refactor: 리팩터링

---------

Co-authored-by: Jeonggyu Choi <[email protected]>
  • Loading branch information
worldii and whatasame authored Oct 14, 2023
1 parent aebada2 commit c3e3afb
Show file tree
Hide file tree
Showing 27 changed files with 364 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import static com.woowacamp.soolsool.core.liquor.code.LiquorResultCode.LIQUOR_UPDATED;

import com.woowacamp.soolsool.core.liquor.code.LiquorResultCode;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorBrewType;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorRegionType;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorStatusType;
import com.woowacamp.soolsool.core.liquor.dto.LiquorDetailResponse;
import com.woowacamp.soolsool.core.liquor.dto.LiquorElementResponse;
import com.woowacamp.soolsool.core.liquor.dto.LiquorModifyRequest;
import com.woowacamp.soolsool.core.liquor.dto.LiquorSaveRequest;
import com.woowacamp.soolsool.core.liquor.dto.PageLiquorResponse;
import com.woowacamp.soolsool.core.liquor.dto.request.LiquorListRequest;
import com.woowacamp.soolsool.core.liquor.dto.request.LiquorModifyRequest;
import com.woowacamp.soolsool.core.liquor.dto.request.LiquorSaveRequest;
import com.woowacamp.soolsool.core.liquor.dto.response.LiquorDetailResponse;
import com.woowacamp.soolsool.core.liquor.dto.response.LiquorElementResponse;
import com.woowacamp.soolsool.core.liquor.dto.response.PageLiquorResponse;
import com.woowacamp.soolsool.core.liquor.service.LiquorService;
import com.woowacamp.soolsool.global.aop.RequestLogging;
import com.woowacamp.soolsool.global.auth.dto.NoAuth;
Expand All @@ -28,15 +26,14 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand Down Expand Up @@ -76,8 +73,8 @@ public ResponseEntity<ApiResponse<LiquorDetailResponse>> liquorDetail(
public ResponseEntity<ApiResponse<List<LiquorElementResponse>>> liquorPurchasedTogether(
@PathVariable final Long liquorId
) {
final List<LiquorElementResponse> response =
liquorService.liquorPurchasedTogether(liquorId);
final List<LiquorElementResponse> response = liquorService.liquorPurchasedTogether(
liquorId);

return ResponseEntity.ok(
ApiResponse.of(LiquorResultCode.LIQUOR_PURCHASED_TOGETHER_FOUND, response));
Expand All @@ -89,11 +86,7 @@ public ResponseEntity<ApiResponse<List<LiquorElementResponse>>> liquorPurchasedT
public ResponseEntity<ApiResponse<PageLiquorResponse>> getLiquorFirstList(
@PageableDefault final Pageable pageable
) {
final PageRequest sortPageable = PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by("createdAt").descending()
);
final PageRequest sortPageable = getSortedPageable(pageable);

final PageLiquorResponse response = liquorService.getFirstPage(sortPageable);

Expand All @@ -104,25 +97,40 @@ public ResponseEntity<ApiResponse<PageLiquorResponse>> getLiquorFirstList(
@RequestLogging
@GetMapping
public ResponseEntity<ApiResponse<PageLiquorResponse>> liquorList(
@RequestParam("brew") @Nullable final LiquorBrewType brew,
@RequestParam("region") @Nullable final LiquorRegionType region,
@RequestParam("status") @Nullable final LiquorStatusType status,
@RequestParam("brand") @Nullable final String brand,
@RequestParam @Nullable final Long cursorId,
@ModelAttribute final LiquorListRequest liquorListRequest,
@PageableDefault final Pageable pageable
) {
final PageRequest sortPageable = PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by("createdAt").descending()
);
final PageRequest sortPageable = getSortedPageable(pageable);

final PageLiquorResponse response = liquorService
.liquorListByLatest(liquorListRequest, sortPageable);

return ResponseEntity.ok(ApiResponse.of(LIQUOR_LIST_FOUND, response));
}

@NoAuth
@RequestLogging
@GetMapping("/click")
public ResponseEntity<ApiResponse<PageLiquorResponse>> liquorListByClick(
@ModelAttribute final LiquorListRequest liquorListRequest,
@PageableDefault final Pageable pageable
) {
final PageRequest sortPageable = getSortedPageable(pageable);

final PageLiquorResponse response = liquorService
.liquorList(brew, region, status, brand, sortPageable, cursorId);
.liquorListByClick(liquorListRequest, sortPageable);

return ResponseEntity.ok(ApiResponse.of(LIQUOR_LIST_FOUND, response));
}

private PageRequest getSortedPageable(Pageable pageable) {
return PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by("createdAt").descending()
);
}

@Vendor
@RequestLogging
@PutMapping("/{liquorId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.woowacamp.soolsool.core.liquor.controller;

import com.woowacamp.soolsool.core.liquor.dto.LiquorStockSaveRequest;
import com.woowacamp.soolsool.core.liquor.dto.request.LiquorStockSaveRequest;
import com.woowacamp.soolsool.core.liquor.service.LiquorStockService;
import com.woowacamp.soolsool.global.aop.RequestLogging;
import com.woowacamp.soolsool.global.auth.dto.Vendor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorStatusType;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorStockCount;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorVolume;
import com.woowacamp.soolsool.core.liquor.dto.LiquorModifyRequest;
import com.woowacamp.soolsool.core.liquor.dto.request.LiquorModifyRequest;
import com.woowacamp.soolsool.global.common.BaseEntity;
import java.math.BigInteger;
import javax.persistence.Column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import static javax.persistence.GenerationType.IDENTITY;

import com.woowacamp.soolsool.core.liquor.code.LiquorCtrErrorCode;
import com.woowacamp.soolsool.core.liquor.domain.converter.LiquorCtrClickConverter;
import com.woowacamp.soolsool.core.liquor.domain.converter.LiquorCtrImpressionConverter;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorCtrClick;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorCtrImpression;
import com.woowacamp.soolsool.global.common.BaseEntity;
import com.woowacamp.soolsool.global.exception.SoolSoolException;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
Expand Down Expand Up @@ -38,8 +38,7 @@ public class LiquorCtr extends BaseEntity {
@Convert(converter = LiquorCtrImpressionConverter.class)
private LiquorCtrImpression impression;

@Column(name = "click", nullable = false)
@Convert(converter = LiquorCtrClickConverter.class)
@Embedded
private LiquorCtrClick click;

public LiquorCtr(@NonNull final Long liquorId) {
Expand All @@ -62,7 +61,7 @@ public double getCtr() {
throw new SoolSoolException(LiquorCtrErrorCode.DIVIDE_BY_ZERO_IMPRESSION);
}

final double ratio = (double) click.getClick() / impression.getImpression();
final double ratio = (double) click.getCount() / impression.getImpression();

return Math.round(ratio * 100) / 100.0;
}
Expand All @@ -72,6 +71,6 @@ public Long getImpression() {
}

public Long getClick() {
return click.getClick();
return click.getCount();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class LiquorCtrClickConverter implements AttributeConverter<LiquorCtrClic

@Override
public Long convertToDatabaseColumn(final LiquorCtrClick click) {
return click.getClick();
return click.getCount();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import com.woowacamp.soolsool.core.liquor.code.LiquorCtrErrorCode;
import com.woowacamp.soolsool.global.exception.SoolSoolException;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Embeddable
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
public class LiquorCtrClick {

private final Long click;
@Column(name = "click", nullable = false)
private Long count;

public LiquorCtrClick(final Long Click) {
validateIsNotNull(Click);
validateIsNotLessThanZero(Click);

this.click = Click;
this.count = Click;
}

private void validateIsNotNull(final Long Click) {
Expand All @@ -30,6 +36,6 @@ private void validateIsNotLessThanZero(final Long Click) {
}

public LiquorCtrClick increaseOne() {
return new LiquorCtrClick(this.click + 1);
return new LiquorCtrClick(this.count + 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.woowacamp.soolsool.core.liquor.dto.request;

import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorBrewType;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorRegionType;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorStatusType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable;


@Getter
@RequiredArgsConstructor
public class LiquorListRequest {
@Nullable
private final LiquorBrewType brew;
@Nullable
private final LiquorRegionType region;
@Nullable
private final LiquorStatusType status;
@Nullable
private final String brand;
@Nullable
private final Long liquorId;
@Nullable
private final Long clickCount;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woowacamp.soolsool.core.liquor.dto;
package com.woowacamp.soolsool.core.liquor.dto.request;

import java.time.LocalDateTime;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woowacamp.soolsool.core.liquor.dto;
package com.woowacamp.soolsool.core.liquor.dto.request;

import com.woowacamp.soolsool.core.liquor.domain.Liquor;
import com.woowacamp.soolsool.core.liquor.domain.LiquorBrew;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woowacamp.soolsool.core.liquor.dto;
package com.woowacamp.soolsool.core.liquor.dto.request;

import com.woowacamp.soolsool.core.liquor.domain.LiquorBrew;
import com.woowacamp.soolsool.core.liquor.domain.LiquorRegion;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woowacamp.soolsool.core.liquor.dto;
package com.woowacamp.soolsool.core.liquor.dto.request;

import com.woowacamp.soolsool.core.liquor.domain.LiquorStock;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorStockCount;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.woowacamp.soolsool.core.liquor.dto.response;

import com.woowacamp.soolsool.core.liquor.domain.Liquor;
import com.woowacamp.soolsool.core.liquor.domain.vo.LiquorCtrClick;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class LiquorClickElementDto {

private final Long id;
private final String name;
private final String price;
private final String imageUrl;
private final Integer stock;
private final Long clickCount;

public LiquorClickElementDto(final Liquor liquor, final LiquorCtrClick clickCount) {
this(
liquor.getId(),
liquor.getName(),
liquor.getPrice().toString(),
liquor.getImageUrl(),
liquor.getTotalStock(),
clickCount.getCount()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woowacamp.soolsool.core.liquor.dto;
package com.woowacamp.soolsool.core.liquor.dto.response;

import com.woowacamp.soolsool.core.liquor.domain.Liquor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.woowacamp.soolsool.core.liquor.dto;
package com.woowacamp.soolsool.core.liquor.dto.response;

import com.woowacamp.soolsool.core.liquor.domain.Liquor;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;

@Getter
@RequiredArgsConstructor
public class LiquorElementResponse {

private final Long id;
Expand All @@ -17,6 +12,20 @@ public class LiquorElementResponse {
private final String imageUrl;
private final Integer stock;

public LiquorElementResponse(
final Long id,
final String name,
final String price,
final String imageUrl,
final Integer stock
) {
this.id = id;
this.name = name;
this.price = price;
this.imageUrl = imageUrl;
this.stock = stock;
}

public LiquorElementResponse(final Liquor liquor) {
this.id = liquor.getId();
this.name = liquor.getName();
Expand All @@ -34,10 +43,4 @@ public static LiquorElementResponse from(final Liquor liquor) {
liquor.getTotalStock()
);
}

public static List<LiquorElementResponse> from(final Page<Liquor> liquors) {
return liquors.getContent().stream()
.map(LiquorElementResponse::from)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.woowacamp.soolsool.core.liquor.dto;
package com.woowacamp.soolsool.core.liquor.dto.response;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.woowacamp.soolsool.core.liquor.domain.Liquor;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import org.springframework.data.domain.Pageable;

Expand All @@ -24,22 +22,17 @@ public PageLiquorResponse(
this.nextCursorId = nextCursorId;
this.liquors = liquors;
}

public static PageLiquorResponse of(
final Pageable pageable,
final List<Liquor> liquors
final List<LiquorElementResponse> liquors
) {
final List<LiquorElementResponse> liquorElements = liquors.stream()
.map(LiquorElementResponse::from)
.collect(Collectors.toList());

if (liquors.size() < pageable.getPageSize()) {
return new PageLiquorResponse(false, liquorElements);
return new PageLiquorResponse(false, liquors);
}

final Long lastReadLiquorId = liquors.get(liquors.size() - 1).getId();

return new PageLiquorResponse(true, lastReadLiquorId, liquorElements);
return new PageLiquorResponse(true, lastReadLiquorId, liquors);
}

private PageLiquorResponse(final boolean hasNext, final List<LiquorElementResponse> liquors) {
Expand Down
Loading

0 comments on commit c3e3afb

Please sign in to comment.