generated from yandex-praktikum/java-explore-with-me
-
Notifications
You must be signed in to change notification settings - Fork 0
Feature comments #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
83ae03d
feat: добавляем новую функциональность - "комментарии"
andrej1307 819f854
feat: Реализовано создание, изменение комменария
andrej1307 4e32da1
feat: реализован контроллер коментариев CommentController
andrej1307 419fbdb
fix: CommentController
andrej1307 69e9e00
feat: добавлена функция модерации комментариев
andrej1307 fcf26b9
fix: исправление ошибок CommentServiceImpl.java
andrej1307 2d52b3d
feat: реализованы тесты Postman
andrej1307 7cc375d
fix: добавлен тесты Postman
andrej1307 778f616
fix: тесты Postman
andrej1307 e8f917d
fix: тесты Postman, исправлен baseurl
andrej1307 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| # java-explore-with-me | ||
| Итоговый проект учебного курса. май 2025. гр. 53. | ||
| Итоговый проект учебного курса. июль 2025. | ||
|
|
||
| Pull request: | ||
| https://github.com/andrej1307/java-explore-with-me/pull/3 |
Binary file not shown.
This file contains hidden or 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
72 changes: 72 additions & 0 deletions
72
ewm-service/src/main/java/ru/practicum/evmsevice/controller/UserCommentController.java
This file contains hidden or 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,72 @@ | ||
| package ru.practicum.evmsevice.controller; | ||
|
|
||
| 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.evmsevice.dto.CommentDto; | ||
| import ru.practicum.evmsevice.dto.CommentModerationDto; | ||
| import ru.practicum.evmsevice.dto.CommentsGroupDto; | ||
| import ru.practicum.evmsevice.dto.NewCommentDto; | ||
| import ru.practicum.evmsevice.service.CommentService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| @RestController | ||
| @RequestMapping("/users") | ||
| public class UserCommentController { | ||
| private final CommentService commentService; | ||
|
|
||
| @PostMapping("/{userId}/events/{eventId}/comments") | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public CommentDto addNewComment(@PathVariable Integer userId, | ||
| @PathVariable Integer eventId, | ||
| @RequestBody @Validated NewCommentDto commentDto) { | ||
| log.info("Пользователь id={} добавляет комментарий к событию id={}. {}", | ||
| userId, eventId, commentDto.toString()); | ||
| return commentService.addComment(userId, eventId, commentDto); | ||
| } | ||
|
|
||
| @PatchMapping("/{userId}/comments/{commentId}") | ||
| @ResponseStatus(HttpStatus.OK) | ||
| public CommentDto updateComment(@PathVariable Integer userId, | ||
| @PathVariable Integer commentId, | ||
| @RequestBody @Validated NewCommentDto commentDto) { | ||
| log.info("Пользователь id={} редактирует комментарий id={}. {}", | ||
| userId, commentId, commentDto.toString()); | ||
| return commentService.updateComment(userId, commentId, commentDto); | ||
| } | ||
|
|
||
| @PatchMapping("/{userId}/events/{eventId}/comments") | ||
| @ResponseStatus(HttpStatus.OK) | ||
| public CommentsGroupDto addNewComment(@PathVariable Integer userId, | ||
| @PathVariable Integer eventId, | ||
| @RequestBody CommentModerationDto cmModDto) { | ||
| log.info("Пользователь id={} модерирует комментарии к событию id={}. {}", | ||
| userId, eventId, cmModDto.toString()); | ||
| return commentService.moderationComments(userId, eventId, cmModDto); | ||
| } | ||
|
|
||
|
|
||
| @GetMapping("/{userId}/comments") | ||
| @ResponseStatus(HttpStatus.OK) | ||
| public List<CommentDto> getCommentsByUserId(@PathVariable Integer userId, | ||
| @RequestParam(name = "from", defaultValue = "0") Integer from, | ||
| @RequestParam(name = "size", defaultValue = "10") Integer size) { | ||
| log.info("Поиск всех комментариев пользователя id={}.", userId); | ||
| return commentService.getCommentsByUserId(userId, from, size); | ||
| } | ||
|
|
||
|
|
||
| @DeleteMapping("/{userId}/comments/{commentId}") | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| public void deleteComment(@PathVariable Integer commentId, | ||
| @PathVariable Integer userId) { | ||
|
|
||
| log.info("Пользователь id={} удаляет комментарий id={}.", userId, commentId); | ||
| commentService.deleteComment(userId, commentId); | ||
| } | ||
| } | ||
This file contains hidden or 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
26 changes: 26 additions & 0 deletions
26
ewm-service/src/main/java/ru/practicum/evmsevice/dto/CommentDto.java
This file contains hidden or 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,26 @@ | ||
| package ru.practicum.evmsevice.dto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import ru.practicum.evmsevice.enums.CommentState; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Setter | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class CommentDto { | ||
| private Integer id; | ||
| private UserDto author; | ||
| @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
| private LocalDateTime createdOn; | ||
| @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
| private LocalDateTime editedOn; | ||
| private Integer eventId; | ||
| private String text; | ||
| private CommentState state; | ||
| } |
19 changes: 19 additions & 0 deletions
19
ewm-service/src/main/java/ru/practicum/evmsevice/dto/CommentModerationDto.java
This file contains hidden or 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 ru.practicum.evmsevice.dto; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import lombok.ToString; | ||
| import ru.practicum.evmsevice.enums.CommentState; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Setter | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @ToString | ||
| public class CommentModerationDto { | ||
| private List<Integer> commentIds = new ArrayList<>(); | ||
| private CommentState state; | ||
| } |
19 changes: 19 additions & 0 deletions
19
ewm-service/src/main/java/ru/practicum/evmsevice/dto/CommentsGroupDto.java
This file contains hidden or 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 ru.practicum.evmsevice.dto; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import lombok.ToString; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Setter | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @ToString | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лишний отступ |
||
| public class CommentsGroupDto { | ||
| private List<CommentDto> approvedComments = new ArrayList<>(); | ||
| private List<CommentDto> rejectedComments = new ArrayList<>(); | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
ewm-service/src/main/java/ru/practicum/evmsevice/dto/NewCommentDto.java
This file contains hidden or 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,16 @@ | ||
| package ru.practicum.evmsevice.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.*; | ||
|
|
||
| @Setter | ||
| @Getter | ||
| @ToString | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class NewCommentDto { | ||
| @NotBlank(message = "Текст комментария должен быть задан.") | ||
| @Size(min = 3, max = 2000, message = "Длина комментария должна быть от 3 до 2000 символов.") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хорошая валидация |
||
| private String text; | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
ewm-service/src/main/java/ru/practicum/evmsevice/enums/CommentState.java
This file contains hidden or 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,18 @@ | ||
| package ru.practicum.evmsevice.enums; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public enum CommentState { | ||
| PENDING, | ||
| APPROVED, | ||
| REJECTED; | ||
|
|
||
| public static Optional<CommentState> from(String state) { | ||
| for (CommentState commentState : CommentState.values()) { | ||
| if (commentState.name().equalsIgnoreCase(state)) { | ||
| return Optional.of(commentState); | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
ewm-service/src/main/java/ru/practicum/evmsevice/mapper/CommentMapper.java
This file contains hidden or 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,31 @@ | ||
| package ru.practicum.evmsevice.mapper; | ||
|
|
||
| import ru.practicum.evmsevice.dto.CommentDto; | ||
| import ru.practicum.evmsevice.dto.NewCommentDto; | ||
| import ru.practicum.evmsevice.model.Comment; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public class CommentMapper { | ||
| private CommentMapper() { | ||
| } | ||
|
|
||
| public static Comment getComment(NewCommentDto newCommemtDto) { | ||
| Comment comment = new Comment(); | ||
| comment.setCreatedOn(LocalDateTime.now()); | ||
| comment.setText(newCommemtDto.getText()); | ||
| return comment; | ||
| } | ||
|
|
||
| public static CommentDto toDto(Comment comment) { | ||
| CommentDto commentDto = new CommentDto(); | ||
| commentDto.setId(comment.getId()); | ||
| commentDto.setEventId(comment.getEventId()); | ||
| commentDto.setText(comment.getText()); | ||
| commentDto.setCreatedOn(comment.getCreatedOn()); | ||
| commentDto.setEditedOn(comment.getEditedOn()); | ||
| commentDto.setAuthor(UserMapper.toUserDto(comment.getAuthor())); | ||
| commentDto.setState(comment.getState()); | ||
| return commentDto; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
ewm-service/src/main/java/ru/practicum/evmsevice/model/Comment.java
This file contains hidden or 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,34 @@ | ||
| package ru.practicum.evmsevice.model; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import ru.practicum.evmsevice.enums.CommentState; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| @Setter | ||
| @Getter | ||
| @Table(name = "comments", schema = "public") | ||
| @NoArgsConstructor | ||
| public class Comment { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Integer id; | ||
| @ManyToOne | ||
| @JoinColumn(name = "author_id", nullable = false) | ||
| private User author; | ||
| @Column(name = "event_id", nullable = false) | ||
| private Integer eventId; | ||
| @Column(name = "text", nullable = false) | ||
| private String text; | ||
| @Column(name = "created_on", nullable = false) | ||
| private LocalDateTime createdOn; | ||
| @Column(name = "edited_on") | ||
| private LocalDateTime editedOn; | ||
| @Column(name = "state") | ||
| @Enumerated(EnumType.STRING) | ||
| private CommentState state; | ||
| } |
14 changes: 14 additions & 0 deletions
14
ewm-service/src/main/java/ru/practicum/evmsevice/repository/CommentRepository.java
This file contains hidden or 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 ru.practicum.evmsevice.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
| import ru.practicum.evmsevice.model.Comment; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface CommentRepository extends JpaRepository<Comment, Integer>, | ||
| JpaSpecificationExecutor<Comment> { | ||
| List<Comment> findAllByEventId(Integer eventId); | ||
|
|
||
| List<Comment> findAllByAuthor_Id(Integer authorId); | ||
| } |
41 changes: 41 additions & 0 deletions
41
ewm-service/src/main/java/ru/practicum/evmsevice/repository/CommentSpecification.java
This file contains hidden or 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,41 @@ | ||
| package ru.practicum.evmsevice.repository; | ||
|
|
||
| import org.springframework.data.jpa.domain.Specification; | ||
| import ru.practicum.evmsevice.enums.CommentState; | ||
| import ru.practicum.evmsevice.model.Comment; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| public class CommentSpecification { | ||
| public static Specification<Comment> commentEventIdEqual(Integer eventId) { | ||
| return ((root, query, criteriaBuilder) -> | ||
| criteriaBuilder.in(root.get("eventId")).value(eventId)); | ||
| } | ||
|
|
||
| public static Specification<Comment> commentStateEqual(CommentState state) { | ||
| return ((root, query, criteriaBuilder) -> | ||
| criteriaBuilder.in(root.get("state")).value(state)); | ||
| } | ||
|
|
||
| public static Specification<Comment> commentContains(String text) { | ||
| return ((root, query, criteriaBuilder) -> | ||
| criteriaBuilder.like(root.get("text"), "%" + text + "%")); | ||
| } | ||
|
|
||
| public static Specification<Comment> commentAuthorIdIn(List<Integer> userIds) { | ||
| return ((root, query, criteriaBuilder) -> | ||
| criteriaBuilder.in(root.join("author").get("id")).value(userIds)); | ||
| } | ||
|
|
||
| public static Specification<Comment> commentCreatedAfter(LocalDateTime startDateTime) { | ||
| return ((root, query, criteriaBuilder) -> | ||
| criteriaBuilder.greaterThanOrEqualTo(root.get("createdOn"), startDateTime)); | ||
| } | ||
|
|
||
| public static Specification<Comment> commentCreatedBefore(LocalDateTime endDateTime) { | ||
| return ((root, query, criteriaBuilder) -> | ||
| criteriaBuilder.lessThan(root.get("createdOn"), endDateTime)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лишний отступ