Skip to content

Commit

Permalink
Merge pull request #5 from aigunov/feature_comments
Browse files Browse the repository at this point in the history
Реализовывал фичу Комментарии
  • Loading branch information
aigunov authored Sep 23, 2024
2 parents dde8c27 + 26fe454 commit 195b46b
Show file tree
Hide file tree
Showing 66 changed files with 1,648 additions and 78 deletions.
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/aigunov/java-explore-with-me/pull/5
2 changes: 1 addition & 1 deletion main-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM amazoncorretto:21
FROM openjdk:21
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import ru.practicum.mainservice.data.model.EntityCreationDataIncorrect;
import ru.practicum.mainservice.data.model.EntityUpdateConflict;
import ru.practicum.mainservice.data.model.NoValidParameter;
import ru.practicum.mainservice.data.model.exceptions.CommentActionException;
import ru.practicum.mainservice.data.model.exceptions.EntityCreationDataIncorrect;
import ru.practicum.mainservice.data.model.exceptions.EntityUpdateConflict;
import ru.practicum.mainservice.data.model.exceptions.NoValidParameter;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -58,7 +59,7 @@ public Map<String, String> handleMissingURLParameter(MissingServletRequestParame

@ExceptionHandler(NoSuchElementException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Map<String, String> handleNotFoundEntity(RuntimeException e) {
public Map<String, String> handleNotFoundEntity(NoSuchElementException e) {
log.error("Получен статус 404 NOT_FOUND {}", e.getMessage());
return Map.of("Ошибка: ", e.getMessage());
}
Expand All @@ -77,6 +78,13 @@ public Map<String, String> handleConflict(final EntityCreationDataIncorrect e) {
return Map.of("Ошибка", e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.CONFLICT)
public Map<String, String> handleCommentException(final CommentActionException e) {
log.error("Получен Статус 409 CONFLIC {}", e.getMessage());
return Map.of("Ошибка", e.getMessage());
}

@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public Map<String, String> handleConstraintViolationException(final DataIntegrityViolationException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.practicum.mainservice.controller.adminapi;

import jakarta.validation.Valid;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.mainservice.data.dto.comment.CommentDto;
import ru.practicum.mainservice.data.dto.comment.CommentStatusUpdateRequest;
import ru.practicum.mainservice.data.dto.comment.CommentStatusUpdateResult;
import ru.practicum.mainservice.service.interfaces.CommentService;

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

@GetMapping("/{commentId}")
public CommentDto getCommentById(@PositiveOrZero @PathVariable Long commentId) {
CommentDto commentById = commentService.findCommentById(commentId);
log.info("GET /admin/comments/{commentId} commentId = {} result = {}", commentById, commentById);
return commentById;
}

@PatchMapping("/event/{eventId}")
@ResponseStatus(HttpStatus.CREATED)
public CommentStatusUpdateResult decisionComments(@PositiveOrZero @PathVariable Long eventId,
@Valid @RequestBody CommentStatusUpdateRequest commentStatusUpdateRequest) {
CommentStatusUpdateResult decision = commentService.decisionComments(eventId, commentStatusUpdateRequest);
log.info("PATCH /admin/comments/event/{eventId}eventId = {} patchedComment = {}", eventId, decision);
return decision;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.mainservice.data.dto.NewCompilationDto;
import ru.practicum.mainservice.data.dto.UpdateCompilationRequest;
import ru.practicum.mainservice.data.dto.compilation.NewCompilationDto;
import ru.practicum.mainservice.data.dto.compilation.UpdateCompilationRequest;
import ru.practicum.mainservice.data.model.Compilations;
import ru.practicum.mainservice.service.interfaces.CompilationsService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.mainservice.data.dto.EventResponseDto;
import ru.practicum.mainservice.data.dto.UpdateEventAdminRequest;
import ru.practicum.mainservice.data.dto.event.EventResponseDto;
import ru.practicum.mainservice.data.dto.event.UpdateEventAdminRequest;
import ru.practicum.mainservice.data.model.RequestParameters;
import ru.practicum.mainservice.data.model.States;
import ru.practicum.mainservice.data.model.enums.States;
import ru.practicum.mainservice.service.interfaces.EventService;
import ru.practicum.mainservice.stat.StatAdapter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.mainservice.data.dto.NewUserRequest;
import ru.practicum.mainservice.data.dto.user.NewUserRequest;
import ru.practicum.mainservice.service.interfaces.UserService;
import ru.practicum.statsdto.dto.UserDto;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.practicum.mainservice.controller.privateapi;

import jakarta.validation.Valid;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.mainservice.data.dto.comment.CommentDto;
import ru.practicum.mainservice.service.interfaces.CommentService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users/{userId}/event/{eventId}/comments")
@RequiredArgsConstructor
public class PrivateCommentsController {
private final CommentService commentService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CommentDto createComment(@PositiveOrZero @PathVariable Long userId,
@PositiveOrZero @PathVariable Long eventId,
@Valid @RequestBody CommentDto comment) {
CommentDto createdComment = commentService.createComment(userId, eventId, comment);
log.info("POST /users/{userId}/event/{eventId}/comments userId = {} eventId = {} createdComment = {}", userId,
eventId, createdComment);
return createdComment;
}

@DeleteMapping("/{commentId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteComment(@PositiveOrZero @PathVariable Long userId,
@PositiveOrZero @PathVariable Long eventId,
@PositiveOrZero @PathVariable Long commentId) {
log.info("DELETE /users/{userId}/event/{eventId}/comments/{commentId} userId = {}," +
" eventId = {}, commentId = {}", userId, eventId, commentId);
commentService.deleteComment(userId, eventId, commentId);
}

@GetMapping
public List<CommentDto> getCommentsByEventId(@PositiveOrZero @PathVariable Long userId,
@PositiveOrZero @PathVariable Long eventId,
@PositiveOrZero @RequestParam(defaultValue = "0") Integer from,
@PositiveOrZero @RequestParam(defaultValue = "0") Integer size) {
List<CommentDto> commentsByEventId = commentService.getCommentsByEventId(userId, eventId, PageRequest.of(from / size, size));
log.info("GET /users/{userId}/event/{eventId}/comments userId = {} eventId = {} result = {}", userId,
eventId, commentsByEventId);
return commentsByEventId;
}

@GetMapping("/{commentId}")
public CommentDto findComment(@PositiveOrZero @PathVariable Long userId,
@PositiveOrZero @PathVariable Long eventId,
@PositiveOrZero @PathVariable Long commentId) {
log.info("GET /users/{userId}/event/{eventId}/comments/{commentId}" +
" userId = {} eventId = {} commentId = {}", userId, eventId, commentId);
CommentDto commentByIdAndEventId = commentService.getCommentByIdAndEventId(userId, eventId, commentId);
log.info("Найденный комментарий {}", commentByIdAndEventId);
return commentByIdAndEventId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.mainservice.data.dto.*;
import ru.practicum.mainservice.data.dto.RequestDto;
import ru.practicum.mainservice.data.dto.event.*;
import ru.practicum.mainservice.service.interfaces.EventService;
import ru.practicum.mainservice.service.interfaces.RequestService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.mainservice.data.dto.EventResponseDto;
import ru.practicum.mainservice.data.dto.event.EventResponseDto;
import ru.practicum.mainservice.data.model.RequestParameters;
import ru.practicum.mainservice.data.model.Sort;
import ru.practicum.mainservice.data.model.enums.Sort;
import ru.practicum.mainservice.service.interfaces.EventService;
import ru.practicum.mainservice.stat.StatAdapter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import ru.practicum.mainservice.data.model.Status;
import ru.practicum.mainservice.data.model.enums.Status;

import java.time.LocalDateTime;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.practicum.mainservice.data.dto.comment;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotBlank;
import lombok.*;
import ru.practicum.mainservice.data.model.enums.CommentStatus;
import ru.practicum.statsdto.dto.UserDto;

import java.time.LocalDateTime;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
public class CommentDto {
private Long id;
private Long eventId;
private String comment;
private UserDto user;
@NotBlank
private String description;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime publishedOn;
private CommentStatus status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.practicum.mainservice.data.dto.comment;

import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommentStatusUpdateRequest {
@NotEmpty
private Set<Long> ids;
@NotNull
private Status status;

public enum Status {
APPROVE,
REJECT
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.practicum.mainservice.data.dto.comment;

import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder(toBuilder = true)
public class CommentStatusUpdateResult {
private List<CommentDto> resolvedComments;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.compilation;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import ru.practicum.mainservice.data.dto.event.EventShortDto;

import java.util.Set;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.compilation;

import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.constraints.NotBlank;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.compilation;

import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

public enum EventRequestStatus {
CONFIRMED, REJECTED;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

import lombok.Builder;
import lombok.Data;
import ru.practicum.mainservice.data.dto.RequestDto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.practicum.mainservice.data.model.States;
import ru.practicum.mainservice.data.model.enums.States;
import ru.practicum.statsdto.dto.CategoryDto;
import ru.practicum.statsdto.dto.UserDto;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.practicum.mainservice.data.model.States;
import ru.practicum.mainservice.data.dto.user.UserShortDto;
import ru.practicum.mainservice.data.model.enums.States;
import ru.practicum.statsdto.dto.CategoryDto;

import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.Valid;
Expand All @@ -7,7 +7,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.practicum.mainservice.data.model.NoValidParameter;
import ru.practicum.mainservice.data.model.exceptions.NoValidParameter;

import java.time.LocalDateTime;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.Valid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.practicum.mainservice.data.dto;
package ru.practicum.mainservice.data.dto.event;

import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down
Loading

0 comments on commit 195b46b

Please sign in to comment.