From 1fad33604e0ca2ed812c8ffc7ca149ad17c0310a Mon Sep 17 00:00:00 2001 From: Valerii_Butko Date: Thu, 29 May 2025 16:38:47 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=8D=D0=BD=D0=B4=D0=BF=D0=BE=D0=B8=D0=BD=D1=82=D1=8B?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/AdminCommentController.java | 36 +++++++++++++ .../priv/PrivateCommentController.java | 31 +++++++++++ .../main/service/CommentService.java | 9 ++++ .../main/service/CommentServiceImpl.java | 54 +++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java create mode 100644 main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java create mode 100644 main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java create mode 100644 main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java new file mode 100644 index 0000000..d563f52 --- /dev/null +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java @@ -0,0 +1,36 @@ +package ru.practicum.explorewithme.main.controller.admin; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import ru.practicum.explorewithme.main.dto.CommentDto; +import ru.practicum.explorewithme.main.service.CommentService; + +@RestController +@RequestMapping("/admin/comments") +@RequiredArgsConstructor +@Validated +@Slf4j +public class AdminCommentController { + + private final CommentService commentService; + + @DeleteMapping("/{commentId}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void deleteComment(@PathVariable Long commentId) { + log.info("Admin: Received request to delete comment with Id: {}", commentId); + commentService.deleteCommentByAdmin(commentId); + log.info("Admin: Comment with Id: {} marked as deleted", commentId); + } + + @PatchMapping("/{commentId}/restore") + @ResponseStatus(HttpStatus.OK) + public CommentDto restoreComment(@PathVariable Long commentId) { + log.info("Admin: Received request to restore comment with Id: {}", commentId); + CommentDto restoredComment = commentService.restoreCommentByAdmin(commentId); + log.info("Admin: Comment with Id: {} restored", commentId); + return restoredComment; + } +} \ No newline at end of file diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java new file mode 100644 index 0000000..98fea5c --- /dev/null +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java @@ -0,0 +1,31 @@ +package ru.practicum.explorewithme.main.controller.priv; + +import jakarta.validation.constraints.Positive; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import ru.practicum.explorewithme.main.service.CommentService; +import ru.practicum.explorewithme.main.service.RequestService; + +@RestController +@RequestMapping("/users/{userId}") +@RequiredArgsConstructor +@Validated +@Slf4j +public class PrivateCommentController { + + private final CommentService commentService; + private final RequestService requestService; + + @DeleteMapping("/comments/{commentId}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void deleteComment( + @PathVariable @Positive Long userId, + @PathVariable @Positive Long commentId) { + log.info("User id={}: Received request to delete comment with Id: {}", userId, commentId); + commentService.deleteUserComment(userId, commentId); + log.info("User id={}: Comment with Id: {} marked as deleted", userId, commentId); + } +} \ No newline at end of file diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java new file mode 100644 index 0000000..42c35fb --- /dev/null +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java @@ -0,0 +1,9 @@ +package ru.practicum.explorewithme.main.service; + +import ru.practicum.explorewithme.main.dto.CommentDto; + +public interface CommentService { + void deleteCommentByAdmin(Long commentId); + void deleteUserComment(Long userId, Long commentId); + CommentDto restoreCommentByAdmin(Long commentId); +} \ No newline at end of file diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java new file mode 100644 index 0000000..edd34ee --- /dev/null +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java @@ -0,0 +1,54 @@ +package ru.practicum.explorewithme.main.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.practicum.explorewithme.main.dto.CommentDto; +import ru.practicum.explorewithme.main.error.BusinessRuleViolationException; +import ru.practicum.explorewithme.main.error.EntityNotFoundException; +import ru.practicum.explorewithme.main.mapper.CommentMapper; +import ru.practicum.explorewithme.main.model.Comment; +import ru.practicum.explorewithme.main.repository.CommentRepository; + +@Service +@RequiredArgsConstructor +@Transactional +public class CommentServiceImpl implements CommentService { + + private final CommentRepository commentRepository; + private final CommentMapper commentMapper; + + @Transactional + public void deleteCommentByAdmin(Long commentId) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new EntityNotFoundException("Comment", "Id", commentId)); + if (!comment.isDeleted()) { + comment.setDeleted(true); + commentRepository.save(comment); + } + } + + @Transactional + public void deleteUserComment(Long userId, Long commentId) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new EntityNotFoundException("Comment", "Id", commentId)); + if (!comment.getAuthor().getId().equals(userId)) { + throw new BusinessRuleViolationException("User with id=" + userId + " is not the author of comment with id=" + commentId); + } + if (!comment.isDeleted()) { + comment.setDeleted(true); + commentRepository.save(comment); + } + } + + @Transactional + public CommentDto restoreCommentByAdmin(Long commentId) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new EntityNotFoundException("Comment", "Id", commentId)); + if (comment.isDeleted()) { + comment.setDeleted(false); + commentRepository.save(comment); + } + return commentMapper.toDto(comment); + } +} \ No newline at end of file From 6325f489b09982ce8037c1dd705a68abbf96b7fb Mon Sep 17 00:00:00 2001 From: Valerii_Butko Date: Thu, 29 May 2025 16:55:55 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D1=81=D1=82=D0=B8=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/practicum/explorewithme/main/service/CommentService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java index 42c35fb..e748910 100644 --- a/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentService.java @@ -4,6 +4,8 @@ public interface CommentService { void deleteCommentByAdmin(Long commentId); + void deleteUserComment(Long userId, Long commentId); + CommentDto restoreCommentByAdmin(Long commentId); } \ No newline at end of file From 02694b0c32ab9c14e1177ae101eae1734e9b2693 Mon Sep 17 00:00:00 2001 From: Valerii_Butko Date: Fri, 30 May 2025 11:46:48 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/AdminCommentController.java | 5 +++-- .../controller/priv/PrivateCommentController.java | 2 -- .../main/service/CommentServiceImpl.java | 12 +++++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java index d563f52..f5567e9 100644 --- a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/admin/AdminCommentController.java @@ -1,5 +1,6 @@ package ru.practicum.explorewithme.main.controller.admin; +import jakarta.validation.constraints.Positive; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -19,7 +20,7 @@ public class AdminCommentController { @DeleteMapping("/{commentId}") @ResponseStatus(HttpStatus.NO_CONTENT) - public void deleteComment(@PathVariable Long commentId) { + public void deleteComment(@PathVariable @Positive Long commentId) { log.info("Admin: Received request to delete comment with Id: {}", commentId); commentService.deleteCommentByAdmin(commentId); log.info("Admin: Comment with Id: {} marked as deleted", commentId); @@ -27,7 +28,7 @@ public void deleteComment(@PathVariable Long commentId) { @PatchMapping("/{commentId}/restore") @ResponseStatus(HttpStatus.OK) - public CommentDto restoreComment(@PathVariable Long commentId) { + public CommentDto restoreComment(@PathVariable @Positive Long commentId) { log.info("Admin: Received request to restore comment with Id: {}", commentId); CommentDto restoredComment = commentService.restoreCommentByAdmin(commentId); log.info("Admin: Comment with Id: {} restored", commentId); diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java index 98fea5c..1e12c80 100644 --- a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java @@ -7,7 +7,6 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import ru.practicum.explorewithme.main.service.CommentService; -import ru.practicum.explorewithme.main.service.RequestService; @RestController @RequestMapping("/users/{userId}") @@ -17,7 +16,6 @@ public class PrivateCommentController { private final CommentService commentService; - private final RequestService requestService; @DeleteMapping("/comments/{commentId}") @ResponseStatus(HttpStatus.NO_CONTENT) diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java index edd34ee..a4a8a2b 100644 --- a/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/service/CommentServiceImpl.java @@ -4,7 +4,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.practicum.explorewithme.main.dto.CommentDto; -import ru.practicum.explorewithme.main.error.BusinessRuleViolationException; import ru.practicum.explorewithme.main.error.EntityNotFoundException; import ru.practicum.explorewithme.main.mapper.CommentMapper; import ru.practicum.explorewithme.main.model.Comment; @@ -18,22 +17,24 @@ public class CommentServiceImpl implements CommentService { private final CommentRepository commentRepository; private final CommentMapper commentMapper; + @Override @Transactional public void deleteCommentByAdmin(Long commentId) { Comment comment = commentRepository.findById(commentId) - .orElseThrow(() -> new EntityNotFoundException("Comment", "Id", commentId)); + .orElseThrow(() -> new EntityNotFoundException(String.format("Comment with id=%d not found", commentId))); if (!comment.isDeleted()) { comment.setDeleted(true); commentRepository.save(comment); } } + @Override @Transactional public void deleteUserComment(Long userId, Long commentId) { Comment comment = commentRepository.findById(commentId) - .orElseThrow(() -> new EntityNotFoundException("Comment", "Id", commentId)); + .orElseThrow(() -> new EntityNotFoundException(String.format("Comment with id=%d not found", commentId))); if (!comment.getAuthor().getId().equals(userId)) { - throw new BusinessRuleViolationException("User with id=" + userId + " is not the author of comment with id=" + commentId); + throw new EntityNotFoundException(String.format("Comment with id=%d not found for user with id=%d", commentId, userId)); } if (!comment.isDeleted()) { comment.setDeleted(true); @@ -41,10 +42,11 @@ public void deleteUserComment(Long userId, Long commentId) { } } + @Override @Transactional public CommentDto restoreCommentByAdmin(Long commentId) { Comment comment = commentRepository.findById(commentId) - .orElseThrow(() -> new EntityNotFoundException("Comment", "Id", commentId)); + .orElseThrow(() -> new EntityNotFoundException(String.format("Comment with id=%d not found", commentId))); if (comment.isDeleted()) { comment.setDeleted(false); commentRepository.save(comment); From da0a11d7a59ec0cbda30a9f0df8bd4003ef3a3d4 Mon Sep 17 00:00:00 2001 From: Valerii_Butko Date: Sun, 1 Jun 2025 00:44:57 +0500 Subject: [PATCH 4/4] =?UTF-8?q?=D1=81=D1=82=D0=B8=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/controller/priv/PrivateCommentController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java index 22778bd..c1d4196 100644 --- a/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java +++ b/main-service/src/main/java/ru/practicum/explorewithme/main/controller/priv/PrivateCommentController.java @@ -30,7 +30,7 @@ public ResponseEntity createComment( @PathVariable @Positive Long userId, @RequestParam @Positive Long eventId, @Valid @RequestBody NewCommentDto newCommentDto) { - log.info("Создание нового комментария {} зарегистрированным пользователем c id {} к событию с id {}", + log.info("Создание нового комментария {} зарегистрированным пользователем c id {} к событию с id {}", newCommentDto, userId, eventId); return ResponseEntity.status(HttpStatus.CREATED) .body(commentService.addComment(userId, eventId, newCommentDto)); @@ -41,7 +41,7 @@ public ResponseEntity updateComment( @PathVariable @Positive Long userId, @PathVariable @Positive Long commentId, @Valid @RequestBody UpdateCommentDto updateCommentDto) { - log.info("Обновление комментария c id {} пользователем c id {}, новый комментарий {}", + log.info("Обновление комментария c id {} пользователем c id {}, новый комментарий {}", commentId, userId, updateCommentDto); return ResponseEntity.status(HttpStatus.OK) .body(commentService.updateUserComment(userId, commentId, updateCommentDto));