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: 알림 관련 API 2개 추가 #189 #191

Merged
merged 1 commit into from
Sep 6, 2022
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
3 changes: 3 additions & 0 deletions src/main/java/com/ceos/bankids/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public enum ErrorCode implements EnumMapperType {
NOTIFICATION_MESSAGE_ERROR("E500-70001"),
NOTIFICATION_SERVICE_ERROR("E500-70002"),
NOTIFICATION_ACCESSCODE_ERROR("E500-70003"),
NOT_EXIST_NOTIFICATION_ERROR("E400-70004"),
ALREADY_READ_NOTIFICATION_ERROR("E400-70005"),
NOT_MATCH_NOTIFICATION_USER_ERROR("E403-70006"),

// Notice
NOT_EXIST_NOTICE_ERROR("E400-90001"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ceos.bankids.domain.FamilyUser;
import com.ceos.bankids.domain.User;
import com.ceos.bankids.dto.AllSendNotificationDTO;
import com.ceos.bankids.dto.NotificationDTO;
import com.ceos.bankids.repository.UserRepository;
import com.ceos.bankids.service.ExpoNotificationServiceImpl;
import com.ceos.bankids.service.NoticeServiceImpl;
Expand All @@ -18,6 +19,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -51,6 +55,29 @@ public CommonResponse<String> allSendNotification(
return CommonResponse.onSuccess("NOTIFICATION SUCCESS");
}

@ApiOperation(value = "유저 알림 리스트 가져오기")
@GetMapping(produces = "application/json; charset=utf-8")
public CommonResponse<List<NotificationDTO>> getNotificationList(
@AuthenticationPrincipal User authUser) {

log.info("api = 유저 알림 리스트 가져오기 user = {}", authUser.getUsername());
List<NotificationDTO> notificationListDTOS = expoNotificationService.readNotificationList(
authUser);
return CommonResponse.onSuccess(notificationListDTOS);
}

@ApiOperation(value = "유저 알림 읽음 확인")
@PatchMapping(value = "/{notificationId}", produces = "application/json; charset=utf-8")
public CommonResponse<NotificationDTO> patchNotification(@AuthenticationPrincipal User authUser,
@PathVariable Long notificationId) {

log.info("api = 유저 알림 읽음 처리 user = {} notification = {}", authUser.getUsername(),
notificationId);
NotificationDTO notificationDTO = expoNotificationService.updateNotification(authUser,
notificationId);
return CommonResponse.onSuccess(notificationDTO);
}

@Async
@ApiOperation(value = "돈길 상태 변경 알림")
public void notification(Challenge challenge, User authUser) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/ceos/bankids/domain/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;

@Getter
@Setter
@Entity
@Table(name = "Notification")
@NoArgsConstructor
@EqualsAndHashCode(of = "id")
@DynamicInsert
public class Notification extends AbstractTimestamp {

@Id
Expand All @@ -33,6 +36,10 @@ public class Notification extends AbstractTimestamp {
@Column
private String message;

@Column(nullable = false)
@ColumnDefault(value = "false")
private Boolean isRead;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
private User user;
Expand All @@ -42,12 +49,13 @@ public Notification(
Long id,
String title,
String message,
Challenge challenge,
Boolean isRead,
User user
) {
this.id = id;
this.title = title;
this.message = message;
this.isRead = isRead;
this.user = user;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/ceos/bankids/dto/NotificationDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.ceos.bankids.dto;

import com.ceos.bankids.domain.Notification;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.sql.Timestamp;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

@ApiModel(value = "알림 리스트로 줄 때 DTO")
@Getter
@ToString
@EqualsAndHashCode
public class NotificationDTO {

@ApiModelProperty(example = "1")
private Long id;

@ApiModelProperty(example = "알림 제목")
private String title;

@ApiModelProperty(example = "알림 내용")
private String message;

@ApiModelProperty(example = "false")
private Boolean isRead;

@ApiModelProperty(example = "2022/07/05 05:05:05")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd hh:mm:ss", timezone = "Asia/Seoul")
private Timestamp createdAt;

public NotificationDTO(Notification notification) {
this.id = notification.getId();
this.title = notification.getTitle();
this.message = notification.getMessage();
this.isRead = notification.getIsRead();
this.createdAt = notification.getCreatedAt();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ceos.bankids.repository;

import com.ceos.bankids.domain.Notification;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;


public interface NotificationRepository extends
JpaRepository<Notification, Long> {

public List<Notification> findAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.ceos.bankids.service;

import com.ceos.bankids.domain.User;
import com.ceos.bankids.dto.NotificationDTO;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public interface ExpoNotificationService {

public List<NotificationDTO> readNotificationList(User user);

public NotificationDTO updateNotification(User user, Long notificationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.ceos.bankids.constant.ErrorCode;
import com.ceos.bankids.domain.Notification;
import com.ceos.bankids.domain.User;
import com.ceos.bankids.dto.NotificationDTO;
import com.ceos.bankids.exception.BadRequestException;
import com.ceos.bankids.exception.ForbiddenException;
import com.ceos.bankids.exception.InternalServerException;
import com.ceos.bankids.repository.NotificationRepository;
import io.github.jav.exposerversdk.ExpoPushMessage;
Expand All @@ -15,13 +17,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.GenericJDBCException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
Expand All @@ -30,6 +34,32 @@ public class ExpoNotificationServiceImpl implements ExpoNotificationService {

private final NotificationRepository notificationRepository;

@Transactional
@Override
public List<NotificationDTO> readNotificationList(User user) {
return notificationRepository.findAllByUserId(user.getId())
.stream().map(NotificationDTO::new)
.collect(
Collectors.toList());
}

@Transactional
@Override
public NotificationDTO updateNotification(User user, Long notificationId) {
Notification notification = notificationRepository.findById(notificationId).orElseThrow(
() -> new BadRequestException(ErrorCode.NOT_EXIST_NOTIFICATION_ERROR.getErrorCode()));
if (notification.getIsRead()) {
throw new BadRequestException(ErrorCode.ALREADY_READ_NOTIFICATION_ERROR.getErrorCode());
}
if (!Objects.equals(notification.getUser().getId(), user.getId())) {
throw new ForbiddenException(
ErrorCode.NOT_MATCH_NOTIFICATION_USER_ERROR.getErrorCode());
}
notification.setIsRead(true);
notificationRepository.save(notification);
return new NotificationDTO(notification);
}

public void sendMessage(User user, String title, String body, Map<String, Object> data) {

String token = user.getExpoToken();
Expand Down