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 19, 2024
2 parents 0f0b81a + e9dd024 commit 21bf510
Show file tree
Hide file tree
Showing 16 changed files with 352 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ClientRequest {
@NotBlank(message = "담당자 이름은 필수 항목입니다.")
private String managerName;

@Schema(description = "카테고리와 키워드 매핑", example = "{ \"SELF\": [\"keyword1\", \"keyword2\"], \"COMPETITOR\": [\"keyword3\"], \"INDUSTRY\": [\"keyword4\"] }")
@Schema(description = "카테고리와 키워드 매핑", example = "{ \"SELF\": [\"keyword1\", \"COMPETITOR\": [\"keyword3\"], \"INDUSTRY\": [\"keyword4\"] }")
@NotNull(message = "카테고리 키워드는 필수 항목입니다.")
private Map<CategoryType, List<String>> categoryKeywords;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package the_monitor.application.dto.request;

import lombok.Getter;
import java.util.List;

@Getter
public class EmailUpdateRequest {

private List<String> recipients;
private List<String> ccs;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package the_monitor.application.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import the_monitor.domain.enums.CategoryType;

import java.util.List;
import java.util.Map;

@Getter
@NoArgsConstructor
public class KeywordUpdateRequest {
@Schema(description = "카테고리와 키워드 매핑", example = "{ \"SELF\": [\"keyword1\", \"keyword2\"], \"COMPETITOR\": [\"keyword3\"], \"INDUSTRY\": [\"keyword4\"] }")
private Map<CategoryType, List<String>> keywordsByCategory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package the_monitor.application.dto.response;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class EmailResponse {
private List<String> recipients;
private List<String> ccs;

@Builder
public EmailResponse(List<String> recipients, List<String> ccs) {
this.recipients = recipients;
this.ccs = ccs;
}
}
16 changes: 16 additions & 0 deletions src/main/java/the_monitor/application/service/CategoryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package the_monitor.application.service;

import the_monitor.domain.enums.CategoryType;
import the_monitor.domain.model.Category;
import the_monitor.domain.model.Client;
import the_monitor.domain.model.Keyword;

import java.util.List;

public interface CategoryService {
// 카테고리 생성 및 키워드 저장
void saveCategoryWithKeywords(CategoryType categoryType, List<String> keywords, Client client);

// 키워드 생성
List<Keyword> createKeywords(List<String> keywords, Category category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@


import jakarta.mail.MessagingException;
import the_monitor.application.dto.request.EmailUpdateRequest;
import the_monitor.application.dto.response.EmailResponse;
import the_monitor.domain.model.Client;

import java.io.UnsupportedEncodingException;
import java.util.List;

public interface EmailService {

void sendEmail(String to, String subject, String text) throws MessagingException, UnsupportedEncodingException;

void saveEmails(List<String> recipientEmails, List<String> ccEmails, Client client);

EmailResponse getEmails(Long clientId);

EmailResponse updateEmails(Long clientId, EmailUpdateRequest emailUpdateRequest);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package the_monitor.application.service;

import the_monitor.application.dto.request.KeywordUpdateRequest;
import the_monitor.application.dto.response.KeywordResponse;
import the_monitor.domain.enums.CategoryType;
import the_monitor.domain.model.Keyword;

import java.util.List;
import java.util.Map;

public interface KeywordService {

KeywordResponse getKeywords(Long clientId);

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

KeywordResponse updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package the_monitor.application.serviceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import the_monitor.application.service.CategoryService;
import the_monitor.domain.enums.CategoryType;
import the_monitor.domain.model.Category;
import the_monitor.domain.model.Client;
import the_monitor.domain.model.Keyword;
import the_monitor.domain.repository.CategoryRepository;
import the_monitor.domain.repository.KeywordRepository;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {

private final CategoryRepository categoryRepository;
private final KeywordRepository keywordRepository;

@Override
public void saveCategoryWithKeywords(CategoryType categoryType, List<String> keywords, Client client) {
// 카테고리 생성
Category category = Category.builder()
.categoryType(categoryType)
.client(client)
.build();
categoryRepository.save(category);

// 키워드 생성
List<Keyword> keywordEntities = createKeywords(keywords, category);
keywordRepository.saveAll(keywordEntities);

category.addKeywords(keywordEntities);
}


@Override
public List<Keyword> createKeywords(List<String> keywords, Category category) {
// 키워드 리스트를 Keyword 엔티티로 변환
return keywords.stream()
.map(keyword -> Keyword.builder()
.keyword(keyword)
.category(category)
.resultCount(0) // 초기 resultCount 설정
.build())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import the_monitor.application.dto.request.ClientRequest;
import the_monitor.application.dto.response.ClientResponse;
import the_monitor.application.dto.response.ReportListResponse;
import the_monitor.application.service.CategoryService;
import the_monitor.application.service.ClientService;
import org.springframework.stereotype.Service;
import the_monitor.application.service.S3Service;
Expand Down Expand Up @@ -35,7 +36,7 @@
public class ClientServiceImpl implements ClientService {

private final ClientRepository clientRepository;
private final CategoryRepository categoryRepository;
private final CategoryService categoryService;
private final AccountRepository accountRepository;
private final KeywordRepository keywordRepository;

Expand All @@ -44,18 +45,20 @@ public class ClientServiceImpl implements ClientService {

private final JwtProvider jwtProvider;
private final S3Service s3Service;
private final EmailServiceImpl emailServiceImpl;

@Autowired
public ClientServiceImpl(ClientRepository clientRepository, CategoryRepository categoryRepository, AccountRepository accountRepository, KeywordRepository keywordRepository,
ClientMailRecipientRepository clientMailRecipientRepository, ClientMailCCRepository clientMailCCRepository, JwtProvider jwtProvider, S3Service s3Service) {
public ClientServiceImpl(ClientRepository clientRepository, CategoryService categoryService, AccountRepository accountRepository, KeywordRepository keywordRepository,
ClientMailRecipientRepository clientMailRecipientRepository, ClientMailCCRepository clientMailCCRepository, JwtProvider jwtProvider, S3Service s3Service, EmailServiceImpl emailServiceImpl) {
this.clientRepository = clientRepository;
this.categoryRepository = categoryRepository;
this.categoryService = categoryService;
this.accountRepository = accountRepository;
this.keywordRepository = keywordRepository;
this.clientMailRecipientRepository = clientMailRecipientRepository;
this.clientMailCCRepository = clientMailCCRepository;
this.jwtProvider = jwtProvider;
this.s3Service = s3Service;
this.emailServiceImpl = emailServiceImpl;
}

@Transactional
Expand All @@ -79,26 +82,21 @@ public ClientResponse createClient(ClientRequest clientRequest, MultipartFile lo
client = clientRepository.save(client);

// 카테고리 및 키워드 저장
Map<CategoryType, List<String>> categoryKeywordsFromRequest = clientRequest.getCategoryKeywords();
for (Map.Entry<CategoryType, List<String>> entry : categoryKeywordsFromRequest.entrySet()) {
CategoryType categoryType = entry.getKey();
List<String> keywords = entry.getValue();
saveCategoryWithKeywords(categoryType, keywords, client); // Lambda 문제 해결
Map<CategoryType, List<String>> categoryKeywords = clientRequest.getCategoryKeywords();
for (Map.Entry<CategoryType, List<String>> entry : categoryKeywords.entrySet()) {
categoryService.saveCategoryWithKeywords(entry.getKey(), entry.getValue(), client); // CategoryService 사용
}

// 이메일 수신자와 참조인 저장
saveEmailRecipients(clientRequest.getRecipientEmails(), clientRequest.getCcEmails(), client);

Map<CategoryType, List<String>> categoryKeywords = clientRequest.getCategoryKeywords();

emailServiceImpl.saveEmails(clientRequest.getRecipientEmails(), clientRequest.getCcEmails(), client);

// ClientResponse 반환
return ClientResponse.builder()
.clientId(client.getId())
.name(client.getName())
.managerName(client.getManagerName())
.logoUrl(client.getLogo())
.categoryKeywords(categoryKeywordsFromRequest)
.categoryKeywords(categoryKeywords)
.clientMailRecipients(client.getClientMailRecipients().stream()
.map(ClientMailRecipient::getAddress)
.collect(Collectors.toList()))
Expand Down Expand Up @@ -131,32 +129,6 @@ public Client findClientById(Long clientId) {
.orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND));
}

private void saveCategoryWithKeywords(CategoryType categoryType, List<String> keywords, Client client) {
// 카테고리 생성
Category category = Category.builder()
.categoryType(categoryType)
.client(client)
.build();
categoryRepository.save(category);

// 키워드 생성
List<Keyword> keywordEntities = createKeywords(keywords, category);
keywordRepository.saveAll(keywordEntities);

category.addKeywords(keywordEntities);
}


private List<Keyword> createKeywords(List<String> keywords, Category category) {
return keywords.stream()
.map(keyword -> Keyword.builder()
.keyword(keyword)
.category(category)
.resultCount(0)
.build())
.collect(Collectors.toList());
}

private String saveLogo(MultipartFile logo) {
// 로고 파일 저장 처리
String directoryPath = "/logo";
Expand All @@ -180,29 +152,4 @@ private Long getAccountIdFromJwt() {
return jwtProvider.getAccountId(token);
}

private void saveEmailRecipients(List<String> recipientEmails, List<String> ccEmails, Client client) {
List<ClientMailRecipient> recipients = new ArrayList<>();
for (String email : recipientEmails) {
ClientMailRecipient recipient = ClientMailRecipient.builder()
.address(email)
.client(client)
.build();
recipients.add(recipient);
}
clientMailRecipientRepository.saveAll(recipients);
client.setClientMailRecipients(recipients);

List<ClientMailCC> ccs = new ArrayList<>();
for (String email : ccEmails) {
ClientMailCC cc = ClientMailCC.builder()
.address(email)
.client(client)
.build();
ccs.add(cc);
}
clientMailCCRepository.saveAll(ccs);
client.setClientMailCCs(ccs);
}


}
Loading

0 comments on commit 21bf510

Please sign in to comment.