Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading