Added Post system
This commit is contained in:
1 parent
d68b7de2d8
commit
5e5871b78c
7 files changed
+97
-21
No files matched your search
@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import site.easystartup.web.domain.model.User;
|
import site.easystartup.web.domain.model.User;
|
||||||
import site.easystartup.web.domain.response.MessageResponse;
|
import site.easystartup.web.domain.response.MessageResponse;
|
||||||
import site.easystartup.web.domain.validation.ResponseErrorValidation;
|
import site.easystartup.web.domain.validation.ResponseErrorValidation;
|
||||||
|
import site.easystartup.web.exception.NoPermissionException;
|
||||||
import site.easystartup.web.post.domain.model.Post;
|
import site.easystartup.web.post.domain.model.Post;
|
||||||
import site.easystartup.web.post.domain.request.PostRequest;
|
import site.easystartup.web.post.domain.request.PostRequest;
|
||||||
import site.easystartup.web.post.dto.PostDto;
|
import site.easystartup.web.post.dto.PostDto;
|
||||||
@@ -39,7 +40,7 @@ public class PostController {
|
|||||||
return ResponseEntity.ok().body(posts);
|
return ResponseEntity.ok().body(posts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/new")
|
@PostMapping("/new")
|
||||||
public ResponseEntity<Object> createPost(@Valid @RequestBody PostRequest postRequest, BindingResult bindingResult,
|
public ResponseEntity<Object> createPost(@Valid @RequestBody PostRequest postRequest, BindingResult bindingResult,
|
||||||
Principal principal) {
|
Principal principal) {
|
||||||
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||||
@@ -49,37 +50,47 @@ public class PostController {
|
|||||||
return ResponseEntity.ok().body(modelMapper.map(post, PostRequest.class));
|
return ResponseEntity.ok().body(modelMapper.map(post, PostRequest.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{username}")
|
@GetMapping("/user/{username}")
|
||||||
public ResponseEntity<List<PostDto>> user(@PathVariable String username) {
|
public ResponseEntity<List<PostDto>> user(@PathVariable String username) {
|
||||||
var posts = postService.getAllPostsByAuthor(username)
|
var posts = postService.getAllPostsByAuthor(username)
|
||||||
.stream().map(post -> modelMapper.map(post, PostDto.class)).toList();
|
.stream().map(post -> modelMapper.map(post, PostDto.class)).toList();
|
||||||
return ResponseEntity.ok().body(posts);
|
return ResponseEntity.ok().body(posts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
@GetMapping("/{post_id}/edit/")
|
||||||
@PutMapping("/message/{message_id}/edit")
|
public ResponseEntity<Object> edit(@Valid @RequestBody PostRequest postRequest, BindingResult bindingResult,
|
||||||
public ResponseEntity<Object> messageEdit(@Valid @RequestBody PostRequest messageRequest,
|
Principal principal, @PathVariable long post_id) {
|
||||||
@PathVariable long message_id,
|
|
||||||
BindingResult bindingResult,
|
|
||||||
Principal principal) {
|
|
||||||
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
var errors = responseErrorValidation.mapValidationService(bindingResult);
|
||||||
if (!ObjectUtils.isEmpty(errors)) return errors;
|
if (!ObjectUtils.isEmpty(errors)) return errors;
|
||||||
|
|
||||||
var messageUpdated = forumService.editDiscussionMessage(messageRequest, message_id, principal);
|
Post post = postService.edit(postRequest, post_id, principal);
|
||||||
return ResponseEntity.ok().body(modelMapper.map(messageUpdated, PostDto.class));
|
return ResponseEntity.ok().body(modelMapper.map(post, PostRequest.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/message/{message_id}/delete")
|
@DeleteMapping("/{post_id}/delete")
|
||||||
public ResponseEntity<Object> messageDelete(@PathVariable long message_id, Principal principal) {
|
public void delete(@PathVariable long post_id,
|
||||||
Post message = forumService.getDiscussionMessageById(message_id);
|
Principal principal) {
|
||||||
User user = userService.getUserByPrincipal(principal);
|
Post post = postService.getPostById(post_id);
|
||||||
if(!(message.getSender().equals(user.getUsername()) && user.isModer()))
|
if(!post.getSender().equals(principal.getName()) && !userService.getUserByPrincipal(principal).isAdmin())
|
||||||
return ResponseEntity.ok(new MessageResponse("No permissions!"));
|
throw new NoPermissionException(String.format("User %s cant delete post %s", principal.getName(), post_id));
|
||||||
forumService.deleteDiscussionMessage(message_id, user);
|
else postService.delete(post);
|
||||||
return ResponseEntity.ok(new MessageResponse("Message was deleted!"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
@PutMapping("/{post_id}/like/")
|
||||||
|
public ResponseEntity<Object> like(Principal principal, @PathVariable long post_id) {
|
||||||
|
Post post = postService.getPostById(post_id);
|
||||||
|
|
||||||
|
Post edited = postService.addLike(post, principal);
|
||||||
|
return ResponseEntity.ok().body(modelMapper.map(edited, PostRequest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{post_id}/dislike/")
|
||||||
|
public ResponseEntity<Object> dislike(Principal principal, @PathVariable long post_id) {
|
||||||
|
Post post = postService.getPostById(post_id);
|
||||||
|
|
||||||
|
Post edited = postService.disLike(post, principal);
|
||||||
|
return ResponseEntity.ok().body(modelMapper.map(edited, PostRequest.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package site.easystartup.web.post.domain.exception;
|
||||||
|
|
||||||
|
public class AlreadyLikedException extends RuntimeException {
|
||||||
|
public AlreadyLikedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package site.easystartup.web.post.domain.exception;
|
||||||
|
|
||||||
|
public class DontLickedException extends RuntimeException{
|
||||||
|
public DontLickedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,11 @@ public class Post implements Comparable<Post>{
|
|||||||
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
|
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
|
||||||
@CollectionTable(name = "post_answers", joinColumns = @JoinColumn(name = "post_id"))
|
@CollectionTable(name = "post_answers", joinColumns = @JoinColumn(name = "post_id"))
|
||||||
private List<Long> answers;
|
private List<Long> answers;
|
||||||
|
|
||||||
|
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
|
||||||
|
@CollectionTable(name = "post_licked_users", joinColumns = @JoinColumn(name = "post_id"))
|
||||||
|
private List<String> liked_users;
|
||||||
|
|
||||||
private Long answered_post;
|
private Long answered_post;
|
||||||
|
|
||||||
@Column(name = "date", updatable = false)
|
@Column(name = "date", updatable = false)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class PostRequest {
|
|||||||
private String sender;
|
private String sender;
|
||||||
private Date date;
|
private Date date;
|
||||||
private boolean edited;
|
private boolean edited;
|
||||||
private long discussion_id;
|
private List<String> liked_users;
|
||||||
private long like;
|
private long like;
|
||||||
private List<Long> answers;
|
private List<Long> answers;
|
||||||
private Long answered_post;
|
private Long answered_post;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class PostDto implements Serializable {
|
|||||||
private String sender;
|
private String sender;
|
||||||
private Date date;
|
private Date date;
|
||||||
private boolean edited;
|
private boolean edited;
|
||||||
private long discussion_id;
|
private List<String> liked_users;
|
||||||
private long like;
|
private long like;
|
||||||
private List<Long> answers;
|
private List<Long> answers;
|
||||||
private long answered_post;
|
private long answered_post;
|
||||||
|
|||||||
@@ -3,12 +3,14 @@ package site.easystartup.web.post.service;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import site.easystartup.web.post.domain.exception.AlreadyLikedException;
|
||||||
import site.easystartup.web.post.domain.exception.PostNotFoundException;
|
import site.easystartup.web.post.domain.exception.PostNotFoundException;
|
||||||
import site.easystartup.web.post.domain.model.Post;
|
import site.easystartup.web.post.domain.model.Post;
|
||||||
import site.easystartup.web.post.domain.request.PostRequest;
|
import site.easystartup.web.post.domain.request.PostRequest;
|
||||||
import site.easystartup.web.post.repo.PostRepo;
|
import site.easystartup.web.post.repo.PostRepo;
|
||||||
|
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -34,6 +36,50 @@ public class PostService {
|
|||||||
post.setLike(0);
|
post.setLike(0);
|
||||||
post.setSender(principal.getName());
|
post.setSender(principal.getName());
|
||||||
post.setText(postRequest.getText());
|
post.setText(postRequest.getText());
|
||||||
|
checkAnswers(post);
|
||||||
|
return postRepo.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post edit(PostRequest postRequest, long post_id, Principal principal) {
|
||||||
|
Post post = postRepo.findById(post_id).orElseThrow(
|
||||||
|
new PostNotFoundException("Post with id " + post_id + " didnt exists"));
|
||||||
|
post.setEdited(true);
|
||||||
|
post.setSender(principal.getName());
|
||||||
|
post.setText(postRequest.getText());
|
||||||
|
checkAnswers(post);
|
||||||
|
return postRepo.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Post post) {
|
||||||
|
postRepo.delete(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkAnswers(Post post) {
|
||||||
|
if(post.getAnswered_post() != null) {
|
||||||
|
Post answered = postRepo.findById(post.getAnswered_post()).orElseThrow(
|
||||||
|
new PostNotFoundException("Post didnt exists: " + post.getAnswered_post()));
|
||||||
|
List<Long> answers = answered.getAnswers() != null ? answered.getAnswers() : new ArrayList<>();
|
||||||
|
answers.add(post.getId());
|
||||||
|
answered.setAnswers(answers);
|
||||||
|
postRepo.save(answered);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post addLike(Post post, Principal principal) {
|
||||||
|
if (post.getLiked_users().contains(principal.getName()))
|
||||||
|
throw new AlreadyLikedException(String.format("User %s already liked post %s", principal.getName(), post.getId()));
|
||||||
|
post.setLike(post.getLike() + 1);
|
||||||
|
List<String> liked = post.getLiked_users() == null ? new ArrayList<>() : post.getLiked_users();
|
||||||
|
liked.add(principal.getName());
|
||||||
|
return postRepo.save(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post disLike(Post post, Principal principal) {
|
||||||
|
if (!post.getLiked_users().contains(principal.getName()))
|
||||||
|
throw new AlreadyLikedException(String.format("User %s already liked post %s", principal.getName(), post.getId()));
|
||||||
|
post.setLike(post.getLike() - 1);
|
||||||
|
List<String> liked = post.getLiked_users() == null ? new ArrayList<>() : post.getLiked_users();
|
||||||
|
liked.remove(principal.getName());
|
||||||
return postRepo.save(post);
|
return postRepo.save(post);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user