From bbded2ce9fced931a7342f361c01f041de716c95 Mon Sep 17 00:00:00 2001 From: devxb Date: Fri, 9 Feb 2024 15:43:41 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20flyway=20schema=EC=97=90=20=5F?= =?UTF-8?q?=EC=9D=B4=20=ED=95=98=EB=82=98=EB=B0=96=EC=97=90=20=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EB=B2=84=EA=B7=B8=EB=A5=BC=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db/migration/{V11_create_alert.sql => V11__create_alert.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/resources/db/migration/{V11_create_alert.sql => V11__create_alert.sql} (100%) diff --git a/src/main/resources/db/migration/V11_create_alert.sql b/src/main/resources/db/migration/V11__create_alert.sql similarity index 100% rename from src/main/resources/db/migration/V11_create_alert.sql rename to src/main/resources/db/migration/V11__create_alert.sql From 199d25f10393ea0fa0852dc18c6324a25eaeed7c Mon Sep 17 00:00:00 2001 From: devxb Date: Fri, 9 Feb 2024 16:10:13 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EC=9C=A0=EC=A0=80=EA=B0=80=20?= =?UTF-8?q?=ED=95=9C=EB=8B=AC=EB=8F=99=EC=95=88=20=EB=B0=9B=EC=9D=80=20?= =?UTF-8?q?=EC=95=8C=EB=9E=8C=EC=9D=84=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EA=B0=9C=EB=B0=9C?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alert/controller/AlertController.java | 17 ++++++++++--- .../alert/domain/AlertRepository.java | 8 ++++++ .../teumteum/alert/domain/AlertService.java | 17 +++++++++++++ .../alert/domain/response/AlertsResponse.java | 25 +++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/teumteum/alert/domain/response/AlertsResponse.java diff --git a/src/main/java/net/teumteum/alert/controller/AlertController.java b/src/main/java/net/teumteum/alert/controller/AlertController.java index 9c543dc..47e5892 100644 --- a/src/main/java/net/teumteum/alert/controller/AlertController.java +++ b/src/main/java/net/teumteum/alert/controller/AlertController.java @@ -3,13 +3,16 @@ import io.sentry.Sentry; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import net.teumteum.alert.domain.AlertService; import net.teumteum.alert.domain.UserAlertService; import net.teumteum.alert.domain.request.RegisterAlertRequest; import net.teumteum.alert.domain.request.UpdateAlertTokenRequest; +import net.teumteum.alert.domain.response.AlertsResponse; import net.teumteum.core.error.ErrorResponse; import net.teumteum.core.security.service.SecurityService; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -20,21 +23,29 @@ @RequiredArgsConstructor public class AlertController { - private final UserAlertService alertService; + private final AlertService alertService; + private final UserAlertService userAlertService; private final SecurityService securityService; @PostMapping("/alerts") @ResponseStatus(HttpStatus.OK) public void registerAlert(@Valid @RequestBody RegisterAlertRequest registerAlertRequest) { var loginUserId = securityService.getCurrentUserId(); - alertService.registerAlert(loginUserId, registerAlertRequest); + userAlertService.registerAlert(loginUserId, registerAlertRequest); } @PatchMapping("/alerts") @ResponseStatus(HttpStatus.OK) public void updateAlert(@Valid @RequestBody UpdateAlertTokenRequest updateAlertTokenRequest) { var loginUserId = securityService.getCurrentUserId(); - alertService.updateAlertToken(loginUserId, updateAlertTokenRequest); + userAlertService.updateAlertToken(loginUserId, updateAlertTokenRequest); + } + + @GetMapping("/alerts") + @ResponseStatus(HttpStatus.OK) + public AlertsResponse getAlerts() { + var loginUserId = securityService.getCurrentUserId(); + return alertService.findAllByUserId(loginUserId); } @ExceptionHandler(IllegalArgumentException.class) diff --git a/src/main/java/net/teumteum/alert/domain/AlertRepository.java b/src/main/java/net/teumteum/alert/domain/AlertRepository.java index 6d999a9..e793cdf 100644 --- a/src/main/java/net/teumteum/alert/domain/AlertRepository.java +++ b/src/main/java/net/teumteum/alert/domain/AlertRepository.java @@ -1,7 +1,15 @@ package net.teumteum.alert.domain; +import java.time.Instant; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface AlertRepository extends JpaRepository { + List findAllByUserId(Long userId); + + @Query("select a from alert as a where a.createdAt <= :createdAt") + List findAll(@Param("createdAt") Instant createdAt); } diff --git a/src/main/java/net/teumteum/alert/domain/AlertService.java b/src/main/java/net/teumteum/alert/domain/AlertService.java index 55d7f97..b65f878 100644 --- a/src/main/java/net/teumteum/alert/domain/AlertService.java +++ b/src/main/java/net/teumteum/alert/domain/AlertService.java @@ -1,6 +1,10 @@ package net.teumteum.alert.domain; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import lombok.RequiredArgsConstructor; +import net.teumteum.alert.domain.response.AlertsResponse; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -9,10 +13,23 @@ @Transactional(readOnly = true) public class AlertService { + private static final String EVERY_12AM = "0 0 0 * * *"; + private final AlertRepository alertRepository; + @Transactional public Alert save(Alert alert) { return alertRepository.save(alert); } + public AlertsResponse findAllByUserId(Long userId) { + return AlertsResponse.of(alertRepository.findAllByUserId(userId)); + } + + @Transactional + @Scheduled(cron = EVERY_12AM) + public void deleteOneMonthBeforeAlert() { + var deleteTargets = alertRepository.findAll(Instant.now().minus(1, ChronoUnit.MONTHS)); + alertRepository.deleteAllInBatch(deleteTargets); + } } diff --git a/src/main/java/net/teumteum/alert/domain/response/AlertsResponse.java b/src/main/java/net/teumteum/alert/domain/response/AlertsResponse.java new file mode 100644 index 0000000..c799ec5 --- /dev/null +++ b/src/main/java/net/teumteum/alert/domain/response/AlertsResponse.java @@ -0,0 +1,25 @@ +package net.teumteum.alert.domain.response; + +import java.util.List; +import net.teumteum.alert.domain.Alert; + +public record AlertsResponse( + List alerts +) { + + public static AlertsResponse of(List alerts) { + return new AlertsResponse( + alerts.stream() + .map(alert -> new AlertResponse(alert.getTitle(), alert.getBody(), alert.getType().name())) + .toList() + ); + } + + public record AlertResponse( + String title, + String body, + String type + ) { + + } +} From 9fdf2cdb4005714537efd74b090c44ad6705f934 Mon Sep 17 00:00:00 2001 From: devxb Date: Sat, 10 Feb 2024 13:26:14 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20AlertRepository=EC=9D=98=20fina?= =?UTF-8?q?lAll=20=EB=A9=94=EC=86=8C=EB=93=9C=EB=A5=BC=20findAllByCreatedA?= =?UTF-8?q?t=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/teumteum/alert/domain/AlertRepository.java | 2 +- src/main/java/net/teumteum/alert/domain/AlertService.java | 2 +- .../java/net/teumteum/meeting/domain/MeetingRepositoryTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/teumteum/alert/domain/AlertRepository.java b/src/main/java/net/teumteum/alert/domain/AlertRepository.java index e793cdf..7c873b6 100644 --- a/src/main/java/net/teumteum/alert/domain/AlertRepository.java +++ b/src/main/java/net/teumteum/alert/domain/AlertRepository.java @@ -11,5 +11,5 @@ public interface AlertRepository extends JpaRepository { List findAllByUserId(Long userId); @Query("select a from alert as a where a.createdAt <= :createdAt") - List findAll(@Param("createdAt") Instant createdAt); + List findAllByCreatedAt(@Param("createdAt") Instant createdAt); } diff --git a/src/main/java/net/teumteum/alert/domain/AlertService.java b/src/main/java/net/teumteum/alert/domain/AlertService.java index b65f878..042524f 100644 --- a/src/main/java/net/teumteum/alert/domain/AlertService.java +++ b/src/main/java/net/teumteum/alert/domain/AlertService.java @@ -29,7 +29,7 @@ public AlertsResponse findAllByUserId(Long userId) { @Transactional @Scheduled(cron = EVERY_12AM) public void deleteOneMonthBeforeAlert() { - var deleteTargets = alertRepository.findAll(Instant.now().minus(1, ChronoUnit.MONTHS)); + var deleteTargets = alertRepository.findAllByCreatedAt(Instant.now().minus(1, ChronoUnit.MONTHS)); alertRepository.deleteAllInBatch(deleteTargets); } } diff --git a/src/test/java/net/teumteum/meeting/domain/MeetingRepositoryTest.java b/src/test/java/net/teumteum/meeting/domain/MeetingRepositoryTest.java index 18d3041..29f2f52 100644 --- a/src/test/java/net/teumteum/meeting/domain/MeetingRepositoryTest.java +++ b/src/test/java/net/teumteum/meeting/domain/MeetingRepositoryTest.java @@ -97,7 +97,7 @@ void Delete_success_if_exists_meeting_input() { } @Nested - @DisplayName("JPA Specification을 이용한 findAll 메소드 중") + @DisplayName("JPA Specification을 이용한 findAllByCreatedAt 메소드 중") class FindAllWithSpecificationAndPageNation_method { @Test