Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# java-explore-with-me
Template repository for ExploreWithMe project.

https://github.com/flykeeperB/java-explore-with-me/pull/5
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.practicum.ewm.comment.controller.admin;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.ewm.comment.dto.CommentVersionDto;
import ru.practicum.ewm.comment.service.CommentService;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.util.List;

@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/admin/comments")
public class AdminCommentController {

private final CommentService commentService;

@DeleteMapping("/{commentId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCommentByAdmin(@PathVariable @NotNull Long commentId) {

commentService.deleteCommentByAdmin(commentId);
}

@GetMapping("/history/{commentId}")
public List<CommentVersionDto> getHistoryOfCommentByAdmin(@PathVariable @NotNull Long commentId,
@RequestParam(required = false, defaultValue = "0") @PositiveOrZero Integer from,
@RequestParam(required = false, defaultValue = "10") @Positive Integer size) {

return commentService.getCommentsHistoryForAdmin(commentId, size, from);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.practicum.ewm.comment.controller.priv;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.ewm.comment.dto.CommentDto;
import ru.practicum.ewm.comment.dto.CommentVersionDto;
import ru.practicum.ewm.comment.dto.NewCommentDto;
import ru.practicum.ewm.comment.dto.UpdateCommentDto;
import ru.practicum.ewm.comment.service.CommentService;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.util.List;

@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/users/{userId}/comments")
public class PrivateCommentController {

private final CommentService commentService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CommentDto createComment(@PathVariable @NotNull Long userId,
@RequestBody @Valid NewCommentDto newCommentDto) {
return commentService.createCommentByUser(userId, newCommentDto);
}

@PatchMapping("/{commentId}")
public CommentDto updateCommentByUser(@PathVariable @NotNull Long userId,
@RequestBody @Valid UpdateCommentDto updateCommentDto) {

return commentService.updateCommentByUser(userId, updateCommentDto);
}

@GetMapping
public List<CommentDto> getAllCommentsOfUser(@PathVariable @NotNull Long userId,
@RequestParam(required = false, defaultValue = "0")
@PositiveOrZero Integer from,
@RequestParam(required = false, defaultValue = "10")
@Positive Integer size) {

return commentService.getCommentsByUserId(userId, size, from);
}

@GetMapping("/history/{commentId}")
public List<CommentVersionDto> getHistoryOfComment(@PathVariable @NotNull Long userId,
@PathVariable @NotNull Long commentId,
@RequestParam(required = false, defaultValue = "0") @PositiveOrZero Integer from,
@RequestParam(required = false, defaultValue = "10") @Positive Integer size) {

return commentService.getCommentsHistory(userId, commentId, size, from);
}

@DeleteMapping("/{commentId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteComment(@PathVariable @NotNull Long userId,
@PathVariable @NotNull Long commentId) {

commentService.deleteCommentByUser(userId, commentId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.practicum.ewm.comment.controller.publ;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.ewm.comment.dto.CommentDto;
import ru.practicum.ewm.comment.service.CommentService;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.util.List;

@RestController
@Validated
@RequiredArgsConstructor
@RequestMapping("/comments")
public class PublicCommentController {

private final CommentService commentService;

@GetMapping("/{commentId}")
public CommentDto getCommentById(@PathVariable @NotNull Long commentId) {

return commentService.getCommentById(commentId);
}

@GetMapping("/events/{eventId}")
public List<CommentDto> getAllCommentsByEventId(@PathVariable @NotNull Long eventId,
@RequestParam(required = false, defaultValue = "0")
@PositiveOrZero Integer from,
@RequestParam(required = false, defaultValue = "10")
@Positive Integer size) {

return commentService.getCommentsByEventId(eventId, size, from);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.practicum.ewm.comment.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommentDto {
private Long id;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime created;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastChanged;

private String authorName;
private Long authorId;
private Long eventId;
private Long replyToCommentId;
private String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.practicum.ewm.comment.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommentVersionDto {
private Long id;

private Long commentId;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime changed;

private Long replyToCommentId;
private String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.practicum.ewm.comment.dto;

import lombok.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NewCommentDto {
@NotNull
private Long eventId;

private Long replyToCommentId;

@NotBlank
@Size(min = 5, max = 1000)
private String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.practicum.ewm.comment.dto;

import lombok.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UpdateCommentDto {
@NotNull
private Long id;

private Long replyToCommentId;

@NotBlank
@Size(min = 5, max = 1000)
private String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.practicum.ewm.comment.mapper;

import ru.practicum.ewm.comment.dto.CommentDto;
import ru.practicum.ewm.comment.dto.CommentVersionDto;
import ru.practicum.ewm.comment.dto.NewCommentDto;
import ru.practicum.ewm.comment.model.Comment;
import ru.practicum.ewm.comment.model.CommentVersion;
import ru.practicum.ewm.event.model.Event;
import ru.practicum.ewm.user.model.User;

import java.util.List;

public interface CommentMapper {
Comment mapToComment(NewCommentDto newCommentDto,
User author,
Event event,
Comment replyToComment);

CommentDto mapToCommentDto(Comment comment);

List<CommentDto> mapToCommentDto(List<Comment> comments);

CommentVersion mapToCommentVersion(Comment comment);

CommentVersionDto mapToCommentVersionDto(CommentVersion commentVersion);

List<CommentVersionDto> mapToCommentVersionDto(List<CommentVersion> commentsHistory);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ru.practicum.ewm.comment.mapper.impl;

import org.springframework.stereotype.Service;
import ru.practicum.ewm.comment.dto.CommentDto;
import ru.practicum.ewm.comment.dto.CommentVersionDto;
import ru.practicum.ewm.comment.dto.NewCommentDto;
import ru.practicum.ewm.comment.mapper.CommentMapper;
import ru.practicum.ewm.comment.model.Comment;
import ru.practicum.ewm.comment.model.CommentVersion;
import ru.practicum.ewm.event.model.Event;
import ru.practicum.ewm.user.model.User;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentMapperImpl implements CommentMapper {
@Override
public Comment mapToComment(NewCommentDto newCommentDto,
User author,
Event event,
Comment replyToComment) {
return Comment.builder()
.author(author)
.event(event)
.replyToComment(replyToComment)
.actualText(newCommentDto.getText())
.build();
}

@Override
public CommentDto mapToCommentDto(Comment comment) {
CommentDto result = CommentDto.builder()
.id(comment.getId())
.authorId(comment.getAuthor().getId())
.authorName(comment.getAuthor().getName())
.created(comment.getCreated())
.eventId(comment.getEvent().getId())
.lastChanged(comment.getLastChanged())
.text(comment.getActualText())
.build();
if (comment.getReplyToComment() != null) {
result.setReplyToCommentId(comment.getReplyToComment().getId());
}

return result;
}

@Override
public List<CommentDto> mapToCommentDto(List<Comment> comments) {
return comments.stream().map(this::mapToCommentDto).collect(Collectors.toList());
}

@Override
public CommentVersion mapToCommentVersion(Comment comment) {
return CommentVersion.builder()
.comment(comment)
.changed(comment.getLastChanged())
.replyToComment(comment.getReplyToComment())
.text(comment.getActualText())
.build();
}

@Override
public CommentVersionDto mapToCommentVersionDto(CommentVersion commentVersion) {
CommentVersionDto result = CommentVersionDto.builder()
.id(commentVersion.getId())
.changed(commentVersion.getChanged())
.commentId(commentVersion.getComment().getId())
.text(commentVersion.getText())
.build();

if (commentVersion.getReplyToComment() != null) {
result.setReplyToCommentId(commentVersion.getReplyToComment().getId());
}

return result;
}

@Override
public List<CommentVersionDto> mapToCommentVersionDto(List<CommentVersion> commentsHistory) {
return commentsHistory.stream().map(this::mapToCommentVersionDto).collect(Collectors.toList());
}
}
Loading