Commit ac1ecf00 authored by Mamadou Diallo's avatar Mamadou Diallo
Browse files

Architecture update + Code cleaning

parent 9f369dbd
......@@ -54,7 +54,7 @@ if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@REM To isolate internal variables from possible postEntity scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
......@@ -169,7 +169,7 @@ set ERROR_CODE=1
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
@REM check for postEntity script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
:skipRcPost
......
package com.example.server.config;
import com.example.server.model.User;
import com.example.server.model.UserEntity;
import com.example.server.model.user.User;
import com.example.server.model.user.UserEntity;
import com.example.server.utils.Const;
import com.example.server.utils.Encoders;
import org.springframework.context.annotation.Configuration;
......
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.UserEntity;
import com.example.server.model.comment.CommentEntity;
import com.example.server.model.post.PostEntity;
import com.example.server.model.user.UserEntity;
import com.example.server.repository.CommentRepository;
import com.example.server.repository.PostRepository;
import com.example.server.repository.UserRepository;
......@@ -31,38 +31,38 @@ public class CommentController {
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add/{postId}/{userid}")
public ResponseEntity<Comment> add(@RequestBody JsonNode jsonNode, @PathVariable String postId,
@PathVariable String userid) throws JsonProcessingException {
public ResponseEntity<CommentEntity> add(@RequestBody JsonNode jsonNode, @PathVariable String postId,
@PathVariable String userid) throws JsonProcessingException {
UserEntity author = userRepository.getUserById(userid);
Post post = postRepository.getPostById(postId);
PostEntity postEntity = postRepository.getPostById(postId);
Comment comment = mapper.treeToValue(jsonNode, Comment.class);
comment.setDate(DateGenerator.generateDate());
comment.setAuthor(author);
comment.setReply(new ArrayList<>());
post.addComment(repository.save(comment));
CommentEntity commentEntity = mapper.treeToValue(jsonNode, CommentEntity.class);
commentEntity.setDate(DateGenerator.generateDate());
commentEntity.setAuthor(author);
commentEntity.setReply(new ArrayList<>());
postEntity.addComment(repository.save(commentEntity));
postRepository.deleteById(postId);
postRepository.save(post);
postRepository.save(postEntity);
return new ResponseEntity<>(comment, HttpStatus.CREATED);
return new ResponseEntity<>(commentEntity, HttpStatus.CREATED);
}
@PutMapping("/{postId}/{commentId}")
public ResponseEntity<Comment> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) throws JsonProcessingException {
public ResponseEntity<CommentEntity> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) throws JsonProcessingException {
if(!repository.existsById(commentId)){
throw new CommentNotFoundException(commentId);
}
Comment comment = repository.getCommentById(commentId);
Post post = postRepository.getPostById(postId);
CommentEntity commentEntity = repository.getCommentById(commentId);
PostEntity postEntity = postRepository.getPostById(postId);
//post.deleteComment(comment);
Comment newComment = mapper.treeToValue(jsonNode, Comment.class);
CommentEntity newCommentEntity = mapper.treeToValue(jsonNode, CommentEntity.class);
comment.setMessage(newComment.getMessage());
commentEntity.setMessage(newCommentEntity.getMessage());
postRepository.save(post);
postRepository.save(postEntity);
postRepository.deleteById(postId);
repository.deleteById(commentId);
......@@ -75,7 +75,7 @@ public class CommentController {
}
@GetMapping("/{id}")
public ResponseEntity<Optional<Comment>> getById(@PathVariable String id) {
public ResponseEntity<Optional<CommentEntity>> getById(@PathVariable String id) {
if(!repository.existsById(id)) throw new CommentNotFoundException(id);
return new ResponseEntity<>(repository.findById(id), HttpStatus.OK);
......@@ -87,20 +87,20 @@ public class CommentController {
throw new CommentNotFoundException(commentId);
}
Post post = postRepository.getPostById(postId);
Comment comment = repository.getCommentById(commentId);
PostEntity postEntity = postRepository.getPostById(postId);
CommentEntity commentEntity = repository.getCommentById(commentId);
post.deleteComment(comment);
postEntity.deleteComment(commentEntity);
repository.deleteById(commentId);
postRepository.deleteById(postId);
postRepository.save(post);
postRepository.save(postEntity);
return ResponseEntity.ok("Comment deleted successfully !");
}
@GetMapping("/all")
public ResponseEntity<List<Comment>> getAll() {
public ResponseEntity<List<CommentEntity>> getAll() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
}
......
package com.example.server.controller;
import com.example.server.exceptions.PostNotFoundException;
import com.example.server.model.Post;
import com.example.server.model.UserEntity;
import com.example.server.repository.PostRepository;
import com.example.server.repository.UserRepository;
import com.example.server.model.post.PostEntity;
import com.example.server.model.user.UserEntity;
import com.example.server.services.post.PostServiceImplement;
import com.example.server.services.user.UserServiceImplement;
import com.example.server.utils.DateGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -22,67 +21,59 @@ import java.util.List;
@RequestMapping("/api/post")
public class PostController {
private final PostRepository repository;
private final UserRepository userRepository;
private final PostServiceImplement postServiceImplement;
private final UserServiceImplement userServiceImplement;
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add/{userid}")
public ResponseEntity<Post> add(@RequestBody JsonNode jsonNode, @PathVariable String userid) {
public ResponseEntity<PostEntity> add(@RequestBody JsonNode jsonNode, @PathVariable String userid) {
UserEntity author = userRepository.getUserById(userid);
Post post = mapper.convertValue(jsonNode, Post.class);
post.setAuthor(author);
post.setDate(DateGenerator.generateDate());
post.setComments(new ArrayList<>());
repository.save(post);
UserEntity author = userServiceImplement.getById(userid);
PostEntity postEntity = mapper.convertValue(jsonNode, PostEntity.class);
postEntity.setAuthor(author);
postEntity.setDate(DateGenerator.generateDate());
postEntity.setCommentEntities(new ArrayList<>());
return new ResponseEntity<>(post, HttpStatus.CREATED);
postServiceImplement.add(postEntity);
return new ResponseEntity<>(postEntity, HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<Post> update(@RequestBody JsonNode jsonNode, @PathVariable String id) throws JsonProcessingException {
if(!repository.existsById(id)){
throw new PostNotFoundException(id);
}
Post post = repository.getPostById(id);
Post newPost = mapper.treeToValue(jsonNode, Post.class);
public ResponseEntity<PostEntity> update(@RequestBody JsonNode jsonNode, @PathVariable String id) throws JsonProcessingException {
PostEntity postEntity = postServiceImplement.getById(id);
PostEntity newPostEntity = mapper.treeToValue(jsonNode, PostEntity.class);
UserEntity userEntity = userRepository.getUserById(post.getAuthor().getId());
newPost.setAuthor(userEntity);
newPost.setDate(post.getDate());
System.out.println(newPost);
UserEntity userEntity = userServiceImplement.getById(postEntity.getAuthor().getId());
newPostEntity.setAuthor(userEntity);
newPostEntity.setDate(postEntity.getDate());
repository.deleteById(id);
postServiceImplement.update(id, newPostEntity);
return new ResponseEntity<>(repository.save(newPost), HttpStatus.ACCEPTED);
return new ResponseEntity<>(newPostEntity, HttpStatus.ACCEPTED);
}
@GetMapping("/{id}")
public ResponseEntity<Post> getById(@PathVariable String id) {
return new ResponseEntity<>(repository.getPostById(id), HttpStatus.OK);
public ResponseEntity<PostEntity> getById(@PathVariable String id) {
return new ResponseEntity<>(postServiceImplement.getById(id), HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteById(@PathVariable String id) {
if(!repository.existsById(id)){
throw new PostNotFoundException(id);
}
repository.deleteById(id);
postServiceImplement.deleteById(id);
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.ACCEPTED);
}
postServiceImplement.deleteAll();
return new ResponseEntity<>("Post repository is empty", HttpStatus.NOT_FOUND);
}
@GetMapping("/all")
public ResponseEntity<List<Post>> getAll() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
public ResponseEntity<List<PostEntity>> getAll() {
return new ResponseEntity<>(postServiceImplement.findAll(), HttpStatus.OK);
}
}
package com.example.server.controller;
import com.example.server.exceptions.UserNotFoundException;
import com.example.server.model.UserEntity;
import com.example.server.repository.UserRepository;
import com.example.server.model.user.UserEntity;
import com.example.server.services.user.UserServiceImplement;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -13,65 +12,48 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequiredArgsConstructor
@RequestMapping("api/user")
public class UserController {
private final UserRepository userRepository;
private final UserServiceImplement userService;
private final ObjectMapper mapper = new ObjectMapper();
/**
*
* @param jsonNode
* @return
* @throws JsonProcessingException
*/
@PostMapping("/add")
public ResponseEntity<UserEntity> add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
UserEntity userEntity = mapper.treeToValue(jsonNode, UserEntity.class);
return new ResponseEntity<>(userRepository.save(userEntity), HttpStatus.CREATED);
return new ResponseEntity<>(userService.add(userEntity), HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<UserEntity> update(@PathVariable("id") String id, @RequestBody JsonNode jsonNode) throws JsonProcessingException {
if(!userRepository.existsById(id)){
throw new UserNotFoundException(id);
}
userRepository.deleteById(id);
UserEntity userEntity = mapper.treeToValue(jsonNode, UserEntity.class);
return new ResponseEntity<>(userRepository.save(userEntity), HttpStatus.OK);
return new ResponseEntity<>(userService.update(id, userEntity), HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<UserEntity> getById(@PathVariable("id") String id) {
System.out.println("id : "+id);
Optional<UserEntity> user = userRepository.findById(id);
return user.map(value -> new ResponseEntity<>(value, HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
return new ResponseEntity<>(userService.getById(id), HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteById(@PathVariable String id) {
if(!userRepository.existsById(id)){
throw new UserNotFoundException(id);
}
userRepository.deleteById(id);
userService.deleteById(id);
return new ResponseEntity<>("Deleted successfully !", HttpStatus.OK);
}
@GetMapping("/all")
@PreAuthorize("hasAnyRole('ADMIN')")
public ResponseEntity<List<UserEntity>> getAll() {
return new ResponseEntity<>(userRepository.findAll(), HttpStatus.OK);
return new ResponseEntity<>(userService.findAll(), HttpStatus.OK);
}
@DeleteMapping("/all")
public ResponseEntity<?> deleteAll(){
if(userRepository.count() > 0){
userRepository.deleteAll();
}
userService.deleteAll();
return ResponseEntity.ok("All users Deleted successfully");
}
}
package com.example.server.model.comment;
import com.example.server.model.user.UserEntity;
import java.util.List;
public interface Comment {
String getId();
String getMessage();
UserEntity getAuthor();
String getDate();
void setDate(String date);
List<CommentEntity> getReply();
void setMessage(String message);
void setAuthor(UserEntity author);
void addReply(CommentEntity commentEntity);
}
package com.example.server.model;
package com.example.server.model.comment;
import com.example.server.model.user.UserEntity;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
......@@ -15,7 +16,7 @@ import java.util.Objects;
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "comment")
public class Comment{
public class CommentEntity implements Comment{
@Id
private String id;
......@@ -26,48 +27,57 @@ public class Comment{
@Field("date")
private String date;
@Field("reply")
private List<Comment> reply;
private List<CommentEntity> reply;
public Comment(String message) {
public CommentEntity(String message) {
this.message = message;
this.author = null;
this.reply = new ArrayList<>();
}
@Override
public String getId() {
return id;
}
@Override
public String getMessage() {
return message;
}
@Override
public UserEntity getAuthor() {
return author;
}
@Override
public String getDate() {
return date;
}
@Override
public void setDate(String date) {
this.date = date;
}
public List<Comment> getReply() {
@Override
public List<CommentEntity> getReply() {
return reply;
}
@Override
public void setMessage(String message) {
this.message = message;
}
@Override
public void setAuthor(UserEntity author) {
this.author = author;
}
public void addReply(Comment comment){
this.reply.add(comment);
@Override
public void addReply(CommentEntity commentEntity){
this.reply.add(commentEntity);
}
@Override
......@@ -84,11 +94,11 @@ public class Comment{
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Comment comment = (Comment) o;
return getId().equals(comment.getId()) &&
getMessage().equals(comment.getMessage()) &&
getAuthor().equals(comment.getAuthor()) &&
getReply().equals(comment.getReply());
CommentEntity commentEntity = (CommentEntity) o;
return getId().equals(commentEntity.getId()) &&
getMessage().equals(commentEntity.getMessage()) &&
getAuthor().equals(commentEntity.getAuthor()) &&
getReply().equals(commentEntity.getReply());
}
@Override
......
package com.example.server.model.post;
import com.example.server.model.comment.CommentEntity;
import com.example.server.model.user.UserEntity;
import java.util.List;
public interface Post {
String getId();
String getSubject();
String getContent();
UserEntity getAuthor();
String getDate();
void setSubject(String subject);
void setContent(String content);
void setAuthor(UserEntity author);
void setCommentEntities(List<CommentEntity> commentEntities);
List<CommentEntity> getCommentEntities();
void addComment(CommentEntity commentEntity);
void deleteComment(CommentEntity commentEntity);
}
package com.example.server.model;
package com.example.server.model.post;
import com.example.server.model.comment.CommentEntity;
import com.example.server.model.user.UserEntity;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
......@@ -13,7 +15,7 @@ import java.util.*;
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "post")
public class Post{
public class PostEntity implements Post{
@Id
private String id;
......@@ -26,62 +28,74 @@ public class Post{
@Field("date")
private String date;
@Field("comments")
private List<Comment> comments;
private List<CommentEntity> commentEntities;
public Post(String subject, String content) {
public PostEntity(String subject, String content) {
this.subject = subject;
this.content = content;
this.author = null;
this.date = null;
this.comments = new ArrayList<>();
this.commentEntities = new ArrayList<>();
}
@Override
public String getId() {
return id;
}
@Override
public String getSubject() {
return subject;
}
@Override
public String getContent() {
return content;
}
@Override
public UserEntity getAuthor() {
return author;
}
@Override
public String getDate() {
return date;
}
@Override
public void setSubject(String subject) {
this.subject = subject;
}
@Override
public void setContent(String content) {
this.content = content;
}
@Override
public void setAuthor(UserEntity author) {
this.author = author;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
@Override
public void setCommentEntities(List<CommentEntity> commentEntities) {
this.commentEntities = commentEntities;
}
public List<Comment> getComments() {
return comments;
@Override
public List<CommentEntity> getCommentEntities() {
return commentEntities;
}
public void addComment(Comment comment){
this.comments.add(comment);
@Override
public void addComment(CommentEntity commentEntity){
this.commentEntities.add(commentEntity);
}
public void deleteComment(Comment comment){
this.comments.remove(comment);
@Override
public void deleteComment(CommentEntity commentEntity){
this.commentEntities.remove(commentEntity);
}
@Override
......@@ -91,7 +105,7 @@ public class Post{
", subject='" + subject + '\'' +
", content='" + content + '\'' +
", author=" + author +
", comments=" + comments +
", comments=" + commentEntities +
'}';
}
......@@ -99,16 +113,16 @@ public class Post{
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;