Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
imenuuu committed Oct 18, 2023
2 parents deb7b13 + 02c9b00 commit fd7e084
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ public class WineController {
private final WineService wineService;

@GetMapping("/wines/{wineId}")
@Operation(summary= "02-04 Wine🍷 와인 상세조회 #001_01.1_와인 카드 자세히 보기",description = "와인 상세조회 API입니다.")
public CommonResponse<WineResponse.WineDTO> getWine(@PathVariable Long wineId) {
return null;
}

@GetMapping("/wines")
public CommonResponse<WineResponse.WineListDTO> getWineList(@RequestParam String name) {
return null;
return CommonResponse.onSuccess(wineService.getWineDTOById(wineId));
}

@PostMapping("/wines")
public CommonResponse<WineResponse.CreateWineDTO> createWine(@RequestBody WineRequest.CreateWineDTO request) {
return null;
}

@GetMapping("/wines/recommend")
@ApiErrorCodeExample(UserAuthErrorCode.class)
@Operation(summary= "02-01 Wine🍷 홈화면 와인 조회 API #FRAME 001_01_홈/메인페이지 Made By Austin",description = "홈화면 와인 추천 조회입니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.wineydomain.tastingNote.repository.TastingNoteRepository;
import com.example.wineydomain.wine.entity.Wine;
import com.example.wineydomain.wine.repository.WineRepository;
import com.example.wineydomain.wine.entity.WineSummary;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -89,4 +90,19 @@ private WineResponse.SearchWineDto WineInfo(Wine result) {
.name(result.getName())
.build();
}

public WineResponse.WineDTO toWineDTO(Wine wine, WineSummary wineSummary) {
return WineResponse.WineDTO.builder()
.wineId(wine.getId())
.type(wine.getType().name()) // Enum의 name() 메서드를 호출하여 문자열로 변환
.name(wine.getName())
.country(wine.getCountry().name()) // Enum의 name() 메서드를 호출하여 문자열로 변환
.varietal(wine.getVarietal())
.sweetness(wine.getSweetness())
.acidity(wine.getAcidity())
.body(wine.getBody())
.tannins(wine.getTannins())
.wineSummary(wineSummary)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.wineyapi.wine.dto;

import com.example.wineydomain.wine.entity.Country;
import com.example.wineydomain.wine.entity.WineSummary;
import com.example.wineydomain.wine.entity.WineType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import javax.persistence.Column;
import java.util.List;
import java.util.Map;

Expand All @@ -15,9 +17,21 @@ public class WineResponse {
@AllArgsConstructor
@Builder
public static class WineDTO {
private String field;
private Long wineId;
private String type;
private String name;
private String country;
private String varietal;

private Integer sweetness;
private Integer acidity;
private Integer body;
private Integer tannins;

private WineSummary wineSummary;
}


@NoArgsConstructor
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface WineService {
List<WineResponse.RecommendWineDTO> recommendWine(User user);

PageResponse<List<WineResponse.SearchWineDto>> searchWineList(Integer page, Integer size, String content);

WineResponse.WineDTO getWineDTOById(Long wineId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.example.wineyapi.tastingNote.convertor.TastingNoteConvertor;
import com.example.wineyapi.wine.convertor.WineConvertor;
import com.example.wineyapi.wine.dto.WineResponse;
import com.example.wineycommon.exception.NotFoundException;
import com.example.wineycommon.exception.errorcode.CommonResponseStatus;
import com.example.wineycommon.reponse.PageResponse;
import com.example.wineydomain.preference.entity.Preference;
import com.example.wineydomain.preference.repository.PreferenceRepository;
Expand All @@ -12,6 +14,8 @@
import com.example.wineydomain.wine.entity.RecommendWine;
import com.example.wineydomain.wine.entity.RecommendWinePk;
import com.example.wineydomain.wine.entity.Wine;
import com.example.wineydomain.wine.entity.WineSummary;
import com.example.wineydomain.wine.exception.ReadWineErrorCode;
import com.example.wineydomain.wine.repository.RecommendWineRepository;
import com.example.wineydomain.wine.repository.WineRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -94,5 +98,12 @@ public PageResponse<List<WineResponse.SearchWineDto>> searchWineList(Integer pag
return wineConvertor.SearchWineList(wines);
}


@Override
public WineResponse.WineDTO getWineDTOById(Long wineId) {
Wine wine = wineRepository.findById(wineId)
.orElseThrow(() -> new NotFoundException(ReadWineErrorCode.NOT_EXIST_WINE));
WineSummary wineSummary = tastingNoteRepository.findWineSummaryByWineId(wineId)
.orElseGet(() -> new WineSummary(0.0, 0, 0, 0, 0));
return wineConvertor.toWineDTO(wine, wineSummary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,6 @@ public enum CommonResponseStatus implements BaseErrorCode {

















private final HttpStatus httpStatus;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.wineydomain.tastingNote.repository;

import com.example.wineydomain.tastingNote.entity.TastingNote;
import com.example.wineydomain.wine.entity.WineSummary;

public interface TastingNoteCustomRepository {
TastingNote getTastingNote(Long noteId, boolean deleted);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import com.example.wineydomain.tastingNote.entity.TastingNote;
import com.example.wineydomain.user.entity.User;
import com.example.wineydomain.wine.entity.Country;
import com.example.wineydomain.wine.entity.WineSummary;
import com.example.wineydomain.wine.entity.WineType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface TastingNoteRepository extends JpaRepository<TastingNote, Long>,TastingNoteCustomRepository {
List<TastingNote> findByUser(User user);


@Query("SELECT new com.example.wineydomain.wine.entity.WineSummary(avg(t.price), avg(t.sweetness), avg(t.acidity), avg(t.body), avg(t.tannins)) FROM TastingNote t WHERE t.wine.id = :wineId AND t.buyAgain = true")
Optional<WineSummary> findWineSummaryByWineId(@Param("wineId") Long wineId);

@Query(value = "SELECT W.id as 'wineId', W.name, W.country, W.type, COALESCE(AVG(CASE WHEN TN.price IS NULL OR TN.price = 0 THEN NULL ELSE TN.price END),0) as 'price', W.varietal " +
"FROM TastingNote TN JOIN Wine W ON TN.wineId = W.id " +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.wineydomain.wine.entity;

import lombok.*;

import java.util.Optional;

@NoArgsConstructor
@Getter @Setter
@AllArgsConstructor
public class WineSummary {
private Double avgPrice;
private Integer avgSweetness;
private Integer avgAcidity;
private Integer avgBody;
private Integer avgTannins;

// 쿼리 결과를 위한 생성자
public WineSummary(Double avgPrice, Double avgSweetness, Double avgAcidity, Double avgBody, Double avgTannins) {
this.avgPrice = roundToFirstDecimal(avgPrice);
this.avgSweetness = roundOrDefault(avgSweetness);
this.avgAcidity = roundOrDefault(avgAcidity);
this.avgBody = roundOrDefault(avgBody);
this.avgTannins = roundOrDefault(avgTannins);
}

private Double roundToFirstDecimal(Double value) {
return Optional.ofNullable(value)
.map(v -> Math.round(v * 10.0) / 10.0)
.orElse(0.0);
}

private int roundOrDefault(Double value) {
return Optional.ofNullable(value)
.map(v -> (int) Math.round(v))
.orElse(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.wineydomain.wine.exception;

import com.example.wineycommon.annotation.ExplainError;
import com.example.wineycommon.exception.errorcode.BaseErrorCode;
import com.example.wineycommon.exception.errorcode.ErrorReason;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.lang.reflect.Field;
import java.util.Objects;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

@Getter
@AllArgsConstructor
public enum ReadWineErrorCode implements BaseErrorCode {
/**
* WXXX : Wine 관련 에러
*/
NOT_EXIST_WINE(BAD_REQUEST,"W001" , "해당 와인이 존재하지 않습니다.");


private final HttpStatus httpStatus;
private final String code;
private final String message;

@Override
public ErrorReason getErrorReason() {
return ErrorReason.builder().message(message).code(code).isSuccess(false).build();
}

@Override
public String getExplainError() throws NoSuchFieldException {
Field field = this.getClass().getField(this.name());
ExplainError annotation = field.getAnnotation(ExplainError.class);
return Objects.nonNull(annotation) ? annotation.value() : this.getMessage();
}

@Override
public ErrorReason getErrorReasonHttpStatus(){
return ErrorReason.builder().message(message).code(code).isSuccess(false).httpStatus(httpStatus).build();
}
}

0 comments on commit fd7e084

Please sign in to comment.