Commit 1c6e5b2a authored by Mamadou Diallo's avatar Mamadou Diallo
Browse files

Comment CRUD + few upgrades

parent 2a277903
......@@ -29,7 +29,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
User user = new User("Diallo", "Mamadou", "greer", "Greer", Const.ADMIN);
auth.inMemoryAuthentication().withUser(user.getUserName())
.password(PasswordEncoderHelper.passwordEncoder().encode(user.getPassword()))
.roles(user.getRoles());
.roles(user.getRole());
}
@Override
......@@ -39,9 +39,9 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
.authorizeRequests()
.antMatchers("/api/admin/**").hasRole(Const.ADMIN)
.antMatchers("/api/home").permitAll()
.antMatchers("/api/user/add").permitAll()
.antMatchers("/api/user/**").permitAll()
.antMatchers("/api/post/**").permitAll()
.antMatchers("/api/comment/**").permitAll()
.antMatchers("/api/login*").permitAll()
.anyRequest().authenticated();
......
......@@ -2,13 +2,21 @@ package com.example.server.controller;
import com.example.server.exceptions.CommentNotFoundException;
import com.example.server.model.Comment;
import com.example.server.model.Post;
import com.example.server.model.User;
import com.example.server.repository.CommentRepository;
import com.example.server.repository.PostRepository;
import com.example.server.repository.UserRepository;
import com.example.server.utils.DateGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
......@@ -18,41 +26,88 @@ import java.util.Optional;
public class CommentController {
private final CommentRepository repository;
private final UserRepository userRepository;
private final PostRepository postRepository;
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add")
public Comment add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
@PostMapping("/add/{postId}/{userid}")
public ResponseEntity<Comment> add(@RequestBody JsonNode jsonNode, @PathVariable String postId,
@PathVariable String userid) throws JsonProcessingException {
User author = userRepository.getUserById(userid);
Post post = postRepository.getPostById(postId);
Comment comment = mapper.treeToValue(jsonNode, Comment.class);
//System.out.println("comment: "+comment.toString());
return comment;
comment.setDate(DateGenerator.generateDate());
comment.setAuthor(author);
comment.setReply(new ArrayList<>());
post.addComment(repository.save(comment));
postRepository.deleteById(postId);
postRepository.save(post);
return new ResponseEntity<>(comment, HttpStatus.CREATED);
}
@PutMapping("/{id}")
public Comment update(@RequestBody JsonNode jsonNode, @PathVariable String id) {
if(!repository.existsById(id)){
throw new CommentNotFoundException(id);
@PutMapping("/{postId}/{commentId}")
public ResponseEntity<Comment> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) {
if(!repository.existsById(commentId)){
throw new CommentNotFoundException(commentId);
}
repository.deleteById(id);
//return userRepository.save(newUser);
Comment comment = repository.getCommentById(commentId);
Post post = postRepository.getPostById(postId);
post.deleteComment(comment);
postRepository.save(post);
postRepository.deleteById(postId);
repository.deleteById(commentId);
/**
* not ended yet
*/
return null;
}
@GetMapping("/{id}")
public Optional<Comment> getById(@PathVariable String id) {
public ResponseEntity<Optional<Comment>> getById(@PathVariable String id) {
if(!repository.existsById(id)) throw new CommentNotFoundException(id);
return repository.findById(id);
return new ResponseEntity<>(repository.findById(id), HttpStatus.OK);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable String id) {
if(!repository.existsById(id)){
throw new CommentNotFoundException(id);
@DeleteMapping("/{postId}/{commentId}")
public ResponseEntity<String> deleteById(@PathVariable String postId, @PathVariable String commentId) {
if(!repository.existsById(commentId)){
throw new CommentNotFoundException(commentId);
}
repository.deleteById(id);
Post post = postRepository.getPostById(postId);
Comment comment = repository.getCommentById(commentId);
post.deleteComment(comment);
repository.deleteById(commentId);
postRepository.deleteById(postId);
postRepository.save(post);
return ResponseEntity.ok("Comment deleted successfully !");
}
@GetMapping("/all")
public List<Comment> getAll() {
return repository.findAll();
public ResponseEntity<List<Comment>> getAll() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
}
@DeleteMapping("/all")
public ResponseEntity<?> deleteAll(){
if(repository.count() > 0){
repository.deleteAll();
return new ResponseEntity<>("All comments deleted successfully", HttpStatus.ACCEPTED);
}
return new ResponseEntity<>("Comment repository is empty", HttpStatus.NOT_FOUND);
}
}
......@@ -14,6 +14,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
......@@ -34,7 +35,7 @@ public class PostController {
Post post = mapper.convertValue(jsonNode, Post.class);
post.setAuthor(author);
post.setDate(DateGenerator.generateDate());
post.setComments(new ArrayList<>());
repository.save(post);
return new ResponseEntity<>(post, HttpStatus.CREATED);
......@@ -55,7 +56,7 @@ public class PostController {
repository.deleteById(id);
return new ResponseEntity<>(repository.save(newPost), HttpStatus.CREATED);
return new ResponseEntity<>(repository.save(newPost), HttpStatus.ACCEPTED);
}
@GetMapping("/{id}")
......@@ -70,19 +71,19 @@ public class PostController {
}
repository.deleteById(id);
return new ResponseEntity<>("Post deleted successfully", HttpStatus.OK);
return new ResponseEntity<>("Post deleted successfully", HttpStatus.ACCEPTED);
}
@DeleteMapping("/all")
public ResponseEntity<?> deleteAll(){
if(repository.count() > 0){
repository.deleteAll();
return new ResponseEntity<>("All posts deleted successfully", HttpStatus.OK);
return new ResponseEntity<>("All posts deleted successfully", HttpStatus.ACCEPTED);
}
return new ResponseEntity<>("Post repository is empty", HttpStatus.NOT_FOUND);
}
@GetMapping("/all")
public List<Post> getAll() {
return repository.findAll();
public ResponseEntity<List<Post>> getAll() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
}
}
......@@ -23,10 +23,9 @@ public class UserController {
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add")
public User add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
public ResponseEntity<User> add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
User user = mapper.treeToValue(jsonNode, User.class);
userRepository.save(user);
return user;
return new ResponseEntity<>(userRepository.save(user), HttpStatus.CREATED);
}
@PutMapping("/{id}")
......@@ -56,8 +55,8 @@ public class UserController {
}
@GetMapping("/all")
public List<User> getAll() {
return userRepository.findAll();
public ResponseEntity<List<User>> getAll() {
return new ResponseEntity<>(userRepository.findAll(), HttpStatus.OK);
}
@DeleteMapping("/all")
......
......@@ -3,6 +3,7 @@ package com.example.server.model;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.ArrayList;
import java.util.List;
......@@ -18,13 +19,18 @@ public class Comment{
@Id
private String id;
@Field("message")
private String message;
@Field("author")
private User author;
@Field("date")
private String date;
@Field("reply")
private List<Comment> reply;
public Comment(User author, String message) {
this.author = author;
public Comment(String message) {
this.message = message;
this.author = null;
this.reply = new ArrayList<>();
}
......@@ -40,6 +46,14 @@ public class Comment{
return author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public List<Comment> getReply() {
return reply;
}
......@@ -52,6 +66,10 @@ public class Comment{
this.author = author;
}
public void addReply(Comment comment){
this.reply.add(comment);
}
@Override
public String toString() {
return "Comment{" +
......
......@@ -10,11 +10,11 @@ public interface IUser {
String getLastName();
String getUserName();
String getPassword();
String getRoles();
String getRole();
void setFirstName(String firstName);
void setLastName(String lastName);
void setUserName(String userName);
void setPassword(String password);
void setRoles(String role);
void setRole(String role);
}
......@@ -80,6 +80,14 @@ public class Post{
return comments;
}
public void addComment(Comment comment){
this.comments.add(comment);
}
public void deleteComment(Comment comment){
this.comments.remove(comment);
}
@Override
public String toString() {
return "Post{" +
......
......@@ -65,7 +65,7 @@ public class User implements IUser{
}
@Override
public String getRoles() {
public String getRole() {
return role;
}
......@@ -90,7 +90,7 @@ public class User implements IUser{
}
@Override
public void setRoles(String role) {
public void setRole(String role) {
this.role = role;
}
......@@ -117,12 +117,12 @@ public class User implements IUser{
getLastName().equals(user.getLastName()) &&
getUserName().equals(user.getUserName()) &&
getPassword().equals(user.getPassword()) &&
getRoles().equals(user.getRoles());
getRole().equals(user.getRole());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getFirstName(), getLastName(), getUserName(), getRoles());
return Objects.hash(getId(), getFirstName(), getLastName(), getUserName(), getRole());
}
}
......@@ -4,4 +4,6 @@ import com.example.server.model.Comment;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CommentRepository extends MongoRepository<Comment, String> {
Comment getCommentById(String id);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment