From 6f64fd1ec95e496194f5088e5d1ae795c2e018f4 Mon Sep 17 00:00:00 2001 From: UU_jeong <80961726+oU-Ua@users.noreply.github.com> Date: Tue, 19 Dec 2023 21:04:13 +0900 Subject: [PATCH 1/2] refactor : separate fetch APi method --- .../controller/CalendarController.java | 6 -- .../promiseme/calendar/entity/Calendar.java | 3 - .../repository/CalendarRepository.java | 5 -- .../calendar/service/CalendarService.java | 4 - .../calendar/service/CalendarServiceImpl.java | 4 - .../meeting/dto/SummaryRequestDTO.java | 1 - .../project/controller/ProjectController.java | 1 - .../project/repository/MemberRepository.java | 2 +- .../project/repository/ProjectRepository.java | 2 - .../project/service/ProjectServiceImpl.java | 6 +- .../project/service/RecommendService.java | 2 - .../project/service/RecommendServiceImpl.java | 83 ++++++++----------- .../users/controller/UserController.java | 1 - 13 files changed, 38 insertions(+), 82 deletions(-) diff --git a/src/main/java/mvc/promiseme/calendar/controller/CalendarController.java b/src/main/java/mvc/promiseme/calendar/controller/CalendarController.java index 53aedbb..5bf03c9 100644 --- a/src/main/java/mvc/promiseme/calendar/controller/CalendarController.java +++ b/src/main/java/mvc/promiseme/calendar/controller/CalendarController.java @@ -2,16 +2,10 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import mvc.promiseme.calendar.dto.CalendarResponseDTO; import mvc.promiseme.calendar.service.CalendarService; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import java.util.List; - @Slf4j @RestController @RequiredArgsConstructor diff --git a/src/main/java/mvc/promiseme/calendar/entity/Calendar.java b/src/main/java/mvc/promiseme/calendar/entity/Calendar.java index 3a422e5..946643d 100644 --- a/src/main/java/mvc/promiseme/calendar/entity/Calendar.java +++ b/src/main/java/mvc/promiseme/calendar/entity/Calendar.java @@ -2,14 +2,11 @@ import jakarta.persistence.*; import lombok.*; - -import mvc.promiseme.project.entity.Member; import mvc.promiseme.project.entity.Project; import mvc.promiseme.project.entity.Role; import mvc.promiseme.todo.entity.Todo; import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java b/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java index c7af2b5..c2e3692 100644 --- a/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java +++ b/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java @@ -1,12 +1,7 @@ package mvc.promiseme.calendar.repository; -import mvc.promiseme.calendar.dto.CalendarResponseDTO; import mvc.promiseme.calendar.entity.Calendar; -import mvc.promiseme.project.entity.Project; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; - -import java.util.List; public interface CalendarRepository extends JpaRepository { // @Query("SELECT NEW mvc.promiseme.calendar.dto.CalendarResponseDTO(c.content, c.startDate, c.finishDate, u.nickname) from Calendar c join c.member m join m.users u where c.project = :project") diff --git a/src/main/java/mvc/promiseme/calendar/service/CalendarService.java b/src/main/java/mvc/promiseme/calendar/service/CalendarService.java index 0d2f0f9..8399700 100644 --- a/src/main/java/mvc/promiseme/calendar/service/CalendarService.java +++ b/src/main/java/mvc/promiseme/calendar/service/CalendarService.java @@ -1,9 +1,5 @@ package mvc.promiseme.calendar.service; -import mvc.promiseme.calendar.dto.CalendarResponseDTO; - -import java.util.List; - public interface CalendarService { //public ListcalendarAll(Long projectId); diff --git a/src/main/java/mvc/promiseme/calendar/service/CalendarServiceImpl.java b/src/main/java/mvc/promiseme/calendar/service/CalendarServiceImpl.java index 1878578..290a23d 100644 --- a/src/main/java/mvc/promiseme/calendar/service/CalendarServiceImpl.java +++ b/src/main/java/mvc/promiseme/calendar/service/CalendarServiceImpl.java @@ -1,13 +1,9 @@ package mvc.promiseme.calendar.service; import lombok.RequiredArgsConstructor; -import mvc.promiseme.calendar.dto.CalendarResponseDTO; import mvc.promiseme.calendar.repository.CalendarRepository; -import mvc.promiseme.project.entity.Project; import org.springframework.stereotype.Service; -import java.util.List; - @Service @RequiredArgsConstructor public class CalendarServiceImpl implements CalendarService{ diff --git a/src/main/java/mvc/promiseme/meeting/dto/SummaryRequestDTO.java b/src/main/java/mvc/promiseme/meeting/dto/SummaryRequestDTO.java index e53ccee..b17b523 100644 --- a/src/main/java/mvc/promiseme/meeting/dto/SummaryRequestDTO.java +++ b/src/main/java/mvc/promiseme/meeting/dto/SummaryRequestDTO.java @@ -1,7 +1,6 @@ package mvc.promiseme.meeting.dto; import lombok.*; -import org.springframework.web.bind.annotation.RequestParam; @NoArgsConstructor @AllArgsConstructor @Getter diff --git a/src/main/java/mvc/promiseme/project/controller/ProjectController.java b/src/main/java/mvc/promiseme/project/controller/ProjectController.java index 7b8fd7a..5c4ebf0 100644 --- a/src/main/java/mvc/promiseme/project/controller/ProjectController.java +++ b/src/main/java/mvc/promiseme/project/controller/ProjectController.java @@ -2,7 +2,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import mvc.promiseme.calendar.dto.CalendarResponseDTO; import mvc.promiseme.project.dto.ProjectRequestDTO; import mvc.promiseme.project.dto.ProjectResponseDTO; import mvc.promiseme.project.dto.RecommendMemberRequestDTO; diff --git a/src/main/java/mvc/promiseme/project/repository/MemberRepository.java b/src/main/java/mvc/promiseme/project/repository/MemberRepository.java index d28d9c5..a2175f8 100644 --- a/src/main/java/mvc/promiseme/project/repository/MemberRepository.java +++ b/src/main/java/mvc/promiseme/project/repository/MemberRepository.java @@ -5,8 +5,8 @@ import mvc.promiseme.users.entity.Users; import org.springframework.data.jpa.repository.JpaRepository; -import java.util.Optional; import java.util.List; +import java.util.Optional; public interface MemberRepository extends JpaRepository { Optional findByUsersAndProject(Users users, Project project); diff --git a/src/main/java/mvc/promiseme/project/repository/ProjectRepository.java b/src/main/java/mvc/promiseme/project/repository/ProjectRepository.java index 23fbfa0..26fca80 100644 --- a/src/main/java/mvc/promiseme/project/repository/ProjectRepository.java +++ b/src/main/java/mvc/promiseme/project/repository/ProjectRepository.java @@ -1,7 +1,5 @@ package mvc.promiseme.project.repository; -import mvc.promiseme.project.dto.ProjectResponseDTO; -import mvc.promiseme.project.entity.Member; import mvc.promiseme.project.entity.Project; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; diff --git a/src/main/java/mvc/promiseme/project/service/ProjectServiceImpl.java b/src/main/java/mvc/promiseme/project/service/ProjectServiceImpl.java index 847cf83..e8e8efa 100644 --- a/src/main/java/mvc/promiseme/project/service/ProjectServiceImpl.java +++ b/src/main/java/mvc/promiseme/project/service/ProjectServiceImpl.java @@ -3,7 +3,6 @@ import lombok.RequiredArgsConstructor; import mvc.promiseme.common.exception.ErrorCode; import mvc.promiseme.common.exception.UserException; -import mvc.promiseme.project.dto.MemberDTO; import mvc.promiseme.project.dto.MemberRequestDTO; import mvc.promiseme.project.dto.ProjectRequestDTO; import mvc.promiseme.project.dto.ProjectResponseDTO; @@ -14,7 +13,6 @@ import mvc.promiseme.project.repository.MemberRepository; import mvc.promiseme.project.repository.ProjectRepository; import mvc.promiseme.project.repository.RoleRepository; -import mvc.promiseme.todo.dto.TodoRequestDTO; import mvc.promiseme.todo.entity.Todo; import mvc.promiseme.todo.repository.TodoRepository; import mvc.promiseme.users.entity.Users; @@ -22,7 +20,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; @Service @RequiredArgsConstructor diff --git a/src/main/java/mvc/promiseme/project/service/RecommendService.java b/src/main/java/mvc/promiseme/project/service/RecommendService.java index 02fb934..a515a2e 100644 --- a/src/main/java/mvc/promiseme/project/service/RecommendService.java +++ b/src/main/java/mvc/promiseme/project/service/RecommendService.java @@ -1,9 +1,7 @@ package mvc.promiseme.project.service; -import lombok.RequiredArgsConstructor; import mvc.promiseme.project.dto.RecommendMemberRequestDTO; import mvc.promiseme.project.dto.RecommendScheduleRequestDTO; -import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; diff --git a/src/main/java/mvc/promiseme/project/service/RecommendServiceImpl.java b/src/main/java/mvc/promiseme/project/service/RecommendServiceImpl.java index cbf3169..ec8de4d 100644 --- a/src/main/java/mvc/promiseme/project/service/RecommendServiceImpl.java +++ b/src/main/java/mvc/promiseme/project/service/RecommendServiceImpl.java @@ -32,9 +32,7 @@ public class RecommendServiceImpl implements RecommendService{ private final RoleRepository roleRepository; @Override public Map recommendMember(RecommendMemberRequestDTO recommendMemberRequestDTO) { - String url = clovaStudioRecommend.getUrl(); - String apiKeyClovaStudio = clovaStudioRecommend.getApiKeyClovaStudio(); - + String requestId = clovaStudioRecommend.getRequestMemberId(); List messages = new ArrayList<>(); @@ -52,27 +50,7 @@ public Map recommendMember(RecommendMemberRequestDTO recommendMe Message requestMessage = new Message("user", "나는" + recommendMemberRequestDTO.getCategory() + "프로젝트를 진행할예정입니다. 시작날짜는" + recommendMemberRequestDTO.getStart() + ", 마감날짜는" + recommendMemberRequestDTO.getDeadline() + "입니다. 이때 필요한 팀의 구성원 역할을 알려주세요. 다른 말 필요없이\n\"역할\" : \"역할이 해야할일\"\n로만 대답해주세요. 다른 말 없이 역할과 역할이 해야할일만 작성해주세요."); messages.add(requestMessage); - - HttpHeaders headers = new HttpHeaders(); - headers.set("X-NCP-CLOVASTUDIO-API-KEY", apiKeyClovaStudio); - headers.set("X-NCP-APIGW-API-KEY", clovaStudioRecommend.getApiGateWayKey()); - headers.set("X-NCP-CLOVASTUDIO-REQUEST-ID", clovaStudioRecommend.getRequestMemberId()); - headers.set("Content-Type", "application/json"); - - Map requestBody = new HashMap<>(); - requestBody.put("messages", messages); - requestBody.put("topP", 0.8); - requestBody.put("topK", 0); - requestBody.put("maxTokens", 256); - requestBody.put("temperature", 0.5); - requestBody.put("repeatPenalty", 5.0); - requestBody.put("includeAiFilters", "True"); - - HttpEntity> requestEntity = new HttpEntity<>(requestBody, headers); - - RestTemplate restTemplate = new RestTemplate(); - ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); - + ResponseEntity responseEntity = fetchClovaAPI(messages,requestId); JsonObject jsonObject = JsonParser.parseString(responseEntity.getBody()).getAsJsonObject(); JsonObject messageObject = jsonObject.getAsJsonObject("result").getAsJsonObject("message"); String content = messageObject.get("content").getAsString(); @@ -93,10 +71,8 @@ public Map recommendMember(RecommendMemberRequestDTO recommendMe } @Transactional public List> recommendSchedule(RecommendScheduleRequestDTO recommendScheduleRequestDTO) { - String url = clovaStudioRecommend.getUrl(); - String apiKeyClovaStudio = clovaStudioRecommend.getApiKeyClovaStudio(); - - + String requestId = clovaStudioRecommend.getRequestScheduleId(); + List messages = new ArrayList<>(); // 첫 번째 메시지 @@ -138,26 +114,8 @@ public List> recommendSchedule(RecommendScheduleRequestDTO r "\"해야할 업무\" (시작날짜 ~ 마감날짜)로만 대답해주세요. 다른 말 없이 역할과 해야할 업무, 시작날짜, 마감날짜만 작성해주세요. 역할 앞에 숫자를 넣지 말고, 업무 앞에 -를 넣지 마세요. "); messages.add(requestMessage); - HttpHeaders headers = new HttpHeaders(); - headers.set("X-NCP-CLOVASTUDIO-API-KEY", apiKeyClovaStudio); - headers.set("X-NCP-APIGW-API-KEY", clovaStudioRecommend.getApiGateWayKey()); - headers.set("X-NCP-CLOVASTUDIO-REQUEST-ID", clovaStudioRecommend.getRequestScheduleId()); - headers.set("Content-Type", "application/json"); - - Map requestBody = new HashMap<>(); - requestBody.put("messages", messages); - requestBody.put("topP", 0.8); - requestBody.put("topK", 0); - requestBody.put("maxTokens", 256); - requestBody.put("temperature", 0.5); - requestBody.put("repeatPenalty", 5.0); - requestBody.put("includeAiFilters", "True"); - - HttpEntity> requestEntity = new HttpEntity<>(requestBody, headers); - - RestTemplate restTemplate = new RestTemplate(); - ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); - + ResponseEntity responseEntity = fetchClovaAPI(messages,requestId); + JsonObject jsonObject = JsonParser.parseString(responseEntity.getBody()).getAsJsonObject(); JsonObject messageObject = jsonObject.getAsJsonObject("result").getAsJsonObject("message"); String content = messageObject.get("content").getAsString(); @@ -178,7 +136,6 @@ public List> recommendSchedule(RecommendScheduleRequestDTO r Matcher roleMatcher = roleRegex.matcher(role); if (roleMatcher.matches()) { - String roleNumber = roleMatcher.group(1); // 1. role = roleMatcher.group(2); // 기획자 // 숫자와 점을 제거한 role @@ -259,6 +216,7 @@ public List> recommendSchedule(RecommendScheduleRequestDTO r @Transactional public void insertCalender(Long projectId, List> roleScheduleList) { + Project project = projectRepository.findById(projectId).orElseThrow(()->new NoSuchElementException("[ERROR] 해당하는 프로젝트가 존재하지 않습니다.")); for(Map map : roleScheduleList){ Role r = new Role(); @@ -274,4 +232,31 @@ public void insertCalender(Long projectId, List> roleSchedul } } + public ResponseEntity fetchClovaAPI(List messages, String requestId){ + String url = clovaStudioRecommend.getUrl(); + String apiKeyClovaStudio = clovaStudioRecommend.getApiKeyClovaStudio(); + String apiGateWayKey = clovaStudioRecommend.getApiGateWayKey(); + + HttpHeaders headers = new HttpHeaders(); + headers.set("X-NCP-CLOVASTUDIO-API-KEY", apiKeyClovaStudio); + headers.set("X-NCP-APIGW-API-KEY", apiGateWayKey); + headers.set("X-NCP-CLOVASTUDIO-REQUEST-ID", requestId); + headers.set("Content-Type", "application/json"); + + Map requestBody = new HashMap<>(); + requestBody.put("messages", messages); + requestBody.put("topP", 0.8); + requestBody.put("topK", 0); + requestBody.put("maxTokens", 256); + requestBody.put("temperature", 0.5); + requestBody.put("repeatPenalty", 5.0); + requestBody.put("includeAiFilters", "True"); + + HttpEntity> requestEntity = new HttpEntity<>(requestBody, headers); + + RestTemplate restTemplate = new RestTemplate(); + return restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); + + } + } diff --git a/src/main/java/mvc/promiseme/users/controller/UserController.java b/src/main/java/mvc/promiseme/users/controller/UserController.java index c8a61a7..a8c7f0f 100644 --- a/src/main/java/mvc/promiseme/users/controller/UserController.java +++ b/src/main/java/mvc/promiseme/users/controller/UserController.java @@ -9,7 +9,6 @@ import org.springframework.http.ResponseEntity; 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 java.util.Map; From 3d865c70489c9535bcf10c6c68a07874f4ae6e1c Mon Sep 17 00:00:00 2001 From: UU_jeong <80961726+oU-Ua@users.noreply.github.com> Date: Tue, 19 Dec 2023 21:17:58 +0900 Subject: [PATCH 2/2] refactor : separate fetch APi method --- .../mvc/promiseme/calendar/repository/CalendarRepository.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java b/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java index c7af2b5..5fd05e0 100644 --- a/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java +++ b/src/main/java/mvc/promiseme/calendar/repository/CalendarRepository.java @@ -6,9 +6,13 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import java.time.LocalDate; import java.util.List; +import java.util.Optional; + public interface CalendarRepository extends JpaRepository { // @Query("SELECT NEW mvc.promiseme.calendar.dto.CalendarResponseDTO(c.content, c.startDate, c.finishDate, u.nickname) from Calendar c join c.member m join m.users u where c.project = :project") // List findByProject(Project project); +List findByProjectAndStartDateLessThanEqualAndFinishDateGreaterThanEqual(Project project, LocalDate startDate, LocalDate finishDate); }