Skip to content
Closed
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
@@ -0,0 +1,45 @@
package ru.practicum.comment.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.comment.dto.CommentRequestDto;
import ru.practicum.comment.dto.CommentResponseDto;
import ru.practicum.comment.service.CommentService;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(path = "/admin/comments")
public class CommentAdminController {
private final CommentService commentService;

@PatchMapping("/{commentId}")
public CommentResponseDto updateCommentByAdmin(
@PathVariable Long commentId,
@RequestBody @Valid CommentRequestDto commentRequestDto) {
log.info("PATCH /admin/comments/{}", commentId);
return commentService.updateCommentByAdmin(commentId, commentRequestDto);
}

@GetMapping("/search")
public List<CommentResponseDto> searchComments(@RequestParam String text,
@RequestParam(defaultValue = "0") @PositiveOrZero Integer from,
@RequestParam(defaultValue = "10") @Positive Integer size) {
log.info("GET /admin/comments/{}", text);
return commentService.searchComments(text, from, size);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{commentId}")
public void deleteCommentByAdmin(@PathVariable Long commentId) {
log.info("DELETE /admin/comments/{}", commentId);
commentService.deleteCommentByAdmin(commentId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.practicum.comment.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.comment.dto.CommentRequestDto;
import ru.practicum.comment.dto.CommentResponseDto;
import ru.practicum.comment.service.CommentService;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(path = "/users/{userId}/comments")
public class CommentPrivateController {
private final CommentService commentService;

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public CommentResponseDto createComment(@PathVariable Long userId,
@RequestParam Long eventId,
@RequestBody @Valid CommentRequestDto commentRequestDto) {
log.info("Запрос на комментарий в событии с id= {}. POST /users/{}/comments", eventId, userId);
return commentService.createComment(userId, eventId, commentRequestDto);
}

@PatchMapping("/{commentId}")
public CommentResponseDto updateComment(@PathVariable Long userId,
@PathVariable Long commentId,
@RequestBody @Valid CommentRequestDto commentRequestDto) {
log.info("PATCH /users/{}/comments/{}", userId, commentId);
return commentService.updateComment(userId, commentId, commentRequestDto);
}

@GetMapping
public List<CommentResponseDto> getUserComments(@PathVariable Long userId,
@RequestParam(defaultValue = "0") @PositiveOrZero Integer from,
@RequestParam(defaultValue = "10") @Positive Integer size) {
log.info("GET /users/{}/comments?from={}&size={}", userId, from, size);
return commentService.getUserComments(userId, from, size);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{commentId}")
public void deleteComment(@PathVariable Long userId, @PathVariable Long commentId) {
commentService.deleteComment(userId, commentId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.practicum.comment.controller;

import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ru.practicum.comment.dto.CommentResponseDto;
import ru.practicum.comment.service.CommentService;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(path = "/events/{eventId}/comments")
public class CommentPublicController {
private final CommentService commentService;

@GetMapping
public List<CommentResponseDto> getCommentsByEvent(
@PathVariable Long eventId,
@RequestParam(defaultValue = "0") @PositiveOrZero Integer from,
@RequestParam(defaultValue = "10") @Positive Integer size) {
log.info("GET /comments?eventId={}&from={}&size={}", eventId, from, size);
return commentService.getCommentsByEvent(eventId, from, size);
}

@GetMapping("/{commentId}")
public CommentResponseDto getCommentById(@PathVariable Long eventId,
@PathVariable Long commentId) {
log.info("GET /events/{}/comments/{}", eventId, commentId);
return commentService.getCommentById(eventId, commentId);
}
}
36 changes: 36 additions & 0 deletions main/src/main/java/ru/practicum/comment/dto/CommentMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.practicum.comment.dto;

import lombok.experimental.UtilityClass;
import ru.practicum.comment.model.Comment;
import ru.practicum.event.model.Event;
import ru.practicum.user.model.User;

import java.time.LocalDateTime;

@UtilityClass
public class CommentMapper {
public static CommentResponseDto toCommentResponseDto(Comment comment) {
return new CommentResponseDto(
comment.getId(),
comment.getText(),
comment.getEvent().getId(),
comment.getAuthor().getId(),
comment.getAuthor().getName(),
comment.getCreated(),
comment.getUpdated(),
comment.getIsEdited()
);
}

public static Comment toComment(CommentRequestDto commentRequestDto, User user, Event event) {
return new Comment(
null,
commentRequestDto.getText(),
event,
user,
LocalDateTime.now(),
null,
false
);
}
}
18 changes: 18 additions & 0 deletions main/src/main/java/ru/practicum/comment/dto/CommentRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.practicum.comment.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class CommentRequestDto {
@NotBlank(message = "Комментарий не может быть пустым")
@Size(min = 1, max = 2000, message = "Комментарий должен быть длинной от {min} до {max} символов")
private String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.practicum.comment.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class CommentResponseDto {
private Long id;
private String text;
private Long eventId;
private Long authorId;
private String authorName;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime created;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updated;
private Boolean isEdited;
}
55 changes: 55 additions & 0 deletions main/src/main/java/ru/practicum/comment/model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.practicum.comment.model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import ru.practicum.event.model.Event;
import ru.practicum.user.model.User;

import java.time.LocalDateTime;
import java.util.Objects;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Table(name = "comments")
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, length = 2000)
private String text;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "event_id", nullable = false)
private Event event;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id", nullable = false)
private User author;

@Column(nullable = false)
private LocalDateTime created;

private LocalDateTime updated;

@Column(name = "is_edited", nullable = false)
private Boolean isEdited = false;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Comment comment)) return false;
return Objects.equals(id, comment.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.practicum.comment.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.practicum.comment.model.Comment;

public interface CommentRepository extends JpaRepository<Comment, Long> {
Page<Comment> findByEventIdOrderByCreatedAsc(Long eventId, Pageable pageable);

Page<Comment> findByAuthorId(Long userId, Pageable pageable);

Page<Comment> findByTextContainingIgnoreCase(String text, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.practicum.comment.service;

import ru.practicum.comment.dto.CommentRequestDto;
import ru.practicum.comment.dto.CommentResponseDto;

import java.util.List;

public interface CommentService {
CommentResponseDto createComment(Long userId, Long eventId, CommentRequestDto commentRequestDto);

CommentResponseDto updateComment(Long userId, Long commentId, CommentRequestDto commentRequestDto);

List<CommentResponseDto> getUserComments(Long userId, Integer from, Integer size);

void deleteComment(Long userId, Long commentId);

List<CommentResponseDto> getCommentsByEvent(Long eventId, Integer from, Integer size);

CommentResponseDto getCommentById(Long eventId, Long commentId);

CommentResponseDto updateCommentByAdmin(Long commentId, CommentRequestDto commentRequestDto);

List<CommentResponseDto> searchComments(String text, Integer from, Integer size);

void deleteCommentByAdmin(Long commentId);
}
Loading
Loading