From a48fdb928f150396cf985030b617946b4f69c3aa Mon Sep 17 00:00:00 2001 From: pureumq Date: Wed, 19 Feb 2025 12:26:33 +0900 Subject: [PATCH 1/2] feat --- .../prdoit/controller/CommentController.java | 51 ++++++++ .../prdoit/dto/comment/CommentPatchDto.java | 17 +++ .../dto/comment/CommentReplyPatchDto.java | 17 +++ .../dto/comment/CommentReplyRequestDto.java | 18 +++ .../dto/comment/CommentReplyResponseDto.java | 18 +++ .../prdoit/dto/comment/CommentRequestDto.java | 18 +++ .../dto/comment/CommentResponseDto.java | 20 +++ .../service/comment/CommentService.java | 18 +++ .../service/comment/CommentServiceImpl.java | 120 ++++++++++++++++++ 9 files changed, 297 insertions(+) create mode 100644 src/main/java/com/example/prdoit/controller/CommentController.java create mode 100644 src/main/java/com/example/prdoit/dto/comment/CommentPatchDto.java create mode 100644 src/main/java/com/example/prdoit/dto/comment/CommentReplyPatchDto.java create mode 100644 src/main/java/com/example/prdoit/dto/comment/CommentReplyRequestDto.java create mode 100644 src/main/java/com/example/prdoit/dto/comment/CommentReplyResponseDto.java create mode 100644 src/main/java/com/example/prdoit/dto/comment/CommentRequestDto.java create mode 100644 src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java create mode 100644 src/main/java/com/example/prdoit/service/comment/CommentService.java create mode 100644 src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java diff --git a/src/main/java/com/example/prdoit/controller/CommentController.java b/src/main/java/com/example/prdoit/controller/CommentController.java new file mode 100644 index 0000000..89bed6d --- /dev/null +++ b/src/main/java/com/example/prdoit/controller/CommentController.java @@ -0,0 +1,51 @@ +package com.example.prdoit.controller; + +import com.example.prdoit.dto.comment.*; +import com.example.prdoit.service.comment.CommentService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/comments") +public class CommentController { + + private final CommentService commentService; + + // 1. 댓글 조회 + @GetMapping("/{contentId}") + public ResponseEntity> getComments(@PathVariable String contentId) { + return ResponseEntity.ok(commentService.getCommentList(contentId)); + } + + // 2. 댓글 작성 + @PostMapping + public ResponseEntity createComment(@RequestBody CommentRequestDto commentRequestDto) { + commentService.postComment(commentRequestDto); + return ResponseEntity.ok("댓글이 성공적으로 작성되었습니다!"); + } + + // 3. 대댓글 작성 + @PostMapping("/reply") + public ResponseEntity createReply(@RequestBody CommentReplyRequestDto commentReplyRequestDto) { + commentService.postCommentReply(commentReplyRequestDto); + return ResponseEntity.ok("대댓글이 성공적으로 작성되었습니다!"); + } + + // 4. 댓글 수정 + @PatchMapping + public ResponseEntity updateComment(@RequestBody CommentPatchDto commentPatchDto) { + commentService.patchComment(commentPatchDto); + return ResponseEntity.ok("댓글이 성공적으로 수정되었습니다!"); + } + + // 5.대댓글 수정 + @PatchMapping("/reply") + public ResponseEntity updateReply(@RequestBody CommentReplyPatchDto commentReplyPatchDto) { + commentService.patchCommentReply(commentReplyPatchDto); + return ResponseEntity.ok("대댓글이 성공적으로 수정되었습니다!"); + } +} diff --git a/src/main/java/com/example/prdoit/dto/comment/CommentPatchDto.java b/src/main/java/com/example/prdoit/dto/comment/CommentPatchDto.java new file mode 100644 index 0000000..dabb6e7 --- /dev/null +++ b/src/main/java/com/example/prdoit/dto/comment/CommentPatchDto.java @@ -0,0 +1,17 @@ +package com.example.prdoit.dto.comment; + +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class CommentPatchDto { + + private int commentId; // 수정할 댓글 ID + private String commentContent; // 수정된 댓글 내용 + private LocalDateTime commentDate; // 수정된 날짜 +} diff --git a/src/main/java/com/example/prdoit/dto/comment/CommentReplyPatchDto.java b/src/main/java/com/example/prdoit/dto/comment/CommentReplyPatchDto.java new file mode 100644 index 0000000..339a517 --- /dev/null +++ b/src/main/java/com/example/prdoit/dto/comment/CommentReplyPatchDto.java @@ -0,0 +1,17 @@ +package com.example.prdoit.dto.comment; + +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class CommentReplyPatchDto { + + private int commentReplyId; // 수정할 대댓글 ID + private String commentReplyContent; // 수정된 대댓글 내용 + private LocalDateTime commentReplyDate; // 수정된 날짜 +} diff --git a/src/main/java/com/example/prdoit/dto/comment/CommentReplyRequestDto.java b/src/main/java/com/example/prdoit/dto/comment/CommentReplyRequestDto.java new file mode 100644 index 0000000..53e4532 --- /dev/null +++ b/src/main/java/com/example/prdoit/dto/comment/CommentReplyRequestDto.java @@ -0,0 +1,18 @@ +package com.example.prdoit.dto.comment; + +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class CommentReplyRequestDto { + + private int commentId; // 대댓글이 달릴 원본 댓글 ID + private String commentReplyContent; // 대댓글 내용 + private String commentReplyNickname; // 대댓글 작성자 닉네임 + private LocalDateTime commentReplyDate; // 대댓글 작성 날짜 +} diff --git a/src/main/java/com/example/prdoit/dto/comment/CommentReplyResponseDto.java b/src/main/java/com/example/prdoit/dto/comment/CommentReplyResponseDto.java new file mode 100644 index 0000000..ff2649d --- /dev/null +++ b/src/main/java/com/example/prdoit/dto/comment/CommentReplyResponseDto.java @@ -0,0 +1,18 @@ +package com.example.prdoit.dto.comment; + +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class CommentReplyResponseDto { + + private int commentReplyId; // 대댓글 ID + private String commentReplyContent; // 대댓글 내용 + private String commentReplyNickname; // 작성자 닉네임 + private LocalDateTime commentReplyDate; // 대댓글 작성 날짜 +} diff --git a/src/main/java/com/example/prdoit/dto/comment/CommentRequestDto.java b/src/main/java/com/example/prdoit/dto/comment/CommentRequestDto.java new file mode 100644 index 0000000..f74a319 --- /dev/null +++ b/src/main/java/com/example/prdoit/dto/comment/CommentRequestDto.java @@ -0,0 +1,18 @@ +package com.example.prdoit.dto.comment; + +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class CommentRequestDto { + + private String contentId; // 댓글 달릴 게시글 ID + private String commentContent; // 댓글 내용 + private String commentNickname; // 댓글 작성자 닉네임 + private LocalDateTime commentDate; // 댓글 작성 날짜 +} diff --git a/src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java b/src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java new file mode 100644 index 0000000..8068c06 --- /dev/null +++ b/src/main/java/com/example/prdoit/dto/comment/CommentResponseDto.java @@ -0,0 +1,20 @@ +package com.example.prdoit.dto.comment; + +import lombok.*; + +import java.time.LocalDateTime; +import java.util.List; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class CommentResponseDto { + + private int commentId; // 댓글 ID + private String commentContent; // 댓글 내용 + private String commentNickname; // 작성자 닉네임 + private LocalDateTime commentDate; // 댓글 작성 날짜 + private List commentReplyResponseDtoList; // 대댓글 리스트 +} diff --git a/src/main/java/com/example/prdoit/service/comment/CommentService.java b/src/main/java/com/example/prdoit/service/comment/CommentService.java new file mode 100644 index 0000000..ab6a0ef --- /dev/null +++ b/src/main/java/com/example/prdoit/service/comment/CommentService.java @@ -0,0 +1,18 @@ +package com.example.prdoit.service.comment; + +import com.example.prdoit.dto.comment.*; + +import java.util.List; + +public interface CommentService { + + List getCommentList(String contentId); + + void postComment(CommentRequestDto commentRequestDto); + + void postCommentReply(CommentReplyRequestDto commentReplyRequestDto); + + void patchComment(CommentPatchDto commentPatchDto); + + void patchCommentReply(CommentReplyPatchDto commentReplyPatchDto); +} diff --git a/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java b/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java new file mode 100644 index 0000000..5b20c61 --- /dev/null +++ b/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java @@ -0,0 +1,120 @@ +package com.example.prdoit.service.comment; + +import com.example.prdoit.dto.comment.*; +import com.example.prdoit.exception.CustomException; +import com.example.prdoit.model.CommentCommentTable; +import com.example.prdoit.model.CommentTable; +import com.example.prdoit.model.ContentTable; +import com.example.prdoit.repository.CommentCommentTableRepository; +import com.example.prdoit.repository.CommentTableRepository; +import com.example.prdoit.repository.ContentTableRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@Slf4j +@RequiredArgsConstructor +@Transactional +public class CommentServiceImpl implements CommentService { + + private final CommentTableRepository commentTableRepository; + private final CommentCommentTableRepository commentCommentTableRepository; + private final ContentTableRepository contentTableRepository; + + // 1. 댓글 목록 조회 (GET) + @Override + public List getCommentList(String contentId) { + log.info("[getCommentList] 댓글 목록 조회 시작 - Content ID: {}", contentId); + + ContentTable contentTable = contentTableRepository.findById(contentId) + .orElseThrow(() -> new CustomException("해당 게시글이 없습니다. ID = " + contentId)); + + return contentTable.getCommentTable().stream() + .map(comment -> CommentResponseDto.builder() + .commentId(comment.getCommentId()) + .commentContent(comment.getCommentContent()) + .commentNickname(comment.getCommentNickname()) + .commentDate(comment.getCommentDate()) + .commentReplyResponseDtoList(comment.getCommentCommentTable().stream() + .map(reply -> CommentReplyResponseDto.builder() + .commentReplyId(reply.getCommentCommentId()) + .commentReplyContent(reply.getCommentCommentContent()) + .commentReplyNickname(reply.getUserNickname()) + .commentReplyDate(reply.getCommentCommentDate()) + .build()) + .collect(Collectors.toList())) + .build()) + .collect(Collectors.toList()); + } + + // 2. 댓글 작성 (POST) + @Override + public void postComment(CommentRequestDto commentRequestDto) { + log.info("[postComment] 댓글 등록 시작 - Content ID: {}", commentRequestDto.getContentId()); + + ContentTable contentTable = contentTableRepository.findById(commentRequestDto.getContentId()) + .orElseThrow(() -> new CustomException("해당 게시글이 없습니다.")); + + commentTableRepository.save(CommentTable.builder() + .commentContent(commentRequestDto.getCommentContent()) + .commentNickname(commentRequestDto.getCommentNickname()) + .commentDate(commentRequestDto.getCommentDate()) + .contentId(contentTable) + .build()); + + log.info("[postComment] 댓글 등록을 완료했습니다."); + } + + // 3. 대댓글 작성 (POST) + @Override + public void postCommentReply(CommentReplyRequestDto commentReplyRequestDto) { + log.info("[postCommentReply] 대댓글 등록 시작"); + + CommentTable parentComment = commentTableRepository.findById(commentReplyRequestDto.getCommentId()) + .orElseThrow(() -> new CustomException("해당 댓글이 없습니다.")); + + commentCommentTableRepository.save(CommentCommentTable.builder() + .commentCommentContent(commentReplyRequestDto.getCommentReplyContent()) + .userNickname(commentReplyRequestDto.getCommentReplyNickname()) + .commentCommentDate(commentReplyRequestDto.getCommentReplyDate()) + .commentId(parentComment) + .build()); + + log.info("[postCommentReply] 대댓글 등록을 완료했습니다."); + } + + // 4. 댓글 수정 (PATCH) + @Override + public void patchComment(CommentPatchDto commentPatchDto) { + log.info("[patchComment] 댓글 수정 시작 - Comment ID: {}", commentPatchDto.getCommentId()); + + CommentTable comment = commentTableRepository.findById(commentPatchDto.getCommentId()) + .orElseThrow(() -> new CustomException("해당 댓글이 없습니다.")); + + comment.setCommentContent(commentPatchDto.getCommentContent()); + comment.setCommentDate(commentPatchDto.getCommentDate()); + commentTableRepository.save(comment); + + log.info("[patchComment] 댓글 수정이 완료되었습니다."); + } + + // 5. 대댓글 수정 (PATCH) + @Override + public void patchCommentReply(CommentReplyPatchDto commentReplyPatchDto) { + log.info("[patchCommentReply] 대댓글 수정 시작 - CommentReply ID: {}", commentReplyPatchDto.getCommentReplyId()); + + CommentCommentTable reply = commentCommentTableRepository.findById(commentReplyPatchDto.getCommentReplyId()) + .orElseThrow(() -> new CustomException("해당 대댓글이 없습니다.")); + + reply.setCommentCommentContent(commentReplyPatchDto.getCommentReplyContent()); + reply.setCommentCommentDate(commentReplyPatchDto.getCommentReplyDate()); + commentCommentTableRepository.save(reply); + + log.info("[patchCommentReply] 대댓글 수정이 완료되었습니다."); + } +} From 15e5f0a0836cb7dc4c5151545d52c6145a06dab5 Mon Sep 17 00:00:00 2001 From: Taehun88 Date: Wed, 19 Feb 2025 15:21:40 +0900 Subject: [PATCH 2/2] feat:notification --- .../prdoit/controller/ProjectController.java | 15 ------- .../prdoit/controller/UserController.java | 14 ++++++ .../notification/NotificationResponseDto.java | 16 +++++++ .../com/example/prdoit/model/IdTable.java | 8 ++-- ...{ReadTable.java => NotificationTable.java} | 15 ++++--- .../NotificationTableRepository.java | 9 ++++ .../service/comment/CommentServiceImpl.java | 21 +++++++-- .../notification/NotificationService.java | 9 ++++ .../notification/NotificationServiceImpl.java | 44 +++++++++++++++++++ 9 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/example/prdoit/dto/notification/NotificationResponseDto.java rename src/main/java/com/example/prdoit/model/{ReadTable.java => NotificationTable.java} (59%) create mode 100644 src/main/java/com/example/prdoit/repository/NotificationTableRepository.java create mode 100644 src/main/java/com/example/prdoit/service/notification/NotificationService.java create mode 100644 src/main/java/com/example/prdoit/service/notification/NotificationServiceImpl.java diff --git a/src/main/java/com/example/prdoit/controller/ProjectController.java b/src/main/java/com/example/prdoit/controller/ProjectController.java index 12b343f..d244ec5 100644 --- a/src/main/java/com/example/prdoit/controller/ProjectController.java +++ b/src/main/java/com/example/prdoit/controller/ProjectController.java @@ -134,19 +134,4 @@ public ResponseEntity deleteProject(@PathVariable String projectId) { return ResponseEntity.badRequest().body("프로젝트 삭제에 실패했습니다."); } } - -/* - @PutMapping("/product") - public ResponseEntity putBacklog(@RequestBody BacklogPutDto backlogPutDto) { - log.info("[putBacklog] 백로그 이동 요청"); - try{ - projectService.putBacklog(backlogPutDto); - return ResponseEntity.ok("백로그 이동에 성공했습니다."); - } catch (RuntimeException e){ - log.error("[putBacklog] {}", e.getMessage()); - return ResponseEntity.badRequest().body("백로그 이동에 실패했습니다."); - } - } - - */ } diff --git a/src/main/java/com/example/prdoit/controller/UserController.java b/src/main/java/com/example/prdoit/controller/UserController.java index 6f56d19..ecbf8f2 100644 --- a/src/main/java/com/example/prdoit/controller/UserController.java +++ b/src/main/java/com/example/prdoit/controller/UserController.java @@ -4,6 +4,7 @@ import com.example.prdoit.dto.user.LoginDto; import com.example.prdoit.dto.user.UserDto; import com.example.prdoit.exception.CustomException; +import com.example.prdoit.service.notification.NotificationService; import com.example.prdoit.service.user.UserService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -20,6 +21,7 @@ public class UserController { private final UserService userService; + private final NotificationService notificationService; @PostMapping("/signup") public ResponseEntity signUp(@RequestBody UserDto userDto) { @@ -141,4 +143,16 @@ public ResponseEntity checkPassword(@RequestBody LoginDto loginDto) { } } + @GetMapping("/notification/{userId}") + public ResponseEntity getNotificationList(@PathVariable String userId) { + log.info("[getNotificationList] 알림 목록 조회 시작 - User ID: {}", userId); + + try { + return ResponseEntity.ok(notificationService.getNotificationList(userId)); + } catch (CustomException e) { + log.error("[getNotificationList] 알림 목록 조회 실패 - 이유: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } + } + } diff --git a/src/main/java/com/example/prdoit/dto/notification/NotificationResponseDto.java b/src/main/java/com/example/prdoit/dto/notification/NotificationResponseDto.java new file mode 100644 index 0000000..b41dbd9 --- /dev/null +++ b/src/main/java/com/example/prdoit/dto/notification/NotificationResponseDto.java @@ -0,0 +1,16 @@ +package com.example.prdoit.dto.notification; + +import lombok.*; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class NotificationResponseDto { + private int notificationId; + private String notificationContent; + private String contentId; + private String userId; + private int isRead; +} diff --git a/src/main/java/com/example/prdoit/model/IdTable.java b/src/main/java/com/example/prdoit/model/IdTable.java index eaec3ef..e23c634 100644 --- a/src/main/java/com/example/prdoit/model/IdTable.java +++ b/src/main/java/com/example/prdoit/model/IdTable.java @@ -35,10 +35,6 @@ public class IdTable { @JsonIgnore private List surveyTable; - @OneToMany(mappedBy = "id", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.EAGER) - @JsonIgnore - private List readTable; - @OneToMany(mappedBy = "id", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.EAGER) @JsonIgnore private List communityTable; @@ -47,4 +43,8 @@ public class IdTable { @JsonIgnore private List contentTable; + @OneToMany(mappedBy = "userId", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.EAGER) + @JsonIgnore + private List notificationTable; + } diff --git a/src/main/java/com/example/prdoit/model/ReadTable.java b/src/main/java/com/example/prdoit/model/NotificationTable.java similarity index 59% rename from src/main/java/com/example/prdoit/model/ReadTable.java rename to src/main/java/com/example/prdoit/model/NotificationTable.java index a979779..eeeb6db 100644 --- a/src/main/java/com/example/prdoit/model/ReadTable.java +++ b/src/main/java/com/example/prdoit/model/NotificationTable.java @@ -11,15 +11,20 @@ @AllArgsConstructor @NoArgsConstructor @Builder -public class ReadTable { +public class NotificationTable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int readId; + private int notificationId; - private int readContentId; + private String notificationContent; + + private String contentId; + + private int isRead; @ManyToOne - @JoinColumn(name = "id") - private IdTable id; + @JoinColumn(name = "userId") + private IdTable userId; + } diff --git a/src/main/java/com/example/prdoit/repository/NotificationTableRepository.java b/src/main/java/com/example/prdoit/repository/NotificationTableRepository.java new file mode 100644 index 0000000..513720e --- /dev/null +++ b/src/main/java/com/example/prdoit/repository/NotificationTableRepository.java @@ -0,0 +1,9 @@ +package com.example.prdoit.repository; + +import com.example.prdoit.model.NotificationTable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface NotificationTableRepository extends JpaRepository { +} diff --git a/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java b/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java index 5b20c61..3f47a88 100644 --- a/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java +++ b/src/main/java/com/example/prdoit/service/comment/CommentServiceImpl.java @@ -5,9 +5,8 @@ import com.example.prdoit.model.CommentCommentTable; import com.example.prdoit.model.CommentTable; import com.example.prdoit.model.ContentTable; -import com.example.prdoit.repository.CommentCommentTableRepository; -import com.example.prdoit.repository.CommentTableRepository; -import com.example.prdoit.repository.ContentTableRepository; +import com.example.prdoit.model.NotificationTable; +import com.example.prdoit.repository.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -25,6 +24,8 @@ public class CommentServiceImpl implements CommentService { private final CommentTableRepository commentTableRepository; private final CommentCommentTableRepository commentCommentTableRepository; private final ContentTableRepository contentTableRepository; + private final NotificationTableRepository notificationTableRepository; + private final IdTableRepository idTableRepository; // 1. 댓글 목록 조회 (GET) @Override @@ -67,6 +68,13 @@ public void postComment(CommentRequestDto commentRequestDto) { .contentId(contentTable) .build()); + notificationTableRepository.save(NotificationTable.builder() + .notificationContent("댓글이 작성되었습니다.") + .contentId(contentTable.getContentId()) + .userId(contentTable.getUserId()) + .isRead(0) + .build()); + log.info("[postComment] 댓글 등록을 완료했습니다."); } @@ -85,6 +93,13 @@ public void postCommentReply(CommentReplyRequestDto commentReplyRequestDto) { .commentId(parentComment) .build()); + notificationTableRepository.save(NotificationTable.builder() + .notificationContent("대댓글이 작성되었습니다.") + .contentId(parentComment.getContentId().getContentId()) + .userId(idTableRepository.findByNickname(parentComment.getCommentNickname())) + .isRead(0) + .build()); + log.info("[postCommentReply] 대댓글 등록을 완료했습니다."); } diff --git a/src/main/java/com/example/prdoit/service/notification/NotificationService.java b/src/main/java/com/example/prdoit/service/notification/NotificationService.java new file mode 100644 index 0000000..bba09fe --- /dev/null +++ b/src/main/java/com/example/prdoit/service/notification/NotificationService.java @@ -0,0 +1,9 @@ +package com.example.prdoit.service.notification; + +import com.example.prdoit.dto.notification.NotificationResponseDto; + +import java.util.List; + +public interface NotificationService { + List getNotificationList(String userId); +} diff --git a/src/main/java/com/example/prdoit/service/notification/NotificationServiceImpl.java b/src/main/java/com/example/prdoit/service/notification/NotificationServiceImpl.java new file mode 100644 index 0000000..5cc9920 --- /dev/null +++ b/src/main/java/com/example/prdoit/service/notification/NotificationServiceImpl.java @@ -0,0 +1,44 @@ +package com.example.prdoit.service.notification; + +import com.example.prdoit.dto.notification.NotificationResponseDto; +import com.example.prdoit.model.IdTable; +import com.example.prdoit.model.NotificationTable; +import com.example.prdoit.repository.IdTableRepository; +import com.example.prdoit.repository.NotificationTableRepository; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@AllArgsConstructor +@Slf4j +public class NotificationServiceImpl implements NotificationService { + + private final IdTableRepository idTableRepository; + private final NotificationTableRepository notificationTableRepository; + + @Override + public List getNotificationList(String userId) { + log.info("[getNotificationList] 알림 목록 조회 시작 - User ID: {}", userId); + + IdTable idTable = idTableRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. ID = " + userId)); + try { + List notificationResponseDtoList = idTable.getNotificationTable().stream().map(notificationTable -> NotificationResponseDto.builder() + .notificationId(notificationTable.getNotificationId()) + .notificationContent(notificationTable.getNotificationContent()) + .contentId(notificationTable.getContentId()) + .userId(notificationTable.getUserId().getId()) + .isRead(notificationTable.getIsRead()) + .build()).toList(); + for(NotificationTable notificationTable : idTable.getNotificationTable()) { + notificationTable.setIsRead(1); + } + notificationTableRepository.saveAll(idTable.getNotificationTable()); + return notificationResponseDtoList; + } catch (Exception e) { + throw new IllegalArgumentException("알림 목록 조회 중 문제가 발생했습니다."); + } + } +}