4 lab начало неизбежного....
This commit is contained in:
parent
55b8db56c7
commit
33f6f1dd21
@ -0,0 +1,31 @@
|
||||
package ru.ulstu.is.sbapp.Comment.controller;
|
||||
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
public class CommentDto {
|
||||
private Long id;
|
||||
private String Text;
|
||||
private Post post;
|
||||
private User user;
|
||||
public CommentDto(String text)
|
||||
{
|
||||
this.Text=text;
|
||||
}
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
public String getText() {return Text;}
|
||||
|
||||
public User getUser()
|
||||
{
|
||||
return user;
|
||||
}
|
||||
|
||||
public Post getPost()
|
||||
{
|
||||
return post;
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package ru.ulstu.is.sbapp.Comment.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
@ -11,7 +12,7 @@ public class Comment {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@NotBlank(message = "Text cannot be null")
|
||||
private String Text;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
|
@ -0,0 +1,7 @@
|
||||
package ru.ulstu.is.sbapp.Comment.service;
|
||||
|
||||
public class CommentNotFoundException extends RuntimeException{
|
||||
public CommentNotFoundException(Long id) {
|
||||
super(String.format("Comment with id [%s] is not found", id));
|
||||
}
|
||||
}
|
46
src/main/java/ru/ulstu/is/sbapp/Post/controller/PostDto.java
Normal file
46
src/main/java/ru/ulstu/is/sbapp/Post/controller/PostDto.java
Normal file
@ -0,0 +1,46 @@
|
||||
package ru.ulstu.is.sbapp.Post.controller;
|
||||
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PostDto {
|
||||
private Long id;
|
||||
|
||||
private String Heading;
|
||||
|
||||
private String Content;
|
||||
|
||||
private User user;
|
||||
|
||||
private List<Comment> comments = new ArrayList<>();
|
||||
|
||||
public PostDto(String Heading, String Content)
|
||||
{
|
||||
this.Heading = Heading;
|
||||
this.Content = Content;
|
||||
}
|
||||
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
public String getHeading()
|
||||
{
|
||||
return Heading;
|
||||
}
|
||||
public String getContent()
|
||||
{
|
||||
return Content;
|
||||
}
|
||||
public User getUser()
|
||||
{
|
||||
return user;
|
||||
}
|
||||
public List<Comment> getComments()
|
||||
{
|
||||
return comments;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package ru.ulstu.is.sbapp.Post.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
@ -15,8 +16,9 @@ public class Post {
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
@NotBlank(message = "Heading cannot be null")
|
||||
private String Heading;
|
||||
|
||||
@NotBlank(message = "Content cannot be null")
|
||||
private String Content;
|
||||
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
package ru.ulstu.is.sbapp.Post.service;
|
||||
|
||||
public class PostNotFoundException extends RuntimeException{
|
||||
public PostNotFoundException(Long id) {
|
||||
super(String.format("Post with id [%s] is not found", id));
|
||||
}
|
||||
}
|
52
src/main/java/ru/ulstu/is/sbapp/User/controller/UserDto.java
Normal file
52
src/main/java/ru/ulstu/is/sbapp/User/controller/UserDto.java
Normal file
@ -0,0 +1,52 @@
|
||||
package ru.ulstu.is.sbapp.User.controller;
|
||||
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String email;
|
||||
|
||||
private List<Post> posts = new ArrayList<>();
|
||||
|
||||
private List<Comment> comments = new ArrayList<>();
|
||||
|
||||
public UserDto(String firstName, String lastName, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email=email;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
public List<Post> getPosts()
|
||||
{
|
||||
return posts;
|
||||
}
|
||||
public List<Comment> getComments()
|
||||
{
|
||||
return comments;
|
||||
}
|
||||
|
||||
|
||||
public String getEmail()
|
||||
{
|
||||
return email;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package ru.ulstu.is.sbapp.User.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
|
||||
@ -15,9 +16,12 @@ public class User {
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
@NotBlank(message = "firstName cannot be null")
|
||||
private String firstName;
|
||||
@NotBlank(message = "lastName cannot be null")
|
||||
private String lastName;
|
||||
|
||||
@NotBlank(message = "email cannot be null")
|
||||
private String email;
|
||||
|
||||
@OneToMany(mappedBy ="user",cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
|
||||
|
@ -0,0 +1,7 @@
|
||||
package ru.ulstu.is.sbapp.User.service;
|
||||
|
||||
public class UserNotFoundException extends RuntimeException{
|
||||
public UserNotFoundException(Long id) {
|
||||
super(String.format("User with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package ru.ulstu.is.sbapp.Util.error;
|
||||
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import ru.ulstu.is.sbapp.Comment.service.CommentNotFoundException;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostNotFoundException;
|
||||
import ru.ulstu.is.sbapp.User.service.UserNotFoundException;
|
||||
import ru.ulstu.is.sbapp.Util.validation.ValidationException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ControllerAdvice
|
||||
public class AdviceController {
|
||||
@ExceptionHandler({
|
||||
CommentNotFoundException.class,
|
||||
UserNotFoundException.class,
|
||||
PostNotFoundException.class,
|
||||
ValidationException.class
|
||||
})
|
||||
public ResponseEntity<Object> handleException(Throwable e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
||||
final ValidationException validationException = new ValidationException(
|
||||
e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
return handleException(validationException);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package ru.ulstu.is.sbapp.Util.validation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(Set<String> errors) {
|
||||
super(String.join("\n", errors));
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package ru.ulstu.is.sbapp.Util.validation;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.ValidatorFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ValidatorUtil {
|
||||
private final Validator validator;
|
||||
|
||||
public ValidatorUtil() {
|
||||
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
||||
this.validator = factory.getValidator();
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void validate(T object) {
|
||||
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||
if (!errors.isEmpty()) {
|
||||
throw new ValidationException(errors.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user