Added Post NEW and GET API
This commit is contained in:
1 parent
94931f1638
commit
dc4982459b
7 files changed
+263
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package site.easystartup.web.post.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import site.easystartup.web.domain.model.User;
|
||||
import site.easystartup.web.domain.response.MessageResponse;
|
||||
import site.easystartup.web.domain.validation.ResponseErrorValidation;
|
||||
import site.easystartup.web.post.domain.model.Post;
|
||||
import site.easystartup.web.post.domain.request.PostRequest;
|
||||
import site.easystartup.web.post.dto.PostDto;
|
||||
import site.easystartup.web.post.service.PostService;
|
||||
import site.easystartup.web.project.domain.requst.ProjectRequest;
|
||||
import site.easystartup.web.service.UserService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/post")
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class PostController {
|
||||
private final PostService postService;
|
||||
private final UserService userService;
|
||||
private final ModelMapper modelMapper;
|
||||
private final ResponseErrorValidation responseErrorValidation;
|
||||
|
||||
@GetMapping("/")
|
||||
public ResponseEntity<List<PostDto>> main(Principal principal) {
|
||||
var posts = postService.getAllPostsByAuthor(principal.getName())
|
||||
.stream().map(post -> modelMapper.map(post, PostDto.class)).toList();
|
||||
return ResponseEntity.ok().body(posts);
|
||||
}
|
||||
|
||||
@GetMapping("/new")
|
||||
public ResponseEntity<Object> createPost(@Valid @RequestBody PostRequest postRequest, BindingResult bindingResult,
|
||||
Principal principal) {
|
||||
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||
|
||||
Post post = postService.createPost(postRequest, principal);
|
||||
return ResponseEntity.ok().body(modelMapper.map(post, PostRequest.class));
|
||||
}
|
||||
|
||||
@GetMapping("/{username}")
|
||||
public ResponseEntity<List<PostDto>> user(@PathVariable String username) {
|
||||
var posts = postService.getAllPostsByAuthor(username)
|
||||
.stream().map(post -> modelMapper.map(post, PostDto.class)).toList();
|
||||
return ResponseEntity.ok().body(posts);
|
||||
}
|
||||
|
||||
/*
|
||||
@PutMapping("/message/{message_id}/edit")
|
||||
public ResponseEntity<Object> messageEdit(@Valid @RequestBody PostRequest messageRequest,
|
||||
@PathVariable long message_id,
|
||||
BindingResult bindingResult,
|
||||
Principal principal) {
|
||||
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||
|
||||
var messageUpdated = forumService.editDiscussionMessage(messageRequest, message_id, principal);
|
||||
return ResponseEntity.ok().body(modelMapper.map(messageUpdated, PostDto.class));
|
||||
}
|
||||
|
||||
@DeleteMapping("/message/{message_id}/delete")
|
||||
public ResponseEntity<Object> messageDelete(@PathVariable long message_id, Principal principal) {
|
||||
Post message = forumService.getDiscussionMessageById(message_id);
|
||||
User user = userService.getUserByPrincipal(principal);
|
||||
if(!(message.getSender().equals(user.getUsername()) && user.isModer()))
|
||||
return ResponseEntity.ok(new MessageResponse("No permissions!"));
|
||||
forumService.deleteDiscussionMessage(message_id, user);
|
||||
return ResponseEntity.ok(new MessageResponse("Message was deleted!"));
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package site.easystartup.web.post.domain.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public class PostNotFoundException extends RuntimeException implements Supplier<PostNotFoundException> {
|
||||
private String message;
|
||||
|
||||
public PostNotFoundException(String message) {
|
||||
super(message);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostNotFoundException get() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package site.easystartup.web.post.domain.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.Hibernate;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@RequiredArgsConstructor
|
||||
@Entity
|
||||
public class Post implements Comparable<Post>{
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "post_id")
|
||||
private long id;
|
||||
@Column(columnDefinition="varchar(1024)")
|
||||
private String text;
|
||||
private String sender;
|
||||
private long like;
|
||||
|
||||
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
|
||||
@CollectionTable(name = "post_answers", joinColumns = @JoinColumn(name = "post_id"))
|
||||
private List<Long> answers;
|
||||
private Long answered_post;
|
||||
|
||||
@Column(name = "date", updatable = false)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date date;
|
||||
|
||||
private boolean edited;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
|
||||
Post that = (Post) o;
|
||||
return Objects.equals(id, that.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getClass().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Post o) {
|
||||
if (getDate() == null || o.getDate() == null)
|
||||
return 0;
|
||||
return getDate().compareTo(o.getDate());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package site.easystartup.web.post.domain.request;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class PostRequest {
|
||||
@Size(min = 3, max = 1024, message = "Text should be no less 3 and no more 1024 signs")
|
||||
@NotNull
|
||||
private String text;
|
||||
private String sender;
|
||||
private Date date;
|
||||
private boolean edited;
|
||||
private long discussion_id;
|
||||
private long like;
|
||||
private List<Long> answers;
|
||||
private Long answered_post;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package site.easystartup.web.post.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PostDto implements Serializable {
|
||||
private long id;
|
||||
@Size(min = 3, max = 1024, message = "Text should be no less 3 and no more 1024 signs")
|
||||
@NotNull
|
||||
private String text;
|
||||
private String sender;
|
||||
private Date date;
|
||||
private boolean edited;
|
||||
private long discussion_id;
|
||||
private long like;
|
||||
private List<Long> answers;
|
||||
private long answered_post;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package site.easystartup.web.post.repo;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import site.easystartup.web.post.domain.model.Post;
|
||||
|
||||
public interface PostRepo extends JpaRepository<Post, Long> {
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package site.easystartup.web.post.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import site.easystartup.web.post.domain.exception.PostNotFoundException;
|
||||
import site.easystartup.web.post.domain.model.Post;
|
||||
import site.easystartup.web.post.domain.request.PostRequest;
|
||||
import site.easystartup.web.post.repo.PostRepo;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class PostService {
|
||||
private final PostRepo postRepo;
|
||||
|
||||
public Post getPostById(long id) {
|
||||
return postRepo.findById(id).orElseThrow(new PostNotFoundException("Post with id " + id + " dont exists!"));
|
||||
}
|
||||
|
||||
public List<Post> getAllPostsByAuthor(String username) {
|
||||
return postRepo.findAll().stream().filter(post -> post.getSender().equals(username)).toList();
|
||||
}
|
||||
|
||||
public Post createPost(PostRequest postRequest, Principal principal) {
|
||||
Post post = new Post();
|
||||
post.setAnswered_post(post.getAnswered_post());
|
||||
post.setAnswers(post.getAnswers());
|
||||
post.setDate(new Date());
|
||||
post.setEdited(false);
|
||||
post.setLike(0);
|
||||
post.setSender(principal.getName());
|
||||
post.setText(postRequest.getText());
|
||||
return postRepo.save(post);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user