Commit 2e651a0a authored by kasmiii's avatar kasmiii
Browse files

Merge remote-tracking branch 'origin/main' into main

# Conflicts:
#	server/src/main/java/com/example/server/controller/UserController.java
parents 02308d41 0de2275e
package com.example.server.config.websocket;
public class Greeting {
private String content;
public Greeting() {
}
public Greeting(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
......@@ -12,13 +12,15 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
config.enableSimpleBroker("/api");
config.setApplicationDestinationPrefixes("/gkz");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").withSockJS();
registry
.addEndpoint("/gkz-stomp-endpoint")
.setAllowedOrigins("http://localhost:4200")
.withSockJS();
}
}
package com.example.server.controller;
import com.example.server.model.comment.Comment;
import com.example.server.model.comment.CommentEntity;
import com.example.server.model.post.Post;
import com.example.server.model.post.PostEntity;
import com.example.server.model.user.User;
import com.example.server.model.user.UserEntity;
import com.example.server.services.comment.CommentService;
import com.example.server.services.post.PostService;
......@@ -30,64 +33,64 @@ public class CommentController {
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add/{postId}/{userid}")
public ResponseEntity<CommentEntity> add(@RequestBody JsonNode jsonNode, @PathVariable String postId,
@PathVariable String userid) throws JsonProcessingException {
public ResponseEntity<Comment> add(@RequestBody JsonNode jsonNode, @PathVariable String postId,
@PathVariable String userid) throws JsonProcessingException {
UserEntity author = userService.getById(userid);
PostEntity postEntity = postService.getById(postId);
User author = userService.getById(userid);
Post post = postService.getById(postId);
CommentEntity commentEntity = mapper.treeToValue(jsonNode, CommentEntity.class);
Comment comment = mapper.treeToValue(jsonNode, CommentEntity.class);
commentEntity.setDate(DateGenerator.generateDate());
commentEntity.setAuthor(author);
commentEntity.setReply(new ArrayList<>());
comment.setDate(DateGenerator.generateDate());
comment.setAuthor(author);
comment.setReply(new ArrayList<>());
postEntity.addComment(commentEntity);
post.addComment(comment);
commentService.add(commentEntity);
commentService.add(comment);
postService.add(postEntity);
postService.add(post);
return new ResponseEntity<>(commentEntity, HttpStatus.CREATED);
return new ResponseEntity<>(comment, HttpStatus.CREATED);
}
@PutMapping("/{postId}/{commentId}")
public ResponseEntity<CommentEntity> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) throws JsonProcessingException {
public ResponseEntity<Comment> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) throws JsonProcessingException {
CommentEntity commentEntity = commentService.getById(commentId);
PostEntity postEntity = postService.getById(postId);
Comment comment = commentService.getById(commentId);
Post post = postService.getById(postId);
CommentEntity newCommentEntity = mapper.treeToValue(jsonNode, CommentEntity.class);
CommentEntity newComment = mapper.treeToValue(jsonNode, CommentEntity.class);
commentEntity.setMessage(newCommentEntity.getMessage());
postEntity.addComment(commentEntity);
comment.setMessage(newComment.getMessage());
post.addComment(comment);
postService.update(postId, postEntity);
postService.update(postId, post);
return new ResponseEntity<>(commentEntity, HttpStatus.ACCEPTED);
return new ResponseEntity<>(comment, HttpStatus.ACCEPTED);
}
@GetMapping("/{id}")
public ResponseEntity<CommentEntity> getById(@PathVariable String id) {
public ResponseEntity<Comment> getById(@PathVariable String id) {
return new ResponseEntity<>(commentService.getById(id), HttpStatus.OK);
}
@DeleteMapping("/{postId}/{commentId}")
public ResponseEntity<String> deleteById(@PathVariable String postId, @PathVariable String commentId) {
PostEntity postEntity = postService.getById(postId);
CommentEntity commentEntity = commentService.getById(commentId);
Post post = postService.getById(postId);
Comment comment = commentService.getById(commentId);
postEntity.deleteComment(commentEntity);
post.deleteComment(comment);
commentService.deleteById(commentId);
postService.update(postId, postEntity);
postService.update(postId, post);
return ResponseEntity.ok("Comment deleted successfully !");
}
@GetMapping("/all")
public ResponseEntity<List<CommentEntity>> getAll() {
public ResponseEntity<List<Comment>> getAll() {
return new ResponseEntity<>(commentService.findAll(), HttpStatus.OK);
}
......
package com.example.server.controller;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class Controller {
/*
@MessageMapping("/api")
@SendTo("/home")
@GetMapping("/home")
public String homePage(){
return "Welcome";
return "Home page !";
}
@GetMapping("/admin")
public String adminHome(){
return "Admin page";
}
*/
}
package com.example.server.controller;
import com.example.server.model.post.Post;
import com.example.server.model.post.PostEntity;
import com.example.server.model.user.User;
import com.example.server.model.user.UserEntity;
import com.example.server.services.post.PostService;
import com.example.server.services.post.PostServiceImplement;
......@@ -31,36 +33,36 @@ public class PostController {
@PostMapping("/add/{userid}")
public ResponseEntity<PostEntity> add(@RequestBody JsonNode jsonNode, @PathVariable String userid) {
public ResponseEntity<Post> add(@RequestBody JsonNode jsonNode, @PathVariable String userid) {
UserEntity author = userService.getById(userid);
PostEntity postEntity = mapper.convertValue(jsonNode, PostEntity.class);
postEntity.setAuthor(author);
postEntity.setDate(DateGenerator.generateDate());
postEntity.setCommentEntities(new ArrayList<>());
User author = userService.getById(userid);
Post post = mapper.convertValue(jsonNode, PostEntity.class);
post.setAuthor(author);
post.setDate(DateGenerator.generateDate());
post.setComments(new ArrayList<>());
postService.add(postEntity);
postService.add(post);
return new ResponseEntity<>(postEntity, HttpStatus.CREATED);
return new ResponseEntity<>(post, HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntity<PostEntity> update(@RequestBody JsonNode jsonNode, @PathVariable String id) throws JsonProcessingException {
public ResponseEntity<Post> update(@RequestBody JsonNode jsonNode, @PathVariable String id) throws JsonProcessingException {
PostEntity postEntity = postService.getById(id);
PostEntity newPostEntity = mapper.treeToValue(jsonNode, PostEntity.class);
Post post = postService.getById(id);
Post newPost = mapper.treeToValue(jsonNode, PostEntity.class);
UserEntity userEntity = userService.getById(postEntity.getAuthor().getId());
newPostEntity.setAuthor(userEntity);
newPostEntity.setDate(postEntity.getDate());
User userEntity = userService.getById(post.getAuthor().getId());
newPost.setAuthor(userEntity);
newPost.setDate(post.getDate());
postService.update(id, newPostEntity);
postService.update(id, newPost);
return new ResponseEntity<>(newPostEntity, HttpStatus.ACCEPTED);
return new ResponseEntity<>(newPost, HttpStatus.ACCEPTED);
}
@GetMapping("/{id}")
public ResponseEntity<PostEntity> getById(@PathVariable String id) {
public ResponseEntity<Post> getById(@PathVariable String id) {
return new ResponseEntity<>(postService.getById(id), HttpStatus.OK);
}
......@@ -77,7 +79,7 @@ public class PostController {
}
@GetMapping("/all")
public ResponseEntity<List<PostEntity>> getAll() {
public ResponseEntity<List<Post>> getAll() {
return new ResponseEntity<>(postService.findAll(), HttpStatus.OK);
}
}
package com.example.server.controller;
import com.example.server.config.websocket.Greeting;
import com.example.server.config.websocket.Message;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.example.server.model.user.User;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;
@Controller
@RequestMapping("/api")
public class SocketController {
@GetMapping("/hello")
@MessageMapping("/hello")
@SendTo("/api/hello")
public Greeting greeting(Message message) throws Exception {
Thread.sleep(1000);
return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getMessage()) + "!");
@MessageMapping("/test")
@SendTo("/api/hi")
public Message greeting(User user) {
return new Message("Hi, " + user.getLastName() + "!");
}
@GetMapping()
public ResponseEntity<String> home(){
return new ResponseEntity<>("Home page", HttpStatus.OK);
}
}
package com.example.server.controller;
import com.example.server.model.Role;
import com.example.server.model.user.UserEntity;
import com.example.server.repository.UserRepository;
import com.example.server.services.user.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
......@@ -22,7 +19,7 @@ public class UserController {
private final UserService userService;
private final ObjectMapper mapper = new ObjectMapper();
private final UserRepository userRepository;
@PostMapping("/add")
public ResponseEntity<UserEntity> add(@RequestBody JsonNode jsonNode) throws JsonProcessingException {
......@@ -57,21 +54,4 @@ public class UserController {
userService.deleteAll();
return ResponseEntity.ok("All users Deleted successfully");
}
//just for testing...
@PostMapping("/saveUsers")
public void saveUsers(){
UserEntity user1=new UserEntity();
user1.setUserName("mohammed");
user1.setUserName("kasmi");
user1.setRole(Role.USER.name());
UserEntity user2=new UserEntity();
user1.setUserName("mohammed2");
user1.setUserName("kasmi2");
user1.setRole(Role.USER.name());
userRepository.save(user1);
userRepository.save(user2);
}
}
package com.example.server.model.comment;
import com.example.server.model.user.User;
import com.example.server.model.user.UserEntity;
import java.util.List;
......@@ -8,12 +9,14 @@ public interface Comment {
String getId();
String getMessage();
UserEntity getAuthor();
User getAuthor();
String getDate();
List<Comment> getReply();
void setDate(String date);
List<CommentEntity> getReply();
void setMessage(String message);
void setAuthor(UserEntity author);
void addReply(CommentEntity commentEntity);
void setAuthor(User author);
void setReply(List<Comment> comment);
void addReply(Comment comment);
void deleteReply(Comment comment);
}
package com.example.server.model.comment;
import com.example.server.model.user.UserEntity;
import com.example.server.model.user.User;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
......@@ -23,11 +23,11 @@ public class CommentEntity implements Comment{
@Field("message")
private String message;
@Field("author")
private UserEntity author;
private User author;
@Field("date")
private String date;
@Field("reply")
private List<CommentEntity> reply;
private List<Comment> reply;
public CommentEntity(String message) {
this.message = message;
......@@ -46,7 +46,7 @@ public class CommentEntity implements Comment{
}
@Override
public UserEntity getAuthor() {
public User getAuthor() {
return author;
}
......@@ -61,7 +61,7 @@ public class CommentEntity implements Comment{
}
@Override
public List<CommentEntity> getReply() {
public List<Comment> getReply() {
return reply;
}
......@@ -71,13 +71,23 @@ public class CommentEntity implements Comment{
}
@Override
public void setAuthor(UserEntity author) {
public void setAuthor(User author) {
this.author = author;
}
@Override
public void addReply(CommentEntity commentEntity){
this.reply.add(commentEntity);
public void setReply(List<Comment> comments){
this.reply = comments;
}
@Override
public void addReply(Comment comment) {
this.reply.add(comment);
}
@Override
public void deleteReply(Comment comment){
this.reply.remove(comment);
}
@Override
......@@ -94,11 +104,11 @@ public class CommentEntity implements Comment{
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommentEntity commentEntity = (CommentEntity) o;
return getId().equals(commentEntity.getId()) &&
getMessage().equals(commentEntity.getMessage()) &&
getAuthor().equals(commentEntity.getAuthor()) &&
getReply().equals(commentEntity.getReply());
Comment comment = (Comment) o;
return getId().equals(comment.getId()) &&
getMessage().equals(comment.getMessage()) &&
getAuthor().equals(comment.getAuthor()) &&
getReply().equals(comment.getReply());
}
@Override
......
package com.example.server.model.post;
import com.example.server.model.comment.Comment;
import com.example.server.model.comment.CommentEntity;
import com.example.server.model.user.User;
import com.example.server.model.user.UserEntity;
import java.util.List;
......@@ -10,14 +12,15 @@ public interface Post {
String getId();
String getSubject();
String getContent();
UserEntity getAuthor();
User getAuthor();
String getDate();
List<Comment> getComments();
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);
void setAuthor(User author);
void setComments(List<Comment> comments);
void addComment(Comment commentEntity);
void deleteComment(Comment commentEntity);
void setDate(String date);
}
package com.example.server.model.post;
import com.example.server.model.comment.CommentEntity;
import com.example.server.model.user.UserEntity;
import com.example.server.model.comment.Comment;
import com.example.server.model.user.User;
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.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Data
@Setter
......@@ -24,18 +26,18 @@ public class PostEntity implements Post{
@Field("content")
private String content;
@Field("author")
private UserEntity author;
private User author;
@Field("date")
private String date;
@Field("comments")
private List<CommentEntity> commentEntities;
private List<Comment> comments;
public PostEntity(String subject, String content) {
this.subject = subject;
this.content = content;
this.author = null;
this.date = null;
this.commentEntities = new ArrayList<>();
this.comments = new ArrayList<>();
}
@Override
......@@ -54,7 +56,7 @@ public class PostEntity implements Post{
}
@Override
public UserEntity getAuthor() {
public User getAuthor() {
return author;
}
......@@ -74,28 +76,33 @@ public class PostEntity implements Post{
}
@Override
public void setAuthor(UserEntity author) {
public void setAuthor(User author) {
this.author = author;
}
@Override
public void setCommentEntities(List<CommentEntity> commentEntities) {
this.commentEntities = commentEntities;
public void setComments(List<Comment> comments) {
this.comments = comments;
}
@Override
public List<CommentEntity> getCommentEntities() {
return commentEntities;
public List<Comment> getComments() {
return comments;
}
@Override
public void addComment(CommentEntity commentEntity){
this.commentEntities.add(commentEntity);
public void addComment(Comment comment){
this.comments.add(comment);
}
@Override
public void deleteComment(CommentEntity commentEntity){
this.commentEntities.remove(commentEntity);
public void deleteComment(Comment comment){
this.comments.remove(comment);
}
@Override
public void setDate(String date){
this.date = date;
}
@Override
......@@ -105,7 +112,7 @@ public class PostEntity implements Post{
", subject='" + subject + '\'' +
", content='" + content + '\'' +
", author=" + author +
", comments=" + commentEntities +
", comments=" + comments +
'}';
}
......@@ -118,11 +125,11 @@ public class PostEntity implements Post{
getSubject().equals(postEntity.getSubject()) &&
getContent().equals(postEntity.getContent()) &&
getAuthor().equals(postEntity.getAuthor()) &&
getCommentEntities().equals(postEntity.getCommentEntities());
getComments().equals(postEntity.getComments());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getSubject(), getContent(), getAuthor(), getCommentEntities());
return Objects.hash(getId(), getSubject(), getContent(), getAuthor(), getComments());
}
}