diff --git a/src/main/java/com/specialwarriors/conal/contribution/scheduler/ContributionScheduler.java b/src/main/java/com/specialwarriors/conal/contribution/scheduler/ContributionScheduler.java index 44384c0..939a71b 100644 --- a/src/main/java/com/specialwarriors/conal/contribution/scheduler/ContributionScheduler.java +++ b/src/main/java/com/specialwarriors/conal/contribution/scheduler/ContributionScheduler.java @@ -3,6 +3,7 @@ import com.specialwarriors.conal.contribution.service.ContributionService; import com.specialwarriors.conal.contributor.domain.Contributor; import com.specialwarriors.conal.github_repo.domain.GithubRepo; +import com.specialwarriors.conal.github_repo.repository.GithubRepoRepository; import com.specialwarriors.conal.notification.domain.NotificationAgreement; import com.specialwarriors.conal.notification.enums.NotificationType; import com.specialwarriors.conal.notification.service.NotificationAgreementQuery; @@ -17,6 +18,7 @@ public class ContributionScheduler { private final NotificationAgreementQuery notificationAgreementQuery; + private final GithubRepoRepository githubRepoRepository; private final ContributionService contributionService; private final MailUtil mailUtil; @@ -26,10 +28,12 @@ public void sendContribution() { List notificationAgreements = notificationAgreementQuery .findAllByType(NotificationType.CONTRIBUTION); - List githubRepos = notificationAgreements.stream() - .map(NotificationAgreement::getGithubRepo) + List githubRepoIds = notificationAgreements.stream() + .map(NotificationAgreement::getGithubRepoId) .toList(); + List githubRepos = githubRepoRepository.findAllById(githubRepoIds); + for (GithubRepo githubRepo : githubRepos) { for (Contributor contributor : githubRepo.getContributors()) { mailUtil.sendContributionForm( diff --git a/src/main/java/com/specialwarriors/conal/github_repo/controller/GithubRepoController.java b/src/main/java/com/specialwarriors/conal/github_repo/controller/GithubRepoController.java index 6023c59..125235e 100644 --- a/src/main/java/com/specialwarriors/conal/github_repo/controller/GithubRepoController.java +++ b/src/main/java/com/specialwarriors/conal/github_repo/controller/GithubRepoController.java @@ -3,7 +3,6 @@ import com.specialwarriors.conal.github.service.GitHubService; import com.specialwarriors.conal.github_repo.dto.request.GithubRepoCreateRequest; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoCreateResponse; -import com.specialwarriors.conal.github_repo.dto.response.GithubRepoDeleteResponse; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoGetResponse; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoPageResponse; import com.specialwarriors.conal.github_repo.service.GithubRepoService; @@ -30,7 +29,7 @@ public class GithubRepoController { @GetMapping("/new") public String showCreateForm(@SessionAttribute Long userId, Model model) { model.addAttribute("repoRequest", - new GithubRepoCreateRequest(userId, "", "", null, Set.of())); + new GithubRepoCreateRequest(userId, "", "", null, Set.of())); model.addAttribute("userId", userId); return "repo/form"; @@ -39,7 +38,7 @@ public String showCreateForm(@SessionAttribute Long userId, Model model) { // 저장 (POST) @PostMapping public String createGitHubRepo(@SessionAttribute Long userId, - @ModelAttribute GithubRepoCreateRequest request) { + @ModelAttribute GithubRepoCreateRequest request) { GithubRepoCreateResponse response = githubRepoService.createGithubRepo(userId, request); gitHubService.updateRepoContribution(response.owner(), response.repo()).subscribe(); @@ -49,8 +48,8 @@ public String createGitHubRepo(@SessionAttribute Long userId, // 목록 조회 (GET) @GetMapping public String getGithubRepos(@SessionAttribute Long userId, - @RequestParam(defaultValue = "0") int page, - Model model) { + @RequestParam(defaultValue = "0") int page, + Model model) { GithubRepoPageResponse response = githubRepoService.getGithubRepoInfos(userId, page); model.addAttribute("repositories", response); @@ -62,8 +61,8 @@ public String getGithubRepos(@SessionAttribute Long userId, // 단일 조회 (GET) @GetMapping("/{repositoryId}") public String getRepositoryId(@SessionAttribute Long userId, - @PathVariable long repositoryId, - Model model) { + @PathVariable long repositoryId, + Model model) { GithubRepoGetResponse response = githubRepoService.getGithubRepoInfo(userId, repositoryId); model.addAttribute("repoInfo", response); @@ -72,8 +71,8 @@ public String getRepositoryId(@SessionAttribute Long userId, @PostMapping("/{repositoryId}") public String deleteRepository(@SessionAttribute Long userId, - @PathVariable long repositoryId) { - GithubRepoDeleteResponse response = githubRepoService.deleteRepo(userId, repositoryId); + @PathVariable long repositoryId) { + githubRepoService.deleteRepo(userId, repositoryId); return "redirect:/home"; } diff --git a/src/main/java/com/specialwarriors/conal/github_repo/domain/GithubRepo.java b/src/main/java/com/specialwarriors/conal/github_repo/domain/GithubRepo.java index fa46cce..06b56b5 100644 --- a/src/main/java/com/specialwarriors/conal/github_repo/domain/GithubRepo.java +++ b/src/main/java/com/specialwarriors/conal/github_repo/domain/GithubRepo.java @@ -43,9 +43,6 @@ public class GithubRepo { @OneToMany(mappedBy = "githubRepo", cascade = CascadeType.ALL) private List contributors = new ArrayList<>(); - @OneToMany(mappedBy = "githubRepo") - private List notificationAgreements = new ArrayList<>(); - @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; @@ -65,13 +62,13 @@ public void addContributors(List contributors) { } } - public void setNotificationAgreement(List agreements) { - for (NotificationAgreement agreement : agreements) { - this.notificationAgreements.add(agreement); - agreement.setGitHubRepo(this); - } + public void assignRepoIdToNotificationAgreements( + List notificationAgreements) { + + notificationAgreements.forEach(agreement -> agreement.setGitHubRepoId(this.getId())); } + public void setUser(User user) { this.user = user; user.addGithubRepo(this); diff --git a/src/main/java/com/specialwarriors/conal/github_repo/dto/GithubRepoMapper.java b/src/main/java/com/specialwarriors/conal/github_repo/dto/GithubRepoMapper.java index b3ad718..a819ee1 100644 --- a/src/main/java/com/specialwarriors/conal/github_repo/dto/GithubRepoMapper.java +++ b/src/main/java/com/specialwarriors/conal/github_repo/dto/GithubRepoMapper.java @@ -4,7 +4,6 @@ import com.specialwarriors.conal.github_repo.domain.GithubRepo; import com.specialwarriors.conal.github_repo.dto.request.GithubRepoCreateRequest; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoCreateResponse; -import com.specialwarriors.conal.github_repo.dto.response.GithubRepoDeleteResponse; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoGetResponse; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoPageResponse; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoPageResponse.GithubRepoSummary; @@ -18,6 +17,7 @@ public interface GithubRepoMapper { default GithubRepoCreateResponse toGithubRepoCreateResponse(String owner, String reponame) { + return new GithubRepoCreateResponse( owner, reponame @@ -26,12 +26,12 @@ default GithubRepoCreateResponse toGithubRepoCreateResponse(String owner, default GithubRepoGetResponse toGithubRepoGetResponse(GithubRepo repo, String owner, String reponame, Long userId) { + return new GithubRepoGetResponse( userId, repo.getId(), repo.getName(), repo.getUrl(), - repo.getNotificationAgreements(), repo.getEndDate(), owner, reponame @@ -42,6 +42,7 @@ default GithubRepoGetResponse toGithubRepoGetResponse(GithubRepo repo, String ow GithubRepoSummary toGithubRepoSummary(GithubRepo repo); default GithubRepoPageResponse toGithubRepoPageResponse(Page page, Long userId) { + return new GithubRepoPageResponse( page.getContent().stream() .map(this::toGithubRepoSummary) @@ -52,11 +53,5 @@ default GithubRepoPageResponse toGithubRepoPageResponse(Page page, L page.getTotalElements() ); } - - default GithubRepoDeleteResponse toGithubDeleteRepoResponse() { - return new GithubRepoDeleteResponse( - true - ); - } - + } diff --git a/src/main/java/com/specialwarriors/conal/github_repo/dto/response/GithubRepoDeleteResponse.java b/src/main/java/com/specialwarriors/conal/github_repo/dto/response/GithubRepoDeleteResponse.java deleted file mode 100644 index 413d87e..0000000 --- a/src/main/java/com/specialwarriors/conal/github_repo/dto/response/GithubRepoDeleteResponse.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.specialwarriors.conal.github_repo.dto.response; - -public record GithubRepoDeleteResponse( - boolean result -) { - -} diff --git a/src/main/java/com/specialwarriors/conal/github_repo/dto/response/GithubRepoGetResponse.java b/src/main/java/com/specialwarriors/conal/github_repo/dto/response/GithubRepoGetResponse.java index 3f7fe27..1d02b9b 100644 --- a/src/main/java/com/specialwarriors/conal/github_repo/dto/response/GithubRepoGetResponse.java +++ b/src/main/java/com/specialwarriors/conal/github_repo/dto/response/GithubRepoGetResponse.java @@ -1,15 +1,12 @@ package com.specialwarriors.conal.github_repo.dto.response; -import com.specialwarriors.conal.notification.domain.NotificationAgreement; import java.time.LocalDate; -import java.util.List; public record GithubRepoGetResponse( Long userId, Long repoId, String name, String url, - List agreement, LocalDate endDate, String owner, String repo diff --git a/src/main/java/com/specialwarriors/conal/github_repo/service/GithubRepoService.java b/src/main/java/com/specialwarriors/conal/github_repo/service/GithubRepoService.java index 73bc5e5..94a3cc3 100644 --- a/src/main/java/com/specialwarriors/conal/github_repo/service/GithubRepoService.java +++ b/src/main/java/com/specialwarriors/conal/github_repo/service/GithubRepoService.java @@ -7,7 +7,6 @@ import com.specialwarriors.conal.github_repo.dto.GithubRepoMapper; import com.specialwarriors.conal.github_repo.dto.request.GithubRepoCreateRequest; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoCreateResponse; -import com.specialwarriors.conal.github_repo.dto.response.GithubRepoDeleteResponse; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoGetResponse; import com.specialwarriors.conal.github_repo.dto.response.GithubRepoPageResponse; import com.specialwarriors.conal.github_repo.exception.GithubRepoException; @@ -54,7 +53,7 @@ public GithubRepoCreateResponse createGithubRepo(Long userId, GithubRepoCreateRe GithubRepo githubRepo = githubRepoMapper.toGithubRepo(request); githubRepo.setUser(user); githubRepo.addContributors(contributors); - githubRepo.setNotificationAgreement(agreements); + githubRepo.assignRepoIdToNotificationAgreements(agreements); githubRepo = githubRepoRepository.save(githubRepo); String[] ownerAndRepo = UrlUtil.urlToOwnerAndReponame(githubRepo.getUrl()); @@ -79,6 +78,7 @@ private List createAndSaveContributors(Set emails) { } private List createAndAttachNotifications() { + return notificationAgreementRepository.saveAll( List.of( new NotificationAgreement(NotificationType.VOTE), @@ -106,12 +106,10 @@ public GithubRepoPageResponse getGithubRepoInfos(Long userId, int page) { } @Transactional - public GithubRepoDeleteResponse deleteRepo(Long userId, Long repositoryId) { + public void deleteRepo(Long userId, Long repositoryId) { GithubRepo repo = githubRepoQuery.findByUserIdAndRepositoryId(userId, repositoryId); contributorRepository.deleteAllByGithubRepo(repo); - notificationAgreementRepository.deleteByGithubRepo(repo); + notificationAgreementRepository.deleteByGithubRepoId(repo.getId()); githubRepoRepository.delete(repo); - - return githubRepoMapper.toGithubDeleteRepoResponse(); } } diff --git a/src/main/java/com/specialwarriors/conal/notification/domain/NotificationAgreement.java b/src/main/java/com/specialwarriors/conal/notification/domain/NotificationAgreement.java index 630fc44..ea57acf 100644 --- a/src/main/java/com/specialwarriors/conal/notification/domain/NotificationAgreement.java +++ b/src/main/java/com/specialwarriors/conal/notification/domain/NotificationAgreement.java @@ -1,16 +1,13 @@ package com.specialwarriors.conal.notification.domain; -import com.specialwarriors.conal.github_repo.domain.GithubRepo; import com.specialwarriors.conal.notification.converter.NotificationTypeConverter; import com.specialwarriors.conal.notification.enums.NotificationType; +import jakarta.persistence.Column; import jakarta.persistence.Convert; import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.AccessLevel; import lombok.Getter; @@ -28,9 +25,8 @@ public class NotificationAgreement { private boolean isAgree; - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "github_repo_id") - private GithubRepo githubRepo; + @Column(name = "github_repo_id") + private long githubRepoId; @Convert(converter = NotificationTypeConverter.class) private NotificationType notificationType; @@ -43,8 +39,8 @@ public void disagree() { this.isAgree = false; } - public void setGitHubRepo(GithubRepo githubRepo) { - this.githubRepo = githubRepo; + public void setGitHubRepoId(Long githubRepoId) { + this.githubRepoId = githubRepoId; } public NotificationAgreement(NotificationType notificationType) { diff --git a/src/main/java/com/specialwarriors/conal/notification/repository/NotificationAgreementRepository.java b/src/main/java/com/specialwarriors/conal/notification/repository/NotificationAgreementRepository.java index d7c6a09..9daccd4 100644 --- a/src/main/java/com/specialwarriors/conal/notification/repository/NotificationAgreementRepository.java +++ b/src/main/java/com/specialwarriors/conal/notification/repository/NotificationAgreementRepository.java @@ -1,6 +1,5 @@ package com.specialwarriors.conal.notification.repository; -import com.specialwarriors.conal.github_repo.domain.GithubRepo; import com.specialwarriors.conal.notification.domain.NotificationAgreement; import com.specialwarriors.conal.notification.enums.NotificationType; import java.util.List; @@ -8,12 +7,13 @@ import org.springframework.stereotype.Repository; @Repository -public interface NotificationAgreementRepository extends JpaRepository { +public interface NotificationAgreementRepository extends + JpaRepository { - List findAllByGithubRepoAndNotificationType(GithubRepo githubRepo, - NotificationType notificationType); + List findAllByGithubRepoIdAndNotificationType(Long githubRepoId, + NotificationType notificationType); List findAllByNotificationType(NotificationType notificationType); - void deleteByGithubRepo(GithubRepo repo); + void deleteByGithubRepoId(long githubRepoId); } diff --git a/src/main/java/com/specialwarriors/conal/notification/service/NotificationAgreementQuery.java b/src/main/java/com/specialwarriors/conal/notification/service/NotificationAgreementQuery.java index 5c7bbe0..399dba0 100644 --- a/src/main/java/com/specialwarriors/conal/notification/service/NotificationAgreementQuery.java +++ b/src/main/java/com/specialwarriors/conal/notification/service/NotificationAgreementQuery.java @@ -22,7 +22,7 @@ public NotificationAgreement findByGithubRepoAndType(GithubRepo githubRepo, NotificationType type) { return notificationAgreementRepository - .findAllByGithubRepoAndNotificationType(githubRepo, type) + .findAllByGithubRepoIdAndNotificationType(githubRepo.getId(), type) .stream() .findFirst() .orElseThrow(() -> new GeneralException( diff --git a/src/main/java/com/specialwarriors/conal/vote/scheduler/VoteScheduler.java b/src/main/java/com/specialwarriors/conal/vote/scheduler/VoteScheduler.java index dc5d1f6..d417150 100644 --- a/src/main/java/com/specialwarriors/conal/vote/scheduler/VoteScheduler.java +++ b/src/main/java/com/specialwarriors/conal/vote/scheduler/VoteScheduler.java @@ -1,6 +1,5 @@ package com.specialwarriors.conal.vote.scheduler; -import com.specialwarriors.conal.github_repo.domain.GithubRepo; import com.specialwarriors.conal.notification.domain.NotificationAgreement; import com.specialwarriors.conal.notification.enums.NotificationType; import com.specialwarriors.conal.notification.service.NotificationAgreementQuery; @@ -23,7 +22,7 @@ public class VoteScheduler { @Scheduled(cron = "0 0 9 ? * FRI") public void openWeeklyVote() { List notificationAgreements = notificationAgreementQuery - .findAllByType(NotificationType.VOTE); + .findAllByType(NotificationType.VOTE); List repositoryIds = extractGithubRepoIdsFrom(notificationAgreements); @@ -33,7 +32,7 @@ public void openWeeklyVote() { @Scheduled(cron = "0 0 18 ? * FRI") public void sendWeeklyVoteForm() { List notificationAgreements = notificationAgreementQuery - .findAllByType(NotificationType.VOTE); + .findAllByType(NotificationType.VOTE); List repositoryIds = extractGithubRepoIdsFrom(notificationAgreements); @@ -45,12 +44,12 @@ public void sendWeeklyVoteForm() { @Scheduled(cron = "0 0 9 ? * MON") public void sendWeeklyVoteResult() { List notificationAgreements = notificationAgreementQuery - .findAllByType(NotificationType.VOTE); + .findAllByType(NotificationType.VOTE); List repositoryIds = extractGithubRepoIdsFrom(notificationAgreements); List voteResults = repositoryIds.stream() - .map(voteService::getVoteResult) - .toList(); + .map(voteService::getVoteResult) + .toList(); for (VoteResultResponse voteResult : voteResults) { List emails = voteResult.emails(); @@ -60,11 +59,11 @@ public void sendWeeklyVoteResult() { private List extractGithubRepoIdsFrom( - List notificationAgreements) { + List notificationAgreements) { return notificationAgreements.stream() - .map(NotificationAgreement::getGithubRepo) - .map(GithubRepo::getId) - .toList(); + .map(NotificationAgreement::getGithubRepoId) + .distinct() + .toList(); } }