Skip to content

Commit

Permalink
Merge pull request #54 from Tave-13th-Project-Team-4-Fiurinee/feature…
Browse files Browse the repository at this point in the history
…/model

feat: 모델 서버와 연동 하여 데이터 받아 오는 코드 추가 작성
  • Loading branch information
ss7622 authored Jul 12, 2024
2 parents 22235cc + a50cc8c commit 65c5bcc
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface FlowerRepository extends JpaRepository<Flower, Long> {
List<Flower> findByPeriodMonth(@Param("startPeriod") Long startPeriod, @Param("endPeriod") Long endPeriod);

Optional<Flower> findByNameAndFlowerLanguage(String name,String flowerLanguage);

List<Flower> findByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,19 @@ public Flower findByNameAndFlowerLanguage(String name,String flowerLangauge){

return flower;
}

public Flower findByName(String name){

name = name.trim();

return flowerRepository.findByName(name).get(0);

}

public Flower findById(Long id){
Flower flower = flowerRepository.findById(id).orElseThrow(
() -> new CustomException("id가 일치하는 꽃이 존재하지 않습니다."));

return flower;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
import com.example.fiurinee.domain.inputMessage.controller.api.ModelApi;
import com.example.fiurinee.domain.inputMessage.dto.*;
import com.example.fiurinee.domain.inputMessage.service.InputMessageService;
import com.example.fiurinee.domain.matchingFlower.entity.MatchingFlower;
import com.example.fiurinee.domain.matchingFlower.service.MatchingFlowerService;
import com.example.fiurinee.domain.member.entity.Member;
import com.example.fiurinee.domain.member.service.MemberService;
import com.example.fiurinee.domain.recommendComment.service.RecommendCommentService;
import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower;
import com.example.fiurinee.domain.recommendFlower.service.RecommendFlowerService;
import com.example.fiurinee.global.api.service.CallApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,6 +33,10 @@ public class ModelController implements ModelApi {

private final InputMessageService inputMessageService;
private final FlowerService flowerService;
private final MemberService memberService;
private final RecommendFlowerService recommendFlowerService;
private final MatchingFlowerService matchingFlowerService;
private final RecommendCommentService recommendCommentService;

@PostMapping("/{id}/ment")
public ResponseEntity<List<ResponseMentDto>> inputMent(@PathVariable("id") Long id,
Expand Down Expand Up @@ -85,26 +98,77 @@ public ResponseEntity<List<ResponseMentDto>> inputMentNotMember(@RequestBody Str
public ResponseEntity<ResponseHarmonyWitnMentDto> selectFlower(@PathVariable ("memberId") Long memberId,
@PathVariable ("flowerId") Long flowerId,
@RequestBody String ment){
ResponseHarmonyDto responseHarmonyDto0 = new ResponseHarmonyDto(flowerService.findByNameAndFlowerLanguage("토마토", "완성된 아름다움"));
ResponseHarmonyDto responseHarmonyDto1 = new ResponseHarmonyDto(flowerService.findByNameAndFlowerLanguage("토레니아", "가련한 욕망"));

String url = "http://3.106.186.161/api/flower_color";
Member byId = memberService.findById(memberId);
Flower flower = flowerService.findById(flowerId);

RequestHarmonyDto requestHarmonyDto = new RequestHarmonyDto(flower.getName(), flower.getFlowerLanguage());

List<FlowerColorDto> flowerColorDtos = CallApiService.harmonyApi(url, requestHarmonyDto);

String flowerName0 = flowerColorDtos.get(0).getFlowerName();
String flowerName1 = flowerColorDtos.get(1).getFlowerName();

Flower byName0 = flowerService.findByName(flowerName0);
Flower byName1 = flowerService.findByName(flowerName1);

//추천 받은 꽃과 어울리는 꽃 정보를 멤버 엔티티에 저장

RecommendFlower saveRecommendFlower = recommendFlowerService.saveRecommendFlower(byId,flower);

matchingFlowerService.save(saveRecommendFlower,byName0);
matchingFlowerService.save(saveRecommendFlower,byName1);

ResponseHarmonyDto responseHarmonyDto0 = new ResponseHarmonyDto(byName0);
ResponseHarmonyDto responseHarmonyDto1 = new ResponseHarmonyDto(byName1);

List<ResponseHarmonyDto> re = new ArrayList<>();
re.add(responseHarmonyDto0);
re.add(responseHarmonyDto1);

return ResponseEntity.ok(new ResponseHarmonyWitnMentDto("추천은 recommend, recommend는 영어로 추천, 내 너무 피곤하다,, 인생이란 뭘까",re));
///모델 서버에서 멘트 추천 받아오기
url = "http://3.106.186.161/api/ment";

RequestGptDto requestGptDto = new RequestGptDto(ment, flower.getName(), flower.getFlowerLanguage());

ResponseGptDto responseGptDto = CallApiService.gptApi(url, requestGptDto);

recommendCommentService.saveRecommendComment(byId,responseGptDto.getFlower_ment());

return ResponseEntity.ok(new ResponseHarmonyWitnMentDto(responseGptDto.getFlower_ment(),re));
}

@PostMapping("/{flowerId}/non")
public ResponseEntity<ResponseHarmonyWitnMentDto> selectFlowerNonMember(@PathVariable ("flowerId") Long flowerId,
@RequestBody String ment){
ResponseHarmonyDto responseHarmonyDto0 = new ResponseHarmonyDto(flowerService.findByNameAndFlowerLanguage("토마토", "완성된 아름다움"));
ResponseHarmonyDto responseHarmonyDto1 = new ResponseHarmonyDto(flowerService.findByNameAndFlowerLanguage("토레니아", "가련한 욕망"));
String url = "http://3.106.186.161/api/flower_color";
Flower flower = flowerService.findById(flowerId);

RequestHarmonyDto requestHarmonyDto = new RequestHarmonyDto(flower.getName(), flower.getFlowerLanguage());

List<FlowerColorDto> flowerColorDtos = CallApiService.harmonyApi(url, requestHarmonyDto);

String flowerName0 = flowerColorDtos.get(0).getFlowerName();
String flowerName1 = flowerColorDtos.get(1).getFlowerName();

Flower byName0 = flowerService.findByName(flowerName0);
Flower byName1 = flowerService.findByName(flowerName1);

ResponseHarmonyDto responseHarmonyDto0 = new ResponseHarmonyDto(byName0);
ResponseHarmonyDto responseHarmonyDto1 = new ResponseHarmonyDto(byName1);

List<ResponseHarmonyDto> re = new ArrayList<>();
re.add(responseHarmonyDto0);
re.add(responseHarmonyDto1);

return ResponseEntity.ok(new ResponseHarmonyWitnMentDto("추천은 recommend, recommend는 영어로 추천, 내 너무 피곤하다,, 인생이란 뭘까",re));
///모델 서버에서 멘트 추천 받아오기
url = "http://3.106.186.161/api/ment";

RequestGptDto requestGptDto = new RequestGptDto(ment, flower.getName(), flower.getFlowerLanguage());

ResponseGptDto responseGptDto = CallApiService.gptApi(url, requestGptDto);

return ResponseEntity.ok(new ResponseHarmonyWitnMentDto(responseGptDto.getFlower_ment(),re));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.fiurinee.domain.inputMessage.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class FlowerColorDto {

@JsonProperty("꽃")
private String flowerName;

@JsonProperty("선택한 색상")
private String selectedColor;

public FlowerColorDto(){}

public FlowerColorDto(String flower, String selectedColor) {
this.flowerName = flower;
this.selectedColor = selectedColor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.fiurinee.domain.inputMessage.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class FlowerColorListResponseDto {

@JsonProperty("flower_color_list")
private List<FlowerColorDto> flowerColorList;

public FlowerColorListResponseDto(){}

public FlowerColorListResponseDto(List<FlowerColorDto> flowerColorList) {
this.flowerColorList = flowerColorList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.fiurinee.domain.inputMessage.dto;

import lombok.Getter;

@Getter
public class RequestGptDto {

private String user_input;
private String flower_name;
private String flower_mean;

public RequestGptDto(){}

public RequestGptDto(String user_input, String flower_name, String flower_mean) {
this.user_input = user_input;
this.flower_name = flower_name;
this.flower_mean = flower_mean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.fiurinee.domain.inputMessage.dto;

import lombok.Getter;

@Getter
public class RequestHarmonyDto {

private String flower_name;
private String flower_mean;

public RequestHarmonyDto(){}

public RequestHarmonyDto(String flower_name, String flower_mean) {
this.flower_name = flower_name;
this.flower_mean = flower_mean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.fiurinee.domain.inputMessage.dto;

import lombok.Getter;

@Getter
public class ResponseGptDto {

private String flower_ment;

public ResponseGptDto(){}

public ResponseGptDto(String flower_ment) {
this.flower_ment = flower_ment;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.fiurinee.domain.matchingFlower.service;

import com.example.fiurinee.domain.flower.entity.Flower;
import com.example.fiurinee.domain.matchingFlower.entity.MatchingFlower;
import com.example.fiurinee.domain.matchingFlower.repository.MatchingFlowerRepository;
import com.example.fiurinee.domain.member.entity.Member;
import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -16,7 +19,13 @@ public class MatchingFlowerService {
private final MatchingFlowerRepository matchingFlowerRepository;

@Transactional
public void save(MatchingFlower matchingFlower){
public void save(RecommendFlower recommendFlower, Flower flower){

MatchingFlower matchingFlower = MatchingFlower.builder()
.recommendFlower(recommendFlower)
.flower(flower)
.build();

matchingFlowerRepository.save(matchingFlower);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.fiurinee.domain.recommendComment.service;

import com.example.fiurinee.domain.member.entity.Member;
import com.example.fiurinee.domain.recommendComment.entity.RecommendComment;
import com.example.fiurinee.domain.recommendComment.repository.RecommendCommentRepository;
import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower;
Expand All @@ -17,7 +18,13 @@ public class RecommendCommentService {
private final RecommendCommentRepository recommendCommentRepository;

@Transactional
public void saveRecommendComment(RecommendComment recommendComment){
recommendCommentRepository.save(recommendComment);
public void saveRecommendComment(Member member, String ment){

RecommendComment comment = RecommendComment.builder()
.prefer(false)
.member(member)
.content(ment)
.build();
recommendCommentRepository.save(comment);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.fiurinee.domain.recommendFlower.service;

import com.example.fiurinee.domain.flower.entity.Flower;
import com.example.fiurinee.domain.member.entity.Member;
import com.example.fiurinee.domain.recommendFlower.entity.RecommendFlower;
import com.example.fiurinee.domain.recommendFlower.repository.RecommendFlowerRepository;
import com.example.fiurinee.global.exception.CustomException;
Expand All @@ -8,6 +10,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Service
@Transactional(readOnly = true)
@Slf4j
Expand All @@ -17,8 +22,16 @@ public class RecommendFlowerService {
private final RecommendFlowerRepository recommendFlowerRepository;

@Transactional
public void saveRecommendFlower(RecommendFlower recommendFlower){
recommendFlowerRepository.save(recommendFlower);
public RecommendFlower saveRecommendFlower(Member member, Flower flower){

RecommendFlower recommendFlower = RecommendFlower.builder()
.member(member)
.prefer(false)
.createAt(Timestamp.valueOf(LocalDateTime.now()))
.flower(flower)
.build();

return recommendFlowerRepository.save(recommendFlower);
}

public RecommendFlower findById(Long id){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.example.fiurinee.global.api.service;

import com.example.fiurinee.domain.inputMessage.dto.MentDto;
import com.example.fiurinee.domain.inputMessage.dto.ModelMentResponseDto;
import com.example.fiurinee.domain.inputMessage.dto.RecommendationResponse;
import com.example.fiurinee.domain.inputMessage.dto.ResponseMentDto;
import com.example.fiurinee.domain.inputMessage.dto.*;
import com.example.fiurinee.domain.member.entity.Member;
import com.example.fiurinee.domain.oauth2.dto.KakaoLogoutDto;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -85,4 +82,37 @@ public static List<ModelMentResponseDto> mentApi(String url, MentDto mentDto) {
return Objects.requireNonNull(response).getRecommendations();

}

public static List<FlowerColorDto> harmonyApi(String url, RequestHarmonyDto requestHarmonyDto) {

WebClient webClient = WebClient.builder().build();

FlowerColorListResponseDto response = webClient
.post()
.uri(url)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(requestHarmonyDto)
.retrieve()
.bodyToMono(FlowerColorListResponseDto.class)
.block();
return Objects.requireNonNull(response).getFlowerColorList();

}

public static ResponseGptDto gptApi(String url, RequestGptDto requestGptDto) {

WebClient webClient = WebClient.builder().build();

ResponseGptDto response = webClient
.post()
.uri(url)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(requestGptDto)
.retrieve()
.bodyToMono(ResponseGptDto.class)
.block();

return Objects.requireNonNull(response);

}
}

0 comments on commit 65c5bcc

Please sign in to comment.