-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from 9uttery/feat/notification-#132
[Feature] 토큰 등록, 알림 조회 API 구현
- Loading branch information
Showing
17 changed files
with
313 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/guttery/madii/domain/notification/application/dto/NotificationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.guttery.madii.domain.notification.application.dto; | ||
|
||
public record NotificationData( | ||
String title, | ||
String contents | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/guttery/madii/domain/notification/application/dto/NotificationInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.guttery.madii.domain.notification.application.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
@Schema(description = "알림 정보") | ||
public record NotificationInfo( | ||
@Schema(description = "알림 이름") | ||
String title, | ||
@Schema(description = "알림 내용") | ||
String contents, | ||
@Schema(description = "알림 생성 날짜") | ||
String createdAt | ||
) { | ||
} |
12 changes: 12 additions & 0 deletions
12
.../java/com/guttery/madii/domain/notification/application/dto/NotificationListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.guttery.madii.domain.notification.application.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
import java.util.List; | ||
|
||
@Schema(description = "알림 리스트 응답") | ||
public record NotificationListResponse( | ||
@Schema(description = "알림 리스트") | ||
List<NotificationInfo> notificationInfos | ||
) { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/guttery/madii/domain/notification/application/dto/SaveTokenRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.guttery.madii.domain.notification.application.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
@Schema(description = "토큰 저장 요청") | ||
public record SaveTokenRequest( | ||
@NotBlank | ||
@Schema(description = "토큰", example = "fcm_token_value") | ||
String token | ||
) { | ||
} |
19 changes: 19 additions & 0 deletions
19
...ain/java/com/guttery/madii/domain/notification/application/mapper/NotificationMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.guttery.madii.domain.notification.application.mapper; | ||
|
||
import com.guttery.madii.domain.notification.application.dto.NotificationInfo; | ||
import com.guttery.madii.domain.notification.domain.model.Notification; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class NotificationMapper { | ||
public static NotificationInfo toNotificationInfo(Notification notification) { | ||
return new NotificationInfo( | ||
notification.getTitle(), | ||
notification.getContents(), | ||
notification.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) | ||
); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...n/java/com/guttery/madii/domain/notification/application/service/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.guttery.madii.domain.notification.application.service; | ||
|
||
import com.guttery.madii.domain.notification.application.dto.NotificationInfo; | ||
import com.guttery.madii.domain.notification.application.dto.NotificationListResponse; | ||
import com.guttery.madii.domain.notification.application.mapper.NotificationMapper; | ||
import com.guttery.madii.domain.notification.domain.repository.NotificationRepository; | ||
import com.guttery.madii.domain.user.application.service.UserServiceHelper; | ||
import com.guttery.madii.domain.user.domain.model.User; | ||
import com.guttery.madii.domain.user.domain.model.UserPrincipal; | ||
import com.guttery.madii.domain.user.domain.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Service | ||
public class NotificationService { | ||
private static final int DATE_CRITERIA_OFFSET = 30; | ||
|
||
private final NotificationRepository notificationRepository; | ||
private final UserRepository userRepository; | ||
|
||
public NotificationListResponse getNotificationList(final UserPrincipal userPrincipal) { | ||
final User foundUser = UserServiceHelper.findExistingUser(userRepository, userPrincipal); | ||
final LocalDateTime dateCriteria = LocalDateTime.now().toLocalDate().atStartOfDay().minusDays(DATE_CRITERIA_OFFSET); | ||
final List<NotificationInfo> notificationInfos = notificationRepository.findAllByUserAndCreatedAtIsAfter(foundUser, dateCriteria) | ||
.stream() | ||
.map(NotificationMapper::toNotificationInfo) | ||
.toList(); | ||
|
||
return new NotificationListResponse(notificationInfos); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...main/java/com/guttery/madii/domain/notification/application/service/UserTokenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.guttery.madii.domain.notification.application.service; | ||
|
||
import com.guttery.madii.common.exception.CustomException; | ||
import com.guttery.madii.common.exception.ErrorDetails; | ||
import com.guttery.madii.domain.notification.application.dto.SaveTokenRequest; | ||
import com.guttery.madii.domain.notification.domain.model.UserTokens; | ||
import com.guttery.madii.domain.notification.domain.repository.UserTokensRepository; | ||
import com.guttery.madii.domain.user.application.service.UserServiceHelper; | ||
import com.guttery.madii.domain.user.domain.model.UserPrincipal; | ||
import com.guttery.madii.domain.user.domain.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Service | ||
public class UserTokenService { | ||
private final UserTokensRepository userTokensRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public void saveUserToken(final SaveTokenRequest saveTokenRequest, final UserPrincipal userPrincipal) { | ||
UserServiceHelper.findExistingUser(userRepository, userPrincipal); | ||
|
||
final UserTokens foundUserTokens = userTokensRepository.findById(userPrincipal.id().toString()) | ||
.orElseGet(() -> UserTokens.createEmpty(userPrincipal.id().toString())); | ||
|
||
foundUserTokens.addToken(saveTokenRequest.token()); | ||
userTokensRepository.save(foundUserTokens); | ||
} | ||
|
||
@Transactional | ||
public void deleteUserToken(final String token, final UserPrincipal userPrincipal) { | ||
UserServiceHelper.findExistingUser(userRepository, userPrincipal); | ||
final UserTokens foundUserTokens = userTokensRepository.findById(userPrincipal.id().toString()) | ||
.orElseThrow(() -> CustomException.of(ErrorDetails.USER_TOKEN_INFORMATION_NOT_EXISTS)); | ||
|
||
foundUserTokens.removeToken(token); | ||
userTokensRepository.save(foundUserTokens); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/guttery/madii/domain/notification/domain/model/Notification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.guttery.madii.domain.notification.domain.model; | ||
|
||
import com.guttery.madii.common.domain.model.BaseTimeEntity; | ||
import com.guttery.madii.domain.user.domain.model.User; | ||
import jakarta.persistence.Access; | ||
import jakarta.persistence.AccessType; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
@Table(name = "t_notification") | ||
@Access(AccessType.FIELD) | ||
public class Notification extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long notificationId; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
private User user; | ||
private String title; | ||
private String contents; | ||
private Boolean isChecked; | ||
|
||
private Notification(User user, String title, String contents, Boolean isChecked) { | ||
this.user = user; | ||
this.title = title; | ||
this.contents = contents; | ||
this.isChecked = isChecked; | ||
} | ||
|
||
public static Notification create(User user, String title, String contents) { | ||
return new Notification(user, title, contents, false); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/guttery/madii/domain/notification/domain/model/UserTokens.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.guttery.madii.domain.notification.domain.model; | ||
|
||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.bson.codecs.pojo.annotations.BsonProperty; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Document(collection = "usertokens") | ||
public class UserTokens { | ||
@Id | ||
@BsonProperty("_id") | ||
private String _id; | ||
private List<String> tokens; | ||
|
||
public static UserTokens createEmpty(String userId) { | ||
return new UserTokens(userId, new ArrayList<>()); | ||
} | ||
|
||
public void addToken(String token) { | ||
tokens.add(token); | ||
} | ||
|
||
public void removeToken(String token) { | ||
tokens.remove(token); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../java/com/guttery/madii/domain/notification/domain/repository/NotificationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.guttery.madii.domain.notification.domain.repository; | ||
|
||
import com.guttery.madii.domain.notification.domain.model.Notification; | ||
import com.guttery.madii.domain.user.domain.model.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public interface NotificationRepository extends JpaRepository<Notification, Long> { | ||
List<Notification> findAllByUserAndCreatedAtIsAfter(User user, LocalDateTime createdAt); | ||
} |
8 changes: 8 additions & 0 deletions
8
...in/java/com/guttery/madii/domain/notification/domain/repository/UserTokensRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.guttery.madii.domain.notification.domain.repository; | ||
|
||
import com.guttery.madii.domain.notification.domain.model.UserTokens; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
public interface UserTokensRepository extends MongoRepository<UserTokens, String> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/guttery/madii/domain/notification/presentation/NotificationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.guttery.madii.domain.notification.presentation; | ||
|
||
import com.guttery.madii.domain.notification.application.dto.NotificationListResponse; | ||
import com.guttery.madii.domain.notification.application.dto.SaveTokenRequest; | ||
import com.guttery.madii.domain.notification.application.service.NotificationService; | ||
import com.guttery.madii.domain.notification.application.service.UserTokenService; | ||
import com.guttery.madii.domain.user.domain.model.UserPrincipal; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/notification") | ||
@Validated | ||
@Tag(name = "Notification", description = "Notification 관련 API입니다.") | ||
public class NotificationController { | ||
private final UserTokenService userTokenService; | ||
private final NotificationService notificationService; | ||
|
||
@PostMapping("/token") | ||
public void saveUserToken( | ||
@RequestBody @Valid SaveTokenRequest request, | ||
@NotNull @AuthenticationPrincipal UserPrincipal userPrincipal | ||
) { | ||
userTokenService.saveUserToken(request, userPrincipal); | ||
} | ||
|
||
@GetMapping | ||
public NotificationListResponse getNotificationList( | ||
@NotNull @AuthenticationPrincipal UserPrincipal userPrincipal | ||
) { | ||
return notificationService.getNotificationList(userPrincipal); | ||
} | ||
} |
Oops, something went wrong.