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

[update] #68 - 알림 관련 API 수정 및 추가 #90

Merged
merged 1 commit into from
Nov 13, 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 @@ -2,25 +2,33 @@

import com.wooribound.domain.notification.dto.NotificationDTO;
import com.wooribound.domain.notification.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "알림 메세지 API", description = "개인회원 서비스 중 알림 메세지 입니다.")
@RestController
@RequiredArgsConstructor
@RequestMapping("/individual/notifications")
public class WbUserNotificationController {

private final NotificationService notificationService;

@Operation(summary = "알림 메세지 전체 목록 조회", description = "알림 메세지 나눔 전체 목록 조회")
@GetMapping
public List<NotificationDTO> getNotifications(@RequestParam String userId) {
return notificationService.getNotifications(userId);
public ResponseEntity getNotifications(Authentication authentication) {
return ResponseEntity.ok().body(notificationService.getNotifications(authentication));
}

@GetMapping("/detail")
public String getNotificationDetail(@RequestParam long notiId) {
return notificationService.getNotificationDetail(notiId);
@Operation(summary = "알림 메세지 읽음 표시 업데이트", description = "알림 메세지 읽음 표시 업데이트")
@PutMapping("/read")
public ResponseEntity readNotification(Authentication authentication, @RequestParam(value = "notiId") Long notiId) {
return ResponseEntity.ok().body(notificationService.readNotification(authentication, notiId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Long createShareKnowhow(String userId, WbUserKnowhowDTO wbUserKnowhowDTO)

Long knowhowId = 1L;

Optional<WbUser> byUserId = wbUserRepository.findByUserId(userId);
Optional<WbUser> byUserId = wbUserRepository.findById(userId);
Optional<Long> maxKnowhowId = knowhowRepository.getMaxKnowhowId();
if (maxKnowhowId.isPresent()) {
knowhowId = maxKnowhowId.get() + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import java.util.List;
import java.util.Optional;

public interface NotificationRepository extends JpaRepository<Notification, Integer> {
public interface NotificationRepository extends JpaRepository<Notification, Long> {
@Query("SELECT n FROM Notification n WHERE n.wbUser.userId = :userId")
List<Notification> findByUserId(@Param("userId") String userId);

Optional<Notification> findByNotiId(long notiId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.wooribound.domain.notification.dto;


import com.wooribound.global.constant.YN;
import lombok.*;

import java.util.Date;

@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Data
public class NotificationDTO {
private int notiId;
private Long notiId;
private String notice;
private YN isConfirmed;
private Date createdAt;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.wooribound.domain.notification.service;

import com.wooribound.domain.notification.dto.NotificationDTO;
import org.springframework.security.core.Authentication;

import java.util.List;

public interface NotificationService {
List<NotificationDTO> getNotifications(String userId);
List<NotificationDTO> getNotifications(Authentication authentication);

String readNotification (Authentication authentication, Long notiId);

String getNotificationDetail(long notiId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,56 @@
import com.wooribound.domain.notification.NotificationRepository;
import com.wooribound.domain.notification.dto.NotificationDTO;
import com.wooribound.global.constant.YN;
import com.wooribound.global.exception.AuthenticationException;
import com.wooribound.global.exception.NotEntityException;
import com.wooribound.global.util.AuthenticateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class NotificationServiceImpl implements NotificationService {

private final AuthenticateUtil authenticateUtil;
private final NotificationRepository notificationRepository;

@Override
public List<NotificationDTO> getNotifications(String userId) {
// userId에 해당하는 모든 알림 조회 후 DTO로 변환
public List<NotificationDTO> getNotifications(Authentication authentication) {
String userId = authenticateUtil.CheckWbUserAuthAndGetUserId(authentication);

return notificationRepository.findByUserId(userId).stream()
.map(notification -> NotificationDTO.builder()
.notiId(Math.toIntExact(notification.getNotiId()))
.notiId(notification.getNotiId())
.notice(notification.getNotice())
.isConfirmed(notification.getIsConfirmed())
.createdAt(notification.getCreatedAt())
.build())
.collect(Collectors.toList());
}

@Override
public String getNotificationDetail(long notiId) {
// 알림 상세 조회 및 is_confirmed를 Y로 변경
Notification notification = notificationRepository.findByNotiId(notiId)
.orElseThrow(() -> new IllegalArgumentException("Notification not found with id: " + notiId));
public String readNotification(Authentication authentication, Long notiId) {
String userId = authenticateUtil.CheckWbUserAuthAndGetUserId(authentication);
Optional<Notification> byIdNotification = notificationRepository.findById(notiId);

if (!byIdNotification.isPresent()) {
throw new NotEntityException();
}

// is_confirmed를 Y로 변경
if (!byIdNotification.get().getWbUser().getUserId().equals(userId)) {
throw new AuthenticationException();
}

Notification notification = byIdNotification.get();
notification.setIsConfirmed(YN.Y);
notificationRepository.save(notification);

return notification.getNotice();
return "Notification updated successfully";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wooribound.global.exception;

public class NotEntityException extends RuntimeException{
public NotEntityException() {super("존재하는 엔티티가 없습니다.");}
}
Loading