From e9dd02431538b9e74548a936fc4cf2a9ecd69881 Mon Sep 17 00:00:00 2001 From: JIHYUN2EE <145944218+JIHYUN2EE@users.noreply.github.com> Date: Tue, 19 Nov 2024 18:28:05 +0900 Subject: [PATCH] =?UTF-8?q?Feature:=20=ED=82=A4=EC=9B=8C=EB=93=9C=20?= =?UTF-8?q?=EB=B0=8F=20=EC=9D=B4=EB=A9=94=EC=9D=BC=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?(#88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: settings 기능 구현 * feat: settings 기능 구현 * fix: 피드백 수정 * fix: @transcational(readOnly 추가) --- .../dto/request/ClientRequest.java | 2 +- .../dto/request/EmailUpdateRequest.java | 11 ++ .../dto/request/KeywordUpdateRequest.java | 16 +++ .../dto/response/EmailResponse.java | 20 ++++ .../application/service/CategoryService.java | 16 +++ .../application/service/EmailService.java | 9 ++ .../application/service/KeywordService.java | 4 + .../serviceImpl/CategoryServiceImpl.java | 53 +++++++++ .../serviceImpl/ClientServiceImpl.java | 77 ++----------- .../serviceImpl/EmailServiceImpl.java | 104 ++++++++++++++++++ .../serviceImpl/KeywordServiceImpl.java | 30 ++++- .../repository/ClientMailCCRepository.java | 12 ++ .../ClientMailRecipientRepository.java | 13 +++ .../domain/repository/KeywordRepository.java | 6 + .../presentation/EmailController.java | 31 ++++++ .../presentation/KeywordController.java | 21 +++- 16 files changed, 352 insertions(+), 73 deletions(-) create mode 100644 src/main/java/the_monitor/application/dto/request/EmailUpdateRequest.java create mode 100644 src/main/java/the_monitor/application/dto/request/KeywordUpdateRequest.java create mode 100644 src/main/java/the_monitor/application/dto/response/EmailResponse.java create mode 100644 src/main/java/the_monitor/application/service/CategoryService.java create mode 100644 src/main/java/the_monitor/application/serviceImpl/CategoryServiceImpl.java create mode 100644 src/main/java/the_monitor/presentation/EmailController.java diff --git a/src/main/java/the_monitor/application/dto/request/ClientRequest.java b/src/main/java/the_monitor/application/dto/request/ClientRequest.java index 1b31c06..24818bd 100644 --- a/src/main/java/the_monitor/application/dto/request/ClientRequest.java +++ b/src/main/java/the_monitor/application/dto/request/ClientRequest.java @@ -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> categoryKeywords; diff --git a/src/main/java/the_monitor/application/dto/request/EmailUpdateRequest.java b/src/main/java/the_monitor/application/dto/request/EmailUpdateRequest.java new file mode 100644 index 0000000..0417357 --- /dev/null +++ b/src/main/java/the_monitor/application/dto/request/EmailUpdateRequest.java @@ -0,0 +1,11 @@ +package the_monitor.application.dto.request; + +import lombok.Getter; +import java.util.List; + +@Getter +public class EmailUpdateRequest { + + private List recipients; + private List ccs; +} \ No newline at end of file diff --git a/src/main/java/the_monitor/application/dto/request/KeywordUpdateRequest.java b/src/main/java/the_monitor/application/dto/request/KeywordUpdateRequest.java new file mode 100644 index 0000000..29ea9a2 --- /dev/null +++ b/src/main/java/the_monitor/application/dto/request/KeywordUpdateRequest.java @@ -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> keywordsByCategory; +} diff --git a/src/main/java/the_monitor/application/dto/response/EmailResponse.java b/src/main/java/the_monitor/application/dto/response/EmailResponse.java new file mode 100644 index 0000000..11c6548 --- /dev/null +++ b/src/main/java/the_monitor/application/dto/response/EmailResponse.java @@ -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 recipients; + private List ccs; + + @Builder + public EmailResponse(List recipients, List ccs) { + this.recipients = recipients; + this.ccs = ccs; + } +} diff --git a/src/main/java/the_monitor/application/service/CategoryService.java b/src/main/java/the_monitor/application/service/CategoryService.java new file mode 100644 index 0000000..39e1e25 --- /dev/null +++ b/src/main/java/the_monitor/application/service/CategoryService.java @@ -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 keywords, Client client); + + // 키워드 생성 + List createKeywords(List keywords, Category category); +} \ No newline at end of file diff --git a/src/main/java/the_monitor/application/service/EmailService.java b/src/main/java/the_monitor/application/service/EmailService.java index 45a13f8..ce92502 100644 --- a/src/main/java/the_monitor/application/service/EmailService.java +++ b/src/main/java/the_monitor/application/service/EmailService.java @@ -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 recipientEmails, List ccEmails, Client client); + + EmailResponse getEmails(Long clientId); + + EmailResponse updateEmails(Long clientId, EmailUpdateRequest emailUpdateRequest); } diff --git a/src/main/java/the_monitor/application/service/KeywordService.java b/src/main/java/the_monitor/application/service/KeywordService.java index a51b4ca..167f5da 100644 --- a/src/main/java/the_monitor/application/service/KeywordService.java +++ b/src/main/java/the_monitor/application/service/KeywordService.java @@ -1,10 +1,12 @@ 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 { @@ -12,4 +14,6 @@ public interface KeywordService { List getKeywordByAccountIdAndClientIdAndCategoryType(Long accountId, Long clientId, CategoryType categoryType); + KeywordResponse updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest); + } diff --git a/src/main/java/the_monitor/application/serviceImpl/CategoryServiceImpl.java b/src/main/java/the_monitor/application/serviceImpl/CategoryServiceImpl.java new file mode 100644 index 0000000..500d3b2 --- /dev/null +++ b/src/main/java/the_monitor/application/serviceImpl/CategoryServiceImpl.java @@ -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 keywords, Client client) { + // 카테고리 생성 + Category category = Category.builder() + .categoryType(categoryType) + .client(client) + .build(); + categoryRepository.save(category); + + // 키워드 생성 + List keywordEntities = createKeywords(keywords, category); + keywordRepository.saveAll(keywordEntities); + + category.addKeywords(keywordEntities); + } + + + @Override + public List createKeywords(List keywords, Category category) { + // 키워드 리스트를 Keyword 엔티티로 변환 + return keywords.stream() + .map(keyword -> Keyword.builder() + .keyword(keyword) + .category(category) + .resultCount(0) // 초기 resultCount 설정 + .build()) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/java/the_monitor/application/serviceImpl/ClientServiceImpl.java b/src/main/java/the_monitor/application/serviceImpl/ClientServiceImpl.java index e0f51e6..9069af8 100644 --- a/src/main/java/the_monitor/application/serviceImpl/ClientServiceImpl.java +++ b/src/main/java/the_monitor/application/serviceImpl/ClientServiceImpl.java @@ -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; @@ -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; @@ -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 @@ -79,18 +82,13 @@ public ClientResponse createClient(ClientRequest clientRequest, MultipartFile lo client = clientRepository.save(client); // 카테고리 및 키워드 저장 - Map> categoryKeywordsFromRequest = clientRequest.getCategoryKeywords(); - for (Map.Entry> entry : categoryKeywordsFromRequest.entrySet()) { - CategoryType categoryType = entry.getKey(); - List keywords = entry.getValue(); - saveCategoryWithKeywords(categoryType, keywords, client); // Lambda 문제 해결 + Map> categoryKeywords = clientRequest.getCategoryKeywords(); + for (Map.Entry> entry : categoryKeywords.entrySet()) { + categoryService.saveCategoryWithKeywords(entry.getKey(), entry.getValue(), client); // CategoryService 사용 } // 이메일 수신자와 참조인 저장 - saveEmailRecipients(clientRequest.getRecipientEmails(), clientRequest.getCcEmails(), client); - - Map> categoryKeywords = clientRequest.getCategoryKeywords(); - + emailServiceImpl.saveEmails(clientRequest.getRecipientEmails(), clientRequest.getCcEmails(), client); // ClientResponse 반환 return ClientResponse.builder() @@ -98,7 +96,7 @@ public ClientResponse createClient(ClientRequest clientRequest, MultipartFile lo .name(client.getName()) .managerName(client.getManagerName()) .logoUrl(client.getLogo()) - .categoryKeywords(categoryKeywordsFromRequest) + .categoryKeywords(categoryKeywords) .clientMailRecipients(client.getClientMailRecipients().stream() .map(ClientMailRecipient::getAddress) .collect(Collectors.toList())) @@ -131,32 +129,6 @@ public Client findClientById(Long clientId) { .orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND)); } - private void saveCategoryWithKeywords(CategoryType categoryType, List keywords, Client client) { - // 카테고리 생성 - Category category = Category.builder() - .categoryType(categoryType) - .client(client) - .build(); - categoryRepository.save(category); - - // 키워드 생성 - List keywordEntities = createKeywords(keywords, category); - keywordRepository.saveAll(keywordEntities); - - category.addKeywords(keywordEntities); - } - - - private List createKeywords(List 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"; @@ -180,29 +152,4 @@ private Long getAccountIdFromJwt() { return jwtProvider.getAccountId(token); } - private void saveEmailRecipients(List recipientEmails, List ccEmails, Client client) { - List 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 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); - } - - } \ No newline at end of file diff --git a/src/main/java/the_monitor/application/serviceImpl/EmailServiceImpl.java b/src/main/java/the_monitor/application/serviceImpl/EmailServiceImpl.java index 72413a1..c157453 100644 --- a/src/main/java/the_monitor/application/serviceImpl/EmailServiceImpl.java +++ b/src/main/java/the_monitor/application/serviceImpl/EmailServiceImpl.java @@ -5,11 +5,27 @@ import lombok.RequiredArgsConstructor; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import the_monitor.application.dto.request.EmailUpdateRequest; +import the_monitor.application.dto.response.EmailResponse; import the_monitor.application.service.EmailService; +import the_monitor.common.ApiException; +import the_monitor.common.ErrorStatus; +import the_monitor.domain.model.Client; +import the_monitor.domain.model.ClientMailCC; +import the_monitor.domain.model.ClientMailRecipient; +import the_monitor.domain.repository.ClientMailCCRepository; +import the_monitor.domain.repository.ClientMailRecipientRepository; +import the_monitor.domain.repository.ClientRepository; +import the_monitor.infrastructure.security.CustomUserDetails; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; @Service @Transactional(readOnly = true) @@ -18,6 +34,10 @@ public class EmailServiceImpl implements EmailService { private final JavaMailSender javaMailSender; + private final ClientMailRecipientRepository clientMailRecipientRepository; + private final ClientMailCCRepository clientMailCCRepository; + private final ClientRepository clientRepository; + @Override public void sendEmail(String toEmail, String subject, String body) throws MessagingException, UnsupportedEncodingException { @@ -33,4 +53,88 @@ public void sendEmail(String toEmail, String subject, String body) throws Messag } + @Override + public void saveEmails(List recipientEmails, List ccEmails, Client client) { + // 수신자 이메일 저장 + List 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 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); + } + + @Override + public EmailResponse getEmails(Long clientId) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal(); + Long accountId = userDetails.getAccountId(); + + Client client = clientRepository.findById(clientId) + .orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND)); + + List recipients = clientMailRecipientRepository.findAllByClient(client) + .stream() + .map(ClientMailRecipient::getAddress) + .collect(Collectors.toList()); + + List ccs = clientMailCCRepository.findAllByClient(client) + .stream() + .map(ClientMailCC::getAddress) + .collect(Collectors.toList()); + + return EmailResponse.builder() + .recipients(recipients) + .ccs(ccs) + .build(); + } + + @Override + @Transactional + public EmailResponse updateEmails(Long clientId, EmailUpdateRequest emailUpdateRequest) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal(); + Long accountId = userDetails.getAccountId(); + + Client client = clientRepository.findById(clientId) + .orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND)); + + clientMailRecipientRepository.deleteAllByClient(client); + clientMailCCRepository.deleteAllByClient(client); + + saveEmails(emailUpdateRequest.getRecipients(), emailUpdateRequest.getCcs(), client); + + List recipientEmails = clientMailRecipientRepository.findAllByClient(client) + .stream() + .map(ClientMailRecipient::getAddress) + .collect(Collectors.toList()); + + List ccEmails = clientMailCCRepository.findAllByClient(client) + .stream() + .map(ClientMailCC::getAddress) + .collect(Collectors.toList()); + + + return EmailResponse.builder() + .recipients(recipientEmails) + .ccs(ccEmails) + .build(); + } + } diff --git a/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java b/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java index 5fe635f..9d74c59 100644 --- a/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java +++ b/src/main/java/the_monitor/application/serviceImpl/KeywordServiceImpl.java @@ -6,10 +6,15 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import the_monitor.application.dto.request.KeywordUpdateRequest; import the_monitor.application.dto.response.KeywordResponse; import the_monitor.application.service.KeywordService; +import the_monitor.common.ApiException; +import the_monitor.common.ErrorStatus; import the_monitor.domain.enums.CategoryType; +import the_monitor.domain.model.Client; import the_monitor.domain.model.Keyword; +import the_monitor.domain.repository.ClientRepository; import the_monitor.domain.repository.KeywordRepository; import the_monitor.infrastructure.security.CustomUserDetails; @@ -23,6 +28,8 @@ public class KeywordServiceImpl implements KeywordService { private final KeywordRepository keywordRepository; + private final ClientRepository clientRepository; + private final CategoryServiceImpl categoryServiceImpl; @Override @@ -37,9 +44,7 @@ public KeywordResponse getKeywords(Long clientId) { Map> keywordsByCategory = new HashMap<>(); for (CategoryType categoryType : CategoryType.values()) { - System.out.printf("categoryType: %s", categoryType); List keywords = keywordRepository.findKeywordByAccountIdAndClientIdAndCategoryType(accountId, clientId, categoryType); - System.out.printf("keywords: %s", keywords); List keywordList = keywords.stream() .map(Keyword::getKeyword) @@ -60,5 +65,26 @@ public List getKeywordByAccountIdAndClientIdAndCategoryType(Long accoun } + @Transactional + @Override + public KeywordResponse updateKeywords(Long clientId, KeywordUpdateRequest keywordUpdateRequest) { + Map> keywordsByCategory = keywordUpdateRequest.getKeywordsByCategory(); + + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal(); + Long accountId = userDetails.getAccountId(); + + Client client = clientRepository.findById(clientId) + .orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND)); + + keywordRepository.deleteAllByClientId(clientId); + + for (Map.Entry> entry : keywordsByCategory.entrySet()) { + categoryServiceImpl.saveCategoryWithKeywords(entry.getKey(), entry.getValue(), client); + } + return KeywordResponse.builder() + .keywordsByCategory(keywordsByCategory) + .build(); + } } diff --git a/src/main/java/the_monitor/domain/repository/ClientMailCCRepository.java b/src/main/java/the_monitor/domain/repository/ClientMailCCRepository.java index ddbd6de..d22419a 100644 --- a/src/main/java/the_monitor/domain/repository/ClientMailCCRepository.java +++ b/src/main/java/the_monitor/domain/repository/ClientMailCCRepository.java @@ -1,7 +1,19 @@ package the_monitor.domain.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import the_monitor.domain.model.Client; import the_monitor.domain.model.ClientMailCC; +import java.util.List; + public interface ClientMailCCRepository extends JpaRepository { + @Query("SELECT cmc FROM ClientMailCC cmc WHERE cmc.client = :client") + List findAllByClient(@Param("client") Client client); + + @Modifying + @Query("DELETE FROM ClientMailCC cmc WHERE cmc.client = :client") + void deleteAllByClient(@Param("client") Client client); } diff --git a/src/main/java/the_monitor/domain/repository/ClientMailRecipientRepository.java b/src/main/java/the_monitor/domain/repository/ClientMailRecipientRepository.java index 91871c2..336b5e6 100644 --- a/src/main/java/the_monitor/domain/repository/ClientMailRecipientRepository.java +++ b/src/main/java/the_monitor/domain/repository/ClientMailRecipientRepository.java @@ -1,7 +1,20 @@ package the_monitor.domain.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import the_monitor.domain.model.Client; import the_monitor.domain.model.ClientMailRecipient; +import java.util.List; + public interface ClientMailRecipientRepository extends JpaRepository { + + @Query("SELECT cmr FROM ClientMailRecipient cmr WHERE cmr.client = :client") + List findAllByClient(@Param("client") Client client); + + @Modifying + @Query("DELETE FROM ClientMailRecipient cmr WHERE cmr.client = :client") + void deleteAllByClient(@Param("client") Client client); } diff --git a/src/main/java/the_monitor/domain/repository/KeywordRepository.java b/src/main/java/the_monitor/domain/repository/KeywordRepository.java index bc2b8e5..bd2476d 100644 --- a/src/main/java/the_monitor/domain/repository/KeywordRepository.java +++ b/src/main/java/the_monitor/domain/repository/KeywordRepository.java @@ -1,6 +1,7 @@ package the_monitor.domain.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -33,4 +34,9 @@ List findKeywordByAccountIdAndClientIdAndCategoryType(@Param("accountId @Param("clientId") Long clientId, @Param("categoryType") CategoryType categoryType); + @Modifying + @Query("DELETE FROM Keyword k " + + "WHERE k.category.client.id = :clientId") + void deleteAllByClientId(@Param("clientId") Long clientId); + } \ No newline at end of file diff --git a/src/main/java/the_monitor/presentation/EmailController.java b/src/main/java/the_monitor/presentation/EmailController.java new file mode 100644 index 0000000..4ccd3ec --- /dev/null +++ b/src/main/java/the_monitor/presentation/EmailController.java @@ -0,0 +1,31 @@ +package the_monitor.presentation; + +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; +import the_monitor.application.dto.request.EmailUpdateRequest; +import the_monitor.application.dto.response.EmailResponse; +import the_monitor.application.service.EmailService; +import the_monitor.common.ApiResponse; + +@RestController +@RequestMapping("/api/v1/emails") +@RequiredArgsConstructor +public class EmailController { + private final EmailService emailService; + + @Operation(summary = "이메일 조회", description = "clientId에 따른 이메일 리스트를 조회합니다.") + @GetMapping + public ApiResponse getEmails(@RequestParam("clientId") Long clientId) { + EmailResponse emailResponse = emailService.getEmails(clientId); + return ApiResponse.onSuccessData("이메일 조회 성공", emailService.getEmails(clientId)); + } + + @Operation(summary = "이메일 수정", description = "clientId에 따른 이메일 리스트를 수정합니다.") + @PutMapping + public ApiResponse updateEmails(@RequestParam("clientId") Long clientId, @RequestBody EmailUpdateRequest emailUpdateRequest) { + + return ApiResponse.onSuccessData("이메일 수정 성공", emailService.updateEmails(clientId, emailUpdateRequest)); + } + +} diff --git a/src/main/java/the_monitor/presentation/KeywordController.java b/src/main/java/the_monitor/presentation/KeywordController.java index 832c229..320c6ca 100644 --- a/src/main/java/the_monitor/presentation/KeywordController.java +++ b/src/main/java/the_monitor/presentation/KeywordController.java @@ -2,13 +2,15 @@ import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; -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 org.springframework.web.bind.annotation.*; +import the_monitor.application.dto.request.KeywordUpdateRequest; import the_monitor.application.dto.response.KeywordResponse; import the_monitor.application.service.KeywordService; import the_monitor.common.ApiResponse; +import the_monitor.domain.enums.CategoryType; + +import java.util.List; +import java.util.Map; @RestController @RequiredArgsConstructor @@ -19,8 +21,17 @@ public class KeywordController { @Operation(summary = "키워드 리스트 반환", description = "clientId에 따른 키워드를 반환합니다.") @GetMapping() - public ApiResponse getKeywords(@RequestParam("clientId") Long clientId) { + public ApiResponse gedtKeywords(@RequestParam("clientId") Long clientId) { return ApiResponse.onSuccessData("keyword 리스트", keywordService.getKeywords(clientId)); } + @PutMapping() + @Operation(summary = "키워드 업데이트", description = "clientId에 따른 키워드를 업데이트합니다.") + public ApiResponse updateKeywords( + @RequestParam("clientId") Long clientId, + @RequestBody KeywordUpdateRequest keywordUpdateRequest) { + + KeywordResponse updatedKeywords = keywordService.updateKeywords(clientId, keywordUpdateRequest); + return ApiResponse.onSuccessData("키워드 업데이트 성공", updatedKeywords); + } }