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.Comments.Controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.Comments.DTO.CommentDTO;
import ru.practicum.Comments.DTO.NewCommentDTO;
import ru.practicum.Comments.DTO.UpdateCommentDTO;
import ru.practicum.Comments.Service.Private.CommentPrivateServiceImpl;


@RestController
@RequestMapping("/events/comments")
@RequiredArgsConstructor
public class CommentPrivateController {
private final CommentPrivateServiceImpl commentService;
public static final String USER_ID = "X-User-Id";

@PostMapping("/{eventId}")
@ResponseStatus(HttpStatus.CREATED)
public CommentDTO addComment(@RequestBody NewCommentDTO commentDTO,
@RequestHeader(USER_ID) Integer userId,
@PathVariable Integer eventId) {
return commentService.addComment(commentDTO, userId, eventId);
}

@GetMapping("/{commentId}")
public CommentDTO getComment(@PathVariable Integer commentId) {
return commentService.getComment(commentId);
}

@PatchMapping("/{commentId}")
public CommentDTO updateComment(@RequestHeader(USER_ID) Integer userId,
@RequestBody UpdateCommentDTO dto,
@PathVariable Integer commentId) {
return commentService.updateComment(dto, userId, commentId);
}

@DeleteMapping("/{commentId}")
public void deleteComment(@RequestHeader(USER_ID) Integer userId,
@PathVariable Integer commentId){
commentService.deleteComment(commentId, userId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.practicum.Comments.Controller;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ru.practicum.Comments.DTO.CommentDTO;
import ru.practicum.Comments.Service.Public.CommentPublicInterface;

import java.util.List;

@RestController
@RequestMapping("/event/{eventId}/comments")
@RequiredArgsConstructor
public class CommentPublicController {
private final CommentPublicInterface commentPublicInterface;

@GetMapping
public List<CommentDTO> getComments(@PathVariable @NotNull Integer eventId,
@RequestParam(defaultValue = "0") @PositiveOrZero Integer from,
@RequestParam(defaultValue = "10") @Positive Integer size) {
return commentPublicInterface.getComments(eventId, from, size);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.practicum.Comments.DTO;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.practicum.Comments.Model.Status;
import ru.practicum.Event.DTO.EventCommentsDTO;
import ru.practicum.User.Model.User;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommentDTO {

private Integer id;

private String comment;

private User author;

private EventCommentsDTO event;

private LocalDateTime createdAt;

private Status status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.practicum.Comments.DTO;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import ru.practicum.StatsRequestDTO;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class NewCommentDTO {

@NotNull
@Size(max = 7000)
@JsonProperty("comment")
private String comment;

@DateTimeFormat(pattern = StatsRequestDTO.DATE_FORMAT)
private final LocalDateTime createdAt = LocalDateTime.now();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.practicum.Comments.DTO;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import ru.practicum.StatsRequestDTO;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UpdateCommentDTO {

@NotNull
@Size(max = 7000)
@JsonProperty("comment")
private String comment;

@DateTimeFormat(pattern = StatsRequestDTO.DATE_FORMAT)
private final LocalDateTime updatedAt = LocalDateTime.now();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.practicum.Comments.DTO;

import java.time.LocalDateTime;

public class UpdatedCommentDTO extends CommentDTO {
private LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.practicum.Comments.Mapper;

import org.mapstruct.Mapper;
import ru.practicum.Comments.DTO.CommentDTO;
import ru.practicum.Comments.DTO.NewCommentDTO;
import ru.practicum.Comments.DTO.UpdateCommentDTO;
import ru.practicum.Comments.DTO.UpdatedCommentDTO;
import ru.practicum.Comments.Model.Comment;

@Mapper(componentModel = "spring")
public interface CommentsMapper {
CommentDTO toCommentDTO(Comment comment);

Comment toComment(NewCommentDTO commentDTO);

UpdatedCommentDTO toUpdatedCommentDTO(Comment comment);

Comment toComment(UpdateCommentDTO commentDTO);
}
44 changes: 44 additions & 0 deletions evm-service/src/main/java/ru/practicum/Comments/Model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ru.practicum.Comments.Model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.practicum.Event.Model.Event;
import ru.practicum.User.Model.User;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "comments")
public class Comment {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;

@Column(name = "comment", length = 7000)
private String comment;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "author_id")
private User author;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "event_id")
private Event event;

@Column(name = "created_at")
private final LocalDateTime createdAt = LocalDateTime.now();

@Column(name = "updated_at")
private LocalDateTime updatedAt;

@Enumerated(EnumType.STRING)
@Column(name = "status")
private Status status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.practicum.Comments.Model;

public enum Status {
CREATED,
UPDATED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.practicum.Comments.Repository;

import org.springframework.data.jpa.repository.JpaRepository;
import ru.practicum.Comments.Model.Comment;

import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional;

public interface CommentsRepository extends JpaRepository<Comment, Integer> {
Comment save(Comment comment);

List<Comment> findCommentsByEvent_Id(Integer eventId, Pageable pageable);

Optional<Comment> findById(Integer commentId);

Comment findCommentByEvent_IdAndAuthor_Id(Integer eventId, Integer authorId);

Optional<Comment> findCommentById(Integer commentId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.practicum.Comments.Service.Private;

import ru.practicum.Comments.DTO.CommentDTO;
import ru.practicum.Comments.DTO.NewCommentDTO;
import ru.practicum.Comments.DTO.UpdateCommentDTO;

public interface CommentPrivateService {
CommentDTO addComment(NewCommentDTO commentDTO, Integer userId, Integer eventId);

CommentDTO getComment(Integer commentId);

CommentDTO updateComment(UpdateCommentDTO commentDTO, Integer userId, Integer commentId);

void deleteComment(Integer commentId, Integer userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package ru.practicum.Comments.Service.Private;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ru.practicum.Comments.DTO.CommentDTO;
import ru.practicum.Comments.DTO.NewCommentDTO;
import ru.practicum.Comments.DTO.UpdateCommentDTO;
import ru.practicum.Comments.Mapper.CommentsMapper;
import ru.practicum.Comments.Model.Comment;
import ru.practicum.Comments.Model.Status;
import ru.practicum.Comments.Repository.CommentsRepository;
import ru.practicum.Event.Model.Event;
import ru.practicum.Event.Model.State;
import ru.practicum.Event.Repository.EventRepository;
import ru.practicum.Exception.NotFoundException;
import ru.practicum.Exception.ValidationException;
import ru.practicum.Request.Model.Request;
import ru.practicum.Request.Repository.RequestRepository;
import ru.practicum.User.Model.User;
import ru.practicum.User.Repository.UserRepository;

import java.util.Objects;

@Service
@RequiredArgsConstructor
public class CommentPrivateServiceImpl implements CommentPrivateService {
private final CommentsRepository commentsRepository;
private final CommentsMapper commentsMapper;
private final UserRepository userRepository;
private final EventRepository eventRepository;
private final RequestRepository requestRepository;

@Override
public CommentDTO addComment(NewCommentDTO commentDTO, Integer userId, Integer eventId) {
User user = checkUser(userId);
Event event = checkEvent(eventId);

if (event.getEventDate().isBefore(commentDTO.getCreatedAt())) {
throw new ValidationException("Event date is before comment date");
}

Request request;
try {
request = requestRepository.findRequestsByEvent_IdAndRequester_Id(eventId, userId);
} catch (NotFoundException e) {
throw new NotFoundException("Request for user with id " + userId +
" and event with id " + eventId + " not found");
}

if (!request.getStatus().equals(State.CONFIRMED)) {
throw new ValidationException("Request is not confirmed");
}

Comment comment = commentsMapper.toComment(commentDTO);
comment.setStatus(Status.CREATED);
comment.setAuthor(user);
comment.setEvent(event);

return commentsMapper.toCommentDTO(commentsRepository.save(comment));
}

@Override
public CommentDTO getComment(Integer commentId) {
Comment comment = commentsRepository.findById(commentId)
.orElseThrow(() -> new NotFoundException("Comment with id " + commentId + " not found"));

if (comment.getStatus().equals(Status.CREATED)) {
return commentsMapper.toCommentDTO(comment);
} else {
return commentsMapper.toUpdatedCommentDTO(comment);
}
}

@Override
public CommentDTO updateComment(UpdateCommentDTO commentDTO, Integer userId, Integer commentId) {
User user = checkUser(userId);
Comment comment = commentsRepository.findById(commentId)
.orElseThrow(() -> new NotFoundException("Comment with id " + commentId + " not found"));

if (!Objects.equals(comment.getAuthor().getId(), user.getId())) {
throw new ValidationException("You are not allowed to update this comment");
}

comment.setComment(commentDTO.getComment());
comment.setStatus(Status.UPDATED);
comment.setUpdatedAt(commentDTO.getUpdatedAt());

return commentsMapper.toUpdatedCommentDTO(commentsRepository.save(comment));
}

@Override
public void deleteComment(Integer commentId, Integer userId) {
User user = checkUser(userId);
Comment comment = commentsRepository.findById(commentId)
.orElseThrow(() -> new NotFoundException("Comment with id " + commentId + " not found"));
if (!Objects.equals(comment.getAuthor().getId(), user.getId())) {
throw new ValidationException("You are not allowed to delete this comment");
}
commentsRepository.deleteById(commentId);
}


private User checkUser(Integer id) {
return userRepository.findById(id)
.orElseThrow(() -> new NotFoundException("User with id " + id + " not found"));
}

private Event checkEvent(Integer id) {
return eventRepository.findEventById(id)
.orElseThrow(() -> new NotFoundException("Event with id " + id + " not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.practicum.Comments.Service.Public;

import ru.practicum.Comments.DTO.CommentDTO;

import java.util.List;

public interface CommentPublicInterface {
List<CommentDTO> getComments(Integer eventId, Integer from, Integer size);
}
Loading
Loading