diff --git a/src/main/java/the_monitor/application/dto/response/KeywordAndIdResponse.java b/src/main/java/the_monitor/application/dto/response/KeywordAndIdResponse.java new file mode 100644 index 0000000..a4a5236 --- /dev/null +++ b/src/main/java/the_monitor/application/dto/response/KeywordAndIdResponse.java @@ -0,0 +1,23 @@ +package the_monitor.application.dto.response; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class KeywordAndIdResponse { + + private Long keywordId; + private String keywordName; + + @Builder + public KeywordAndIdResponse(Long keywordId, + String keywordName) { + + this.keywordId = keywordId; + this.keywordName = keywordName; + + } + +} diff --git a/src/main/java/the_monitor/application/dto/response/KeywordResponse.java b/src/main/java/the_monitor/application/dto/response/KeywordResponse.java index 8eeeab6..cb81a8f 100644 --- a/src/main/java/the_monitor/application/dto/response/KeywordResponse.java +++ b/src/main/java/the_monitor/application/dto/response/KeywordResponse.java @@ -13,12 +13,16 @@ @NoArgsConstructor public class KeywordResponse { - @JsonInclude - private Map> keywordsByCategory; + private CategoryType categoryType; + private List keywordAndIdResponses; @Builder - public KeywordResponse(Map> keywordsByCategory) { - this.keywordsByCategory = keywordsByCategory; + public KeywordResponse(CategoryType categoryType, + List keywordAndIdResponses) { + + this.categoryType = categoryType; + this.keywordAndIdResponses = keywordAndIdResponses; + } } diff --git a/src/main/java/the_monitor/application/service/KeywordService.java b/src/main/java/the_monitor/application/service/KeywordService.java index 167f5da..5189c4d 100644 --- a/src/main/java/the_monitor/application/service/KeywordService.java +++ b/src/main/java/the_monitor/application/service/KeywordService.java @@ -10,10 +10,10 @@ public interface KeywordService { - KeywordResponse getKeywords(Long clientId); + List getKeywords(Long clientId); List getKeywordByAccountIdAndClientIdAndCategoryType(Long accountId, Long clientId, CategoryType categoryType); - KeywordResponse updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest); + List updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest); } diff --git a/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java b/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java index 96abff7..cfc47dc 100644 --- a/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java +++ b/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import the_monitor.application.dto.request.KeywordUpdateRequest; +import the_monitor.application.dto.response.KeywordAndIdResponse; import the_monitor.application.dto.response.KeywordResponse; import the_monitor.application.service.KeywordService; import the_monitor.common.ApiException; @@ -33,27 +34,11 @@ public class KeywordServiceImpl implements KeywordService { @Override - public KeywordResponse getKeywords(Long clientId) { + public List getKeywords(Long clientId) { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal(); // CustomUserDetails 캐스팅 - - Long accountId = userDetails.getAccountId(); - - Map> keywordsByCategory = new HashMap<>(); - - for (CategoryType categoryType : CategoryType.values()) { - List keywords = keywordRepository.findKeywordByAccountIdAndClientIdAndCategoryType(accountId, clientId, categoryType); + Long accountId = getAccountIdFromAuthentication(); - List keywordList = keywords.stream() - .map(Keyword::getKeyword) - .collect(Collectors.toList()); - keywordsByCategory.put(categoryType, keywordList); - } - - return KeywordResponse.builder() - .keywordsByCategory(keywordsByCategory) - .build(); + return getKeywordResponses(accountId, clientId); } @@ -66,24 +51,67 @@ public List getKeywordByAccountIdAndClientIdAndCategoryType(Long accoun @Transactional @Override - public KeywordResponse updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest) { + public List updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest) { + Map> keywordsByCategory = keywordUpdateRequest.getKeywordsByCategory(); - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal(); - Long accountId = userDetails.getAccountId(); + Long accountId = getAccountIdFromAuthentication(); - Client client = clientRepository.findById(clientId) - .orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND)); + // 클라이언트 확인 + Client client = findClientById(clientId); + // 기존 키워드 삭제 keywordRepository.deleteAllByClientId(clientId); + // 새로운 키워드 저장 for (Map.Entry> entry : keywordsByCategory.entrySet()) { categoryServiceImpl.saveCategoryWithKeywords(entry.getKey(), entry.getValue(), client); } - return KeywordResponse.builder() - .keywordsByCategory(keywordsByCategory) - .build(); + return getKeywordResponses(accountId, clientId); + + } + + private Long getAccountIdFromAuthentication() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal(); + return userDetails.getAccountId(); } + + private Client findClientById(Long clientId) { + return clientRepository.findById(clientId) + .orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND)); + } + + + private List getKeywordResponses(Long accountId, Long clientId) { + // 갱신된 키워드 가져오기 + List keywords = findKeywordByAccountIdAndClientId(accountId, clientId); + + // CategoryType별로 키워드를 그룹화 + Map> keywordsByCategoryType = getKeywordsByCategoryType(keywords); + + return keywordsByCategoryType.entrySet().stream() + .map(entry -> KeywordResponse.builder() + .categoryType(entry.getKey()) + .keywordAndIdResponses(entry.getValue().stream() + .map(keyword -> KeywordAndIdResponse.builder() + .keywordId(keyword.getId()) + .keywordName(keyword.getKeyword()) + .build()) + .toList()) + .build()) + .toList(); + + } + + private List findKeywordByAccountIdAndClientId(Long accountId, Long clientId) { + return keywordRepository.findKeywordByAccountIdAndClientId(accountId, clientId); + } + + private Map> getKeywordsByCategoryType(List keywords) { + return keywords.stream() + .collect(Collectors.groupingBy(keyword -> keyword.getCategory().getCategoryType())); + } + } diff --git a/src/main/java/the_monitor/application/serviceImpl/ReportServiceImpl.java b/src/main/java/the_monitor/application/serviceImpl/ReportServiceImpl.java index 5fbf61f..a24a6fb 100644 --- a/src/main/java/the_monitor/application/serviceImpl/ReportServiceImpl.java +++ b/src/main/java/the_monitor/application/serviceImpl/ReportServiceImpl.java @@ -67,13 +67,11 @@ public String createReports(Long clientId, ReportCreateRequest request, Multipar logoUrl = s3Service.uploadFile(logo); } - Report report = reportRepository.save(request.toEntity(client, logoUrl)); + Report report = reportRepository.save(request.toEntity(client, logoUrl)); // 각 카테고리별로 ReportArticle 생성 및 저장 createAndSaveReportArticlesByCategories(report, request); - reportRepository.save(report); // Report와 관련된 ReportArticles 자동 저장 - return "보고서 생성 성공"; } @@ -216,6 +214,9 @@ public List searchReport(Long clientId, String searchTitle) // ReportCreateRequest로부터 ReportArticle 생성 및 저장 private void createAndSaveReportArticlesByCategories(Report report, ReportCreateRequest request) { + + List reportArticleList = new ArrayList<>(); + request.getReportArticles().forEach(reportCategoryArticleDto -> { CategoryType categoryType = reportCategoryArticleDto.getCategoryType(); @@ -234,13 +235,14 @@ private void createAndSaveReportArticlesByCategories(Report report, ReportCreate reportArticleRepository.save(reportArticle); - // Report에 추가 - report.addReportArticle(reportArticle); + reportArticleList.add(reportArticle); }); }); + report.addReportArticles(reportArticleList); + } // ReportArticle 리스트를 CategoryType으로 그룹화 diff --git a/src/main/java/the_monitor/domain/model/Report.java b/src/main/java/the_monitor/domain/model/Report.java index c1a5aa3..7bfc157 100644 --- a/src/main/java/the_monitor/domain/model/Report.java +++ b/src/main/java/the_monitor/domain/model/Report.java @@ -52,8 +52,8 @@ public Report(String title, } - public void addReportArticle(ReportArticle reportArticles) { - this.reportArticles.add(reportArticles); + public void addReportArticles(List reportArticles) { + this.reportArticles = reportArticles; } public void updateTitle(String title) { diff --git a/src/main/java/the_monitor/presentation/KeywordController.java b/src/main/java/the_monitor/presentation/KeywordController.java index 978e120..d854dec 100644 --- a/src/main/java/the_monitor/presentation/KeywordController.java +++ b/src/main/java/the_monitor/presentation/KeywordController.java @@ -21,13 +21,13 @@ public class KeywordController { @Operation(summary = "키워드 리스트 반환", description = "clientId에 따른 키워드를 반환합니다.") @GetMapping() - public ApiResponse getKeywords(@RequestParam("clientId") Long clientId) { + public ApiResponse> getKeywords(@RequestParam("clientId") Long clientId) { return ApiResponse.onSuccessData("keyword 리스트", keywordService.getKeywords(clientId)); } @PutMapping() @Operation(summary = "키워드 업데이트", description = "clientId에 따른 키워드를 업데이트합니다.") - public ApiResponse updateKeywords( + public ApiResponse> updateKeywords( @RequestParam("clientId") Long clientId, @RequestBody KeywordUpdateRequest keywordUpdateRequest) {