Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
L-U-Ready committed Nov 22, 2024
2 parents 14e1e6e + 118de0e commit 08ba9b5
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -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;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
@NoArgsConstructor
public class KeywordResponse {

@JsonInclude
private Map<CategoryType, List<String>> keywordsByCategory;
private CategoryType categoryType;
private List<KeywordAndIdResponse> keywordAndIdResponses;

@Builder
public KeywordResponse(Map<CategoryType, List<String>> keywordsByCategory) {
this.keywordsByCategory = keywordsByCategory;
public KeywordResponse(CategoryType categoryType,
List<KeywordAndIdResponse> keywordAndIdResponses) {

this.categoryType = categoryType;
this.keywordAndIdResponses = keywordAndIdResponses;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

public interface KeywordService {

KeywordResponse getKeywords(Long clientId);
List<KeywordResponse> getKeywords(Long clientId);

List<Keyword> getKeywordByAccountIdAndClientIdAndCategoryType(Long accountId, Long clientId, CategoryType categoryType);

KeywordResponse updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest);
List<KeywordResponse> updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,27 +34,11 @@ public class KeywordServiceImpl implements KeywordService {


@Override
public KeywordResponse getKeywords(Long clientId) {
public List<KeywordResponse> getKeywords(Long clientId) {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal(); // CustomUserDetails 캐스팅

Long accountId = userDetails.getAccountId();

Map<CategoryType, List<String>> keywordsByCategory = new HashMap<>();

for (CategoryType categoryType : CategoryType.values()) {
List<Keyword> keywords = keywordRepository.findKeywordByAccountIdAndClientIdAndCategoryType(accountId, clientId, categoryType);
Long accountId = getAccountIdFromAuthentication();

List<String> keywordList = keywords.stream()
.map(Keyword::getKeyword)
.collect(Collectors.toList());
keywordsByCategory.put(categoryType, keywordList);
}

return KeywordResponse.builder()
.keywordsByCategory(keywordsByCategory)
.build();
return getKeywordResponses(accountId, clientId);

}

Expand All @@ -66,24 +51,67 @@ public List<Keyword> getKeywordByAccountIdAndClientIdAndCategoryType(Long accoun

@Transactional
@Override
public KeywordResponse updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest) {
public List<KeywordResponse> updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest) {

Map<CategoryType, List<String>> 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<CategoryType, List<String>> 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<KeywordResponse> getKeywordResponses(Long accountId, Long clientId) {
// 갱신된 키워드 가져오기
List<Keyword> keywords = findKeywordByAccountIdAndClientId(accountId, clientId);

// CategoryType별로 키워드를 그룹화
Map<CategoryType, List<Keyword>> 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<Keyword> findKeywordByAccountIdAndClientId(Long accountId, Long clientId) {
return keywordRepository.findKeywordByAccountIdAndClientId(accountId, clientId);
}

private Map<CategoryType, List<Keyword>> getKeywordsByCategoryType(List<Keyword> keywords) {
return keywords.stream()
.collect(Collectors.groupingBy(keyword -> keyword.getCategory().getCategoryType()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 "보고서 생성 성공";
}

Expand Down Expand Up @@ -216,6 +214,9 @@ public List<ReportListResponse> searchReport(Long clientId, String searchTitle)
// ReportCreateRequest로부터 ReportArticle 생성 및 저장
private void createAndSaveReportArticlesByCategories(Report report, ReportCreateRequest request) {


List<ReportArticle> reportArticleList = new ArrayList<>();

request.getReportArticles().forEach(reportCategoryArticleDto -> {

CategoryType categoryType = reportCategoryArticleDto.getCategoryType();
Expand All @@ -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으로 그룹화
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/the_monitor/domain/model/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public Report(String title,

}

public void addReportArticle(ReportArticle reportArticles) {
this.reportArticles.add(reportArticles);
public void addReportArticles(List<ReportArticle> reportArticles) {
this.reportArticles = reportArticles;
}

public void updateTitle(String title) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/the_monitor/presentation/KeywordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public class KeywordController {

@Operation(summary = "키워드 리스트 반환", description = "clientId에 따른 키워드를 반환합니다.")
@GetMapping()
public ApiResponse<KeywordResponse> getKeywords(@RequestParam("clientId") Long clientId) {
public ApiResponse<List<KeywordResponse>> getKeywords(@RequestParam("clientId") Long clientId) {
return ApiResponse.onSuccessData("keyword 리스트", keywordService.getKeywords(clientId));
}

@PutMapping()
@Operation(summary = "키워드 업데이트", description = "clientId에 따른 키워드를 업데이트합니다.")
public ApiResponse<KeywordResponse> updateKeywords(
public ApiResponse<List<KeywordResponse>> updateKeywords(
@RequestParam("clientId") Long clientId,
@RequestBody KeywordUpdateRequest keywordUpdateRequest) {

Expand Down

0 comments on commit 08ba9b5

Please sign in to comment.