Skip to content

Commit

Permalink
Merge pull request #81 from Tea-Bliss/dev
Browse files Browse the repository at this point in the history
[feat] 설문조사 Get API 추가
  • Loading branch information
mun9659 authored Jun 22, 2024
2 parents b0cba6c + 40a7f08 commit 2011e5e
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package store.teabliss.survey.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import store.teabliss.member.entity.MemberDetails;
import store.teabliss.survey.dto.SurveyCreateDto;
import store.teabliss.survey.dto.SurveyDto;
import store.teabliss.survey.dto.SurveyResponse;
import store.teabliss.survey.service.SurveyService;

Expand All @@ -22,6 +21,7 @@ public class SurveyController {
private final SurveyService surveyService;

@PostMapping("")
@Operation(summary = "설문조사 등록", description = "설문조사 등록 API")
public ResponseEntity<SurveyResponse> createSurvey(
@AuthenticationPrincipal MemberDetails memberDetails,
@RequestBody SurveyCreateDto surveyCreateDto
Expand All @@ -32,4 +32,15 @@ public ResponseEntity<SurveyResponse> createSurvey(
return ResponseEntity.ok(SurveyResponse.ok("정상적으로 설문조사가 등록되었습니다.", id));
}

@GetMapping("/{id}")
@Operation(summary = "설문조사 조회", description = "설문조사 조회 API")
public ResponseEntity<SurveyResponse> getSurvey(
@PathVariable Long id
) {

SurveyDto surveyDto = surveyService.findById(id);

return ResponseEntity.ok(SurveyResponse.ok(surveyDto));
}

}
4 changes: 2 additions & 2 deletions src/main/java/store/teabliss/survey/dto/SurveyCreateDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class SurveyCreateDto {
@Schema(description = "맛", example = "1")
private int taste;

@Schema(description = "원하는 가격대", example = "10,000원대")
private String sale;
@Schema(description = "원하는 가격대", example = "10000")
private int sale;

@Schema(description = "원하는 재료", example = "Black,Pu Erh")
private String category;
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/store/teabliss/survey/dto/SurveyDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package store.teabliss.survey.dto;

import lombok.Builder;
import lombok.Getter;
import store.teabliss.survey.entity.Survey;
import store.teabliss.tea.dto.TeaFinalDto;
import store.teabliss.tea.entity.Tea;

import java.util.List;

@Getter
@Builder
public class SurveyDto {

private int taste;

private int sale;

private String category;

private String caffeine;

private List<TeaFinalDto> teas;

public static SurveyDto of(Survey survey, List<Tea> list) {
return SurveyDto.builder()
.taste(survey.getTaste())
.sale(survey.getSale())
.category(survey.getCategory())
.caffeine(survey.getCaffeine())
.teas(TeaFinalDto.of(list))
.build();
}
}
2 changes: 2 additions & 0 deletions src/main/java/store/teabliss/survey/dto/SurveyResponse.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package store.teabliss.survey.dto;

import lombok.Builder;
import lombok.Getter;
import store.teabliss.common.response.CommonResponse;
import store.teabliss.ingredient.dto.IngredientResponse;

@Getter
public class SurveyResponse extends CommonResponse {

private final Object data;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/store/teabliss/survey/entity/Survey.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Survey {

private int taste;

private String sale;
private int sale;

private String category;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/store/teabliss/survey/mapper/SurveyMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface SurveyMapper {

Long createSurvey(Survey survey);

Survey findById(Long id);

}
22 changes: 22 additions & 0 deletions src/main/java/store/teabliss/survey/service/SurveyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import store.teabliss.survey.dto.SurveyCreateDto;
import store.teabliss.survey.dto.SurveyDto;
import store.teabliss.survey.entity.Survey;
import store.teabliss.survey.mapper.SurveyMapper;
import store.teabliss.tea.entity.Tea;
import store.teabliss.tea.mapper.TeaMapper;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SurveyService {

private final SurveyMapper surveyMapper;
private final TeaMapper teaMapper;

public Long createSurvey(Long memId, SurveyCreateDto surveyCreateDto) {

Expand All @@ -19,4 +25,20 @@ public Long createSurvey(Long memId, SurveyCreateDto surveyCreateDto) {
return surveyMapper.createSurvey(survey);
}

public SurveyDto findById(Long id) {

Survey survey = surveyMapper.findById(id);

Tea tea = Tea.builder()
.priceStart(survey.getSale())
.priceEnd(survey.getSale() + 10000)
.category(survey.getCategory())
.caffeine(survey.getCaffeine().equals("N"))
.build();

List<Tea> teas = teaMapper.surveyRecommendTea(tea);

return SurveyDto.of(survey, teas);
}

}
2 changes: 0 additions & 2 deletions src/main/java/store/teabliss/tea/dto/TeaFinalDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public static List<TeaFinalDto> of(List<Tea> list){
List<TeaFinalDto> teaFinalDto = new ArrayList<>();
// var fommatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.KOREA);



for (Tea tea : list){
TeaFinalDto dto = TeaFinalDto.builder()
.id(tea.getId())
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/store/teabliss/tea/entity/Tea.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ public class Tea {
private LocalDateTime createDt;

private LocalDateTime updateDt;

/**
* 가격대 검색을 위한 컬럼
*/
private int priceStart;

private int priceEnd;

}
2 changes: 2 additions & 0 deletions src/main/java/store/teabliss/tea/mapper/TeaMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ public interface TeaMapper {

// List<Tea> category();

List<Tea> surveyRecommendTea(Tea tea);


}
11 changes: 11 additions & 0 deletions src/main/resources/mapper/SurveyMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@
VALUES (#{taste}, #{sale}, #{category}, #{caffeine}, #{memId})
</insert>

<select id="findById" parameterType="Long">
SELECT
taste,
sale,
category,
caffeine
FROM SURVEY
WHERE 1=1
AND survey_id = #{value}
</select>

</mapper>
34 changes: 33 additions & 1 deletion src/main/resources/mapper/TeaMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,39 @@
</update>



<!-- 설문조사 결과로 인한 완제품 추천 -->
<select id="surveyRecommendTea" parameterType="Tea">
SELECT
id,
price,
category,
sale,
rating,
rate,
season,
name,
name_eng,
caffeine,
description,
img,
inventory,
sale_status
FROM TEA
WHERE 1=1
<if test="category != null and category != ''">
AND category IN (#{category})
</if>
<if test="caffeine != null and caffeine != ''">
AND caffeine = #{caffeine}
</if>
<if test="priceStart != null and priceEnd != null and priceStart != '' and priceEnd != ''">
<![CDATA[
AND price >= #{priceStart} and price < #{priceEnd}
]]>
</if>
ORDER BY RAND()
LIMIT 3
</select>


</mapper>

0 comments on commit 2011e5e

Please sign in to comment.