Skip to content

Commit 991d357

Browse files
committed
stage 3 - add feature comments
1 parent a5d367d commit 991d357

19 files changed

Lines changed: 1432 additions & 0 deletions
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.practicum.ewm.comment.controller;
2+
3+
import jakarta.validation.Valid;
4+
import jakarta.validation.constraints.Positive;
5+
import jakarta.validation.constraints.PositiveOrZero;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.*;
10+
import ru.practicum.ewm.comment.dto.AdminCommentUpdateRequest;
11+
import ru.practicum.ewm.comment.dto.CommentDto;
12+
import ru.practicum.ewm.comment.service.CommentService;
13+
14+
import java.util.List;
15+
16+
@RestController
17+
@RequestMapping("/admin/comments")
18+
@RequiredArgsConstructor
19+
@Validated
20+
public class AdminCommentController {
21+
private final CommentService commentService;
22+
23+
@GetMapping
24+
public List<CommentDto> searchComments(@RequestParam(required = false) Long eventId,
25+
@RequestParam(required = false) Long authorId,
26+
@RequestParam(required = false) String status,
27+
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
28+
@RequestParam(defaultValue = "10") @Positive int size) {
29+
return commentService.searchCommentsAdmin(eventId, authorId, status, from, size);
30+
}
31+
32+
@PatchMapping("/{commentId}")
33+
public CommentDto updateCommentStatus(@PathVariable Long commentId,
34+
@Valid @RequestBody AdminCommentUpdateRequest request) {
35+
return commentService.updateCommentStatusByAdmin(commentId, request);
36+
}
37+
38+
@DeleteMapping("/{commentId}")
39+
@ResponseStatus(HttpStatus.NO_CONTENT)
40+
public void deleteComment(@PathVariable Long commentId) {
41+
commentService.deleteCommentByAdmin(commentId);
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.practicum.ewm.comment.controller;
2+
3+
import jakarta.validation.Valid;
4+
import jakarta.validation.constraints.Positive;
5+
import jakarta.validation.constraints.PositiveOrZero;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.*;
10+
import ru.practicum.ewm.comment.dto.CommentDto;
11+
import ru.practicum.ewm.comment.dto.NewCommentDto;
12+
import ru.practicum.ewm.comment.dto.UpdateCommentRequest;
13+
import ru.practicum.ewm.comment.service.CommentService;
14+
15+
import java.util.List;
16+
17+
@RestController
18+
@RequestMapping("/users/{userId}")
19+
@RequiredArgsConstructor
20+
@Validated
21+
public class PrivateCommentController {
22+
private final CommentService commentService;
23+
24+
@PostMapping("/events/{eventId}/comments")
25+
@ResponseStatus(HttpStatus.CREATED)
26+
public CommentDto createComment(@PathVariable Long userId,
27+
@PathVariable Long eventId,
28+
@Valid @RequestBody NewCommentDto dto) {
29+
return commentService.createComment(userId, eventId, dto);
30+
}
31+
32+
@PatchMapping("/comments/{commentId}")
33+
public CommentDto updateComment(@PathVariable Long userId,
34+
@PathVariable Long commentId,
35+
@Valid @RequestBody UpdateCommentRequest request) {
36+
return commentService.updateComment(userId, commentId, request);
37+
}
38+
39+
@DeleteMapping("/comments/{commentId}")
40+
@ResponseStatus(HttpStatus.NO_CONTENT)
41+
public void deleteComment(@PathVariable Long userId,
42+
@PathVariable Long commentId) {
43+
commentService.deleteCommentByUser(userId, commentId);
44+
}
45+
46+
@GetMapping("/comments")
47+
public List<CommentDto> getUserComments(@PathVariable Long userId,
48+
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
49+
@RequestParam(defaultValue = "10") @Positive int size) {
50+
return commentService.getUserComments(userId, from, size);
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.practicum.ewm.comment.controller;
2+
3+
import jakarta.validation.constraints.Positive;
4+
import jakarta.validation.constraints.PositiveOrZero;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.validation.annotation.Validated;
7+
import org.springframework.web.bind.annotation.*;
8+
import ru.practicum.ewm.comment.dto.CommentDto;
9+
import ru.practicum.ewm.comment.service.CommentService;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping("/events/{eventId}/comments")
15+
@RequiredArgsConstructor
16+
@Validated
17+
public class PublicCommentController {
18+
private final CommentService commentService;
19+
20+
@GetMapping
21+
public List<CommentDto> getPublishedComments(@PathVariable Long eventId,
22+
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
23+
@RequestParam(defaultValue = "10") @Positive int size) {
24+
return commentService.getPublishedCommentsByEvent(eventId, from, size);
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.practicum.ewm.comment.dto;
2+
3+
import jakarta.validation.constraints.NotNull;
4+
import lombok.Data;
5+
import ru.practicum.ewm.comment.model.CommentStatus;
6+
7+
@Data
8+
public class AdminCommentUpdateRequest {
9+
@NotNull(message = "Статус обязателен")
10+
private CommentStatus status;
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.practicum.ewm.comment.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import ru.practicum.ewm.user.dto.UserShortDto;
8+
9+
import java.time.LocalDateTime;
10+
11+
@Data
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class CommentDto {
15+
private Long id;
16+
private String text;
17+
private UserShortDto author;
18+
private Long eventId;
19+
20+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
21+
private LocalDateTime createdOn;
22+
23+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
24+
private LocalDateTime editedOn;
25+
26+
private String status;
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.practicum.ewm.comment.dto;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.Size;
5+
import lombok.Data;
6+
7+
@Data
8+
public class NewCommentDto {
9+
@NotBlank(message = "Текст комментария не может быть пустым")
10+
@Size(min = 1, max = 2000, message = "Длина комментария от 1 до 2000 символов")
11+
private String text;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.practicum.ewm.comment.dto;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.Size;
5+
import lombok.Data;
6+
7+
@Data
8+
public class UpdateCommentRequest {
9+
@NotBlank(message = "Текст комментария не может быть пустым")
10+
@Size(min = 1, max = 2000, message = "Длина комментария от 1 до 2000 символов")
11+
private String text;
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.practicum.ewm.comment.mapper;
2+
3+
import ru.practicum.ewm.comment.dto.CommentDto;
4+
import ru.practicum.ewm.comment.dto.NewCommentDto;
5+
import ru.practicum.ewm.comment.model.Comment;
6+
import ru.practicum.ewm.comment.model.CommentStatus;
7+
import ru.practicum.ewm.event.model.Event;
8+
import ru.practicum.ewm.user.mapper.UserMapper;
9+
import ru.practicum.ewm.user.model.User;
10+
11+
import java.time.LocalDateTime;
12+
13+
public class CommentMapper {
14+
15+
public static Comment toComment(NewCommentDto dto, User author, Event event) {
16+
Comment comment = new Comment();
17+
comment.setText(dto.getText());
18+
comment.setAuthor(author);
19+
comment.setEvent(event);
20+
comment.setCreatedOn(LocalDateTime.now());
21+
comment.setStatus(CommentStatus.PUBLISHED);
22+
return comment;
23+
}
24+
25+
public static CommentDto toCommentDto(Comment comment) {
26+
return new CommentDto(
27+
comment.getId(),
28+
comment.getText(),
29+
UserMapper.toUserShortDto(comment.getAuthor()),
30+
comment.getEvent().getId(),
31+
comment.getCreatedOn(),
32+
comment.getEditedOn(),
33+
comment.getStatus().name()
34+
);
35+
}
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ru.practicum.ewm.comment.model;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import ru.practicum.ewm.event.model.Event;
8+
import ru.practicum.ewm.user.model.User;
9+
10+
import java.time.LocalDateTime;
11+
12+
@Entity
13+
@Table(name = "comments")
14+
@Data
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class Comment {
18+
@Id
19+
@GeneratedValue(strategy = GenerationType.IDENTITY)
20+
private Long id;
21+
22+
@Column(nullable = false, columnDefinition = "TEXT")
23+
private String text;
24+
25+
@Column(name = "created_on", nullable = false)
26+
private LocalDateTime createdOn;
27+
28+
@Column(name = "edited_on")
29+
private LocalDateTime editedOn;
30+
31+
@Enumerated(EnumType.STRING)
32+
@Column(nullable = false, length = 20)
33+
private CommentStatus status;
34+
35+
@ManyToOne(fetch = FetchType.LAZY)
36+
@JoinColumn(name = "author_id", nullable = false)
37+
private User author;
38+
39+
@ManyToOne(fetch = FetchType.LAZY)
40+
@JoinColumn(name = "event_id", nullable = false)
41+
private Event event;
42+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ru.practicum.ewm.comment.model;
2+
3+
public enum CommentStatus {
4+
PUBLISHED,
5+
BLOCKED
6+
}

0 commit comments

Comments
 (0)