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
515 changes: 0 additions & 515 deletions Postman/ewm-stat-service.json

This file was deleted.

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/Ilusha92/java-explore-with-me/pull/8
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class MainCommonUtils {
public static final int MAX_LENGTH_TITLE = 120;
public static final int MIN_LENGTH_UPDATE_TITLE = 3;
public static final int MAX_LENGTH_UPDATE_TITLE = 50;
// public static final int MIN_LENGTH_COMMENT = 3;
// public static final int MAX_LENGTH_COMMENT = 7000;
public static final int MIN_LENGTH_COMMENT = 3;
public static final int MAX_LENGTH_COMMENT = 7000;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.practicum.main_service.comment.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.main_service.MainCommonUtils;
import ru.practicum.main_service.comment.dto.CommentDto;
import ru.practicum.main_service.comment.service.CommentService;

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

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

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<CommentDto> getCommentsByAdmin(
@RequestParam(required = false, defaultValue = MainCommonUtils.PAGE_DEFAULT_FROM) @PositiveOrZero Integer from,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultValue уже подразумевает, что параметра может и не быть. Поэтому вполне можно не писать required = false

@RequestParam(required = false, defaultValue = MainCommonUtils.PAGE_DEFAULT_SIZE) @Positive Integer size) {
return commentService.getCommentsByAdmin(PageRequest.of(from / size, size));
}

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

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.main_service.MainCommonUtils;
import ru.practicum.main_service.comment.dto.CommentDto;
import ru.practicum.main_service.comment.dto.NewCommentDto;
import ru.practicum.main_service.comment.service.CommentService;

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

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

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CommentDto createByPrivate(@PathVariable Long userId,
@RequestParam Long eventId,
@Valid @RequestBody NewCommentDto newCommentDto) {
return commentService.createByPrivate(userId, eventId, newCommentDto);
}

@PatchMapping("/{commentId}")
@ResponseStatus(HttpStatus.OK)
public CommentDto patchByPrivate(@PathVariable Long userId,
@PathVariable Long commentId,
@Valid @RequestBody NewCommentDto newCommentDto) {
return commentService.patchByPrivate(userId, commentId, newCommentDto);
}

@DeleteMapping("/{commentId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteByPrivate(@PathVariable Long userId,
@PathVariable Long commentId) {
commentService.deleteByPrivate(userId, commentId);
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<CommentDto> getCommentsByPrivate(
@PathVariable Long userId,
@RequestParam(required = false) Long eventId,
@RequestParam(required = false, defaultValue = MainCommonUtils.PAGE_DEFAULT_FROM) @PositiveOrZero Integer from,
@RequestParam(required = false, defaultValue = MainCommonUtils.PAGE_DEFAULT_SIZE) @Positive Integer size) {
return commentService.getCommentsByPrivate(userId, eventId, PageRequest.of(from / size, size));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.practicum.main_service.comment.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.main_service.MainCommonUtils;
import ru.practicum.main_service.comment.dto.CommentDto;
import ru.practicum.main_service.comment.service.CommentService;

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

@RestController
@RequiredArgsConstructor
@RequestMapping("/comments")
@Validated
public class CommentPublicController {
private final CommentService commentService;

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<CommentDto> getCommentsByPublic(
@RequestParam Long eventId,
@RequestParam(required = false, defaultValue = MainCommonUtils.PAGE_DEFAULT_FROM) @PositiveOrZero Integer from,
@RequestParam(required = false, defaultValue = MainCommonUtils.PAGE_DEFAULT_SIZE) @Positive Integer size) {
return commentService.getCommentsByPublic(eventId, PageRequest.of(from / size, size));
}

@GetMapping("/{commentId}")
@ResponseStatus(HttpStatus.OK)
public CommentDto getCommentByPublic(@PathVariable Long commentId) {
return commentService.getCommentByPublic(commentId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.practicum.main_service.comment.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import ru.practicum.main_service.MainCommonUtils;
import ru.practicum.main_service.user.dto.UserShortDto;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommentDto {
private Long id;
private String text;
private UserShortDto author;
private Long eventId;

@JsonFormat(pattern = MainCommonUtils.DT_FORMAT, shape = JsonFormat.Shape.STRING)
private LocalDateTime createdOn;

@JsonFormat(pattern = MainCommonUtils.DT_FORMAT, shape = JsonFormat.Shape.STRING)
private LocalDateTime editedOn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.practicum.main_service.comment.dto;

import lombok.*;
import ru.practicum.main_service.MainCommonUtils;

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

@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class NewCommentDto {
@NotBlank
@Size(min = MainCommonUtils.MIN_LENGTH_COMMENT, max = MainCommonUtils.MAX_LENGTH_COMMENT)
private String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.practicum.main_service.comment.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import ru.practicum.main_service.comment.dto.CommentDto;
import ru.practicum.main_service.comment.model.Comment;
import ru.practicum.main_service.user.mapper.UserMapper;

@Mapper(componentModel = "spring", uses = {UserMapper.class})
public interface CommentMapper {
@Mapping(target = "eventId", expression = "java(comment.getEvent().getId())")
CommentDto toCommentDto(Comment comment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.practicum.main_service.comment.model;

import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import ru.practicum.main_service.MainCommonUtils;
import ru.practicum.main_service.event.model.Event;
import ru.practicum.main_service.user.model.User;

import javax.persistence.*;
import java.time.LocalDateTime;

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

@Column(length = MainCommonUtils.MAX_LENGTH_COMMENT, nullable = false)
private String text;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "author_id", referencedColumnName = "id")
@OnDelete(action = OnDeleteAction.CASCADE)
private User author;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "event_id", referencedColumnName = "id")
@OnDelete(action = OnDeleteAction.CASCADE)
private Event event;

@Column(nullable = false)
private LocalDateTime createdOn;

private LocalDateTime editedOn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.practicum.main_service.comment.repository;

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

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findAllByAuthorId(Long userId);

List<Comment> findAllByAuthorIdAndEventId(Long userId, Long eventId);

List<Comment> findAllByEventId(Long eventId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.practicum.main_service.comment.service;

import org.springframework.data.domain.Pageable;
import ru.practicum.main_service.comment.dto.CommentDto;
import ru.practicum.main_service.comment.dto.NewCommentDto;

import java.util.List;

public interface CommentService {
List<CommentDto> getCommentsByAdmin(Pageable pageable);

void deleteByAdmin(Long commentId);

List<CommentDto> getCommentsByPrivate(Long userId, Long eventId, Pageable pageable);

CommentDto createByPrivate(Long userId, Long eventId, NewCommentDto newCommentDto);

CommentDto patchByPrivate(Long userId, Long commentId, NewCommentDto newCommentDto);

void deleteByPrivate(Long userId, Long commentId);

List<CommentDto> getCommentsByPublic(Long eventId, Pageable pageable);

CommentDto getCommentByPublic(Long commentId);
}
Loading