Skip to content

Commit

Permalink
feat: 유저가 한달 동안 받은 알람을 조회하는 기능 개발 (#185)
Browse files Browse the repository at this point in the history
* fix: flyway schema에 _이 하나밖에 없는 버그를 수정한다

* feat: 유저가 한달동안 받은 알람을 조회하는 기능을 개발한다

* refactor: AlertRepository의 finalAll 메소드를 findAllByCreatedAt으로 변경한다
  • Loading branch information
devxb authored Feb 10, 2024
1 parent 0357d19 commit be58b5b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/main/java/net/teumteum/alert/controller/AlertController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/teumteum/alert/domain/AlertRepository.java
Original file line number Diff line number Diff line change
@@ -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<Alert, Long> {

List<Alert> findAllByUserId(Long userId);

@Query("select a from alert as a where a.createdAt <= :createdAt")
List<Alert> findAllByCreatedAt(@Param("createdAt") Instant createdAt);
}
17 changes: 17 additions & 0 deletions src/main/java/net/teumteum/alert/domain/AlertService.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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.findAllByCreatedAt(Instant.now().minus(1, ChronoUnit.MONTHS));
alertRepository.deleteAllInBatch(deleteTargets);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.teumteum.alert.domain.response;

import java.util.List;
import net.teumteum.alert.domain.Alert;

public record AlertsResponse(
List<AlertResponse> alerts
) {

public static AlertsResponse of(List<Alert> 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
) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void Delete_success_if_exists_meeting_input() {
}

@Nested
@DisplayName("JPA Specification을 이용한 findAll 메소드 중")
@DisplayName("JPA Specification을 이용한 findAllByCreatedAt 메소드 중")
class FindAllWithSpecificationAndPageNation_method {

@Test
Expand Down

0 comments on commit be58b5b

Please sign in to comment.