diff --git a/build.gradle b/build.gradle index e0dbe9c..f2e7794 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,8 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'com.h2database:h2:2.1.210' + implementation 'org.hibernate.validator:hibernate-validator' + implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' } tasks.named('test') { diff --git a/data.mv.db b/data.mv.db index c139741..18776d2 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java new file mode 100644 index 0000000..6b2bd7f --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java @@ -0,0 +1,51 @@ +package com.webproglabs.lab1.lab34.controller; + +import com.webproglabs.lab1.lab34.model.Comment; +import com.webproglabs.lab1.lab34.model.Profile; +import com.webproglabs.lab1.lab34.services.CommentService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/comment") +public class CommentController { + private final CommentService commentService; + + public CommentController(CommentService commentService) { + this.commentService = commentService; + } + + @GetMapping("/{id}") + public CommentDto getComment(@PathVariable Long id) { + return new CommentDto(commentService.findComment(id)); + } + + @GetMapping + public List getComments() { + return commentService.findAllComments().stream() + .map(CommentDto::new) + .toList(); + } + + @PostMapping + public CommentDto createComment(@RequestParam("text") String text, @RequestParam("ownerId") Long id, @RequestParam("postId") Long postId){ + final Comment comment = commentService.addComment(text, id, postId); + return new CommentDto(comment); + } + + @PutMapping("/{id}") + public CommentDto updateComment(@RequestParam("text") String text, @PathVariable Long id) { + return new CommentDto(commentService.updateComment(id, text)); + } + + @DeleteMapping("/{id}") + public CommentDto deleteComment(@PathVariable Long id) { + return new CommentDto(commentService.deleteComment(id)); + } + + @DeleteMapping + public void deleteAllComments(){ + commentService.deleteAllComments(); + } +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java new file mode 100644 index 0000000..4b1bc5d --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java @@ -0,0 +1,16 @@ +package com.webproglabs.lab1.lab34.controller; + +import com.webproglabs.lab1.lab34.model.Comment; + +public class CommentDto { + private Long id; + private String text; + + public CommentDto(Comment comment) { + this.id = comment.getId(); + this.text = comment.getText(); + } + + public Long getId() {return id;} + public String getText() {return text;} +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java new file mode 100644 index 0000000..a0551d7 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java @@ -0,0 +1,56 @@ +package com.webproglabs.lab1.lab34.controller; + +import com.webproglabs.lab1.lab34.services.PostService; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/post") +public class PostController { + private final PostService postService; + + public PostController(PostService postService) { + this.postService = postService; + } + + @GetMapping("/{id}") + public PostDto getPost(@PathVariable Long id) { + return new PostDto(postService.findPost(id)); + } + + @GetMapping + public List getPosts() { + return postService.findAllPosts().stream() + .map(PostDto::new) + .toList(); + } + + @PostMapping + public PostDto createPost( + @RequestParam("text") String text, + @RequestParam("authorId") Long authorId + ) + { + return new PostDto(postService.addPost(text, new ArrayList<>(),authorId)); + } + + @PutMapping("/{id}") + public PostDto updatePost( + @PathVariable Long id, + @RequestParam("text") String text + ) + { + return new PostDto(postService.updatePost(id, text)); + } + + @DeleteMapping("/{id}") + public PostDto deletePost (@PathVariable Long id) { + return new PostDto(postService.deletePost(id)); + } + @DeleteMapping + public void deleteAllPosts() { + postService.deleteAllPosts(); + } +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java b/src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java new file mode 100644 index 0000000..49075af --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java @@ -0,0 +1,26 @@ +package com.webproglabs.lab1.lab34.controller; + +import com.webproglabs.lab1.lab34.model.Comment; +import com.webproglabs.lab1.lab34.model.Post; +import com.webproglabs.lab1.lab34.model.Profile; + +import java.util.ArrayList; +import java.util.List; + +public class PostDto { + private Long id; + private String text; + private List comments = new ArrayList<>(); + + public PostDto(Post post){ + this.id = post.getId(); + this.text = post.getText(); + for(Comment comment: post.getComments()){ + comments.add(new CommentDto(comment)); + } + } + + public Long getId() {return id;} + public String getText() {return text;} + public List getComments() {return comments;} +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java new file mode 100644 index 0000000..46cb114 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java @@ -0,0 +1,57 @@ +package com.webproglabs.lab1.lab34.controller; + +import com.webproglabs.lab1.lab34.services.ProfileService; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/profile") +public class ProfileController { + private final ProfileService profileService; + + public ProfileController(ProfileService profileService) { + this.profileService = profileService; + } + + @GetMapping("/{id}") + public ProfileDto getProfile(@PathVariable Long id) { + return new ProfileDto(profileService.findUser(id)); + } + + @GetMapping + public List getProfiles() { + return profileService.findAllUsers().stream() + .map(ProfileDto::new) + .toList(); + } + + @PostMapping + public ProfileDto createProfile( + @RequestParam("login") String login, + @RequestParam("password") String password + ) + { + return new ProfileDto(profileService.addUser(login, password, new ArrayList<>(),new ArrayList<>() )); + } + + @PutMapping("/{id}") + public ProfileDto updateProfile( + @PathVariable Long id, + @RequestParam("login") String login, + @RequestParam("password") String password + ) + { + return new ProfileDto(profileService.updateUser(id, login, password)); + } + + @DeleteMapping("/{id}") + public ProfileDto deleteProfile (@PathVariable Long id) { + return new ProfileDto(profileService.deleteUser(id)); + } + @DeleteMapping + public void deleteAllProfiles() { + profileService.deleteAllUsers(); + } +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java new file mode 100644 index 0000000..29f948b --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java @@ -0,0 +1,34 @@ +package com.webproglabs.lab1.lab34.controller; + +import com.webproglabs.lab1.lab34.model.Comment; +import com.webproglabs.lab1.lab34.model.Post; +import com.webproglabs.lab1.lab34.model.Profile; + +import java.util.ArrayList; +import java.util.List; + +public class ProfileDto { + private Long id; + private String login; + private String password; + private List comments = new ArrayList<>(); + + private List posts = new ArrayList<>(); + public ProfileDto(Profile profile){ + this.id = profile.getId(); + this.login = profile.getLogin(); + this.password = profile.getPassword(); + for(Comment comment: profile.getComments()){ + comments.add(new CommentDto(comment)); + } + for (Post post: profile.getPosts()) { + posts.add(new PostDto(post)); + } + } + + public Long getId() {return id;} + public String getLogin() {return login;} + public String getPassword() {return password;} + public List getComments() {return comments;} + public List getPosts() {return posts;} +} diff --git a/src/main/java/com/webproglabs/lab1/model/Comment.java b/src/main/java/com/webproglabs/lab1/lab34/model/Comment.java similarity index 70% rename from src/main/java/com/webproglabs/lab1/model/Comment.java rename to src/main/java/com/webproglabs/lab1/lab34/model/Comment.java index ef88bf6..f3da3d4 100644 --- a/src/main/java/com/webproglabs/lab1/model/Comment.java +++ b/src/main/java/com/webproglabs/lab1/lab34/model/Comment.java @@ -1,4 +1,4 @@ -package com.webproglabs.lab1.model; +package com.webproglabs.lab1.lab34.model; import javax.persistence.*; import java.util.Objects; @@ -14,10 +14,14 @@ public class Comment { @ManyToOne() private Profile owner; + @ManyToOne() + private Post post; + public Comment(){}; - public Comment(String text, Profile owner) { + public Comment(String text, Profile owner, Post post) { this.text = text; this.owner = owner; + this.post = post; } public Long getId() { @@ -36,12 +40,23 @@ public class Comment { return owner; } public void setOwner(Profile owner) { + this.owner.getComments().remove(this); this.owner = owner; if (!owner.getComments().contains(this)) { owner.getComments().add(this); } } + public Post getPost() {return post; } + + public void setPost(Post post) { + this.post.getComments().remove(this); + this.post = post; + if (!post.getComments().contains(this)) { + post.getComments().add(this); + } + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -60,6 +75,7 @@ public class Comment { "id=" + id + ", text='" + text + '\'' + ", owner='" + owner.ToString() + '\'' + + ", post='" + post.ToString() + '\'' + '}'; } } diff --git a/src/main/java/com/webproglabs/lab1/lab34/model/Post.java b/src/main/java/com/webproglabs/lab1/lab34/model/Post.java new file mode 100644 index 0000000..8779f51 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/model/Post.java @@ -0,0 +1,90 @@ +package com.webproglabs.lab1.lab34.model; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Entity +public class Post { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String text; + + @OneToMany(mappedBy = "post", orphanRemoval = true, fetch = FetchType.EAGER) + private List comments = new ArrayList(); + + @ManyToOne() + private Profile author; + + public Post(){} + public Post(String text, Profile author, List comments) { + this.text = text; + this.author = author; + for (int i = 0; i < comments.size(); i++) { + addComment(comments.get(i)); + } + } + + public Long getId() { + return id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Profile getAuthor() { + return author; + } + + public void setAuthor(Profile author) { + this.author.getPosts().remove(this); + this.author = author; + if (!author.getPosts().contains(this)) { + author.getPosts().add(this); + } + } + + public List getComments() { return comments; } + public int getCommentsSize() { + if (comments == null) return 0; + else return comments.size(); + } + + public void addComment(Comment comment) { + this.comments.add(comment); + if (comment.getPost() != this) { + comment.setPost(this); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Post post = (Post) o; + return Objects.equals(id, post.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + public String ToString() { + return "Post{" + + "id=" + id + + ", text='" + text + '\'' + + ", author='" + author.ToString() + '\'' + + ", comments='" + getCommentsSize() + '\'' + + '}'; + } + +} diff --git a/src/main/java/com/webproglabs/lab1/model/Profile.java b/src/main/java/com/webproglabs/lab1/lab34/model/Profile.java similarity index 75% rename from src/main/java/com/webproglabs/lab1/model/Profile.java rename to src/main/java/com/webproglabs/lab1/lab34/model/Profile.java index fde0ff3..79702d6 100644 --- a/src/main/java/com/webproglabs/lab1/model/Profile.java +++ b/src/main/java/com/webproglabs/lab1/lab34/model/Profile.java @@ -1,4 +1,4 @@ -package com.webproglabs.lab1.model; +package com.webproglabs.lab1.lab34.model; import javax.persistence.*; import java.util.ArrayList; @@ -18,16 +18,19 @@ public class Profile { @OneToMany(mappedBy = "owner", orphanRemoval = true, fetch = FetchType.EAGER) private List comments = new ArrayList(); - @OneToMany(mappedBy = "friends", fetch = FetchType.EAGER) - private List friends = new ArrayList<>(); + @OneToMany(mappedBy = "author", orphanRemoval = true, fetch = FetchType.EAGER) + private List posts = new ArrayList(); public Profile(){} - public Profile(String login, String password, List comments) { + public Profile(String login, String password, List comments, List posts) { this.login = login; this.password=password; for (int i = 0; i < comments.size(); i++) { addComment(comments.get(i)); } + for (int i = 0; i < posts.size(); i++) { + addPost(posts.get(i)); + } }; public Long getId() { @@ -52,22 +55,16 @@ public class Profile { public List getComments() { return comments; } + public List getPosts() {return posts; } + public int getCommentsSize() { if (comments == null) return 0; else return comments.size(); } - public List getFriends(){ - return friends; - } - - public boolean addFriend(Profile friend){ - if (!this.equals(friend) && !friends.contains(friend)) { - this.friends.add(friend); - friend.getFriends().add(this); - return true; - } - return false; + public int getPostsSize() { + if (posts == null) return 0; + else return posts.size(); } public void addComment(Comment comment) { @@ -77,6 +74,13 @@ public class Profile { } } + public void addPost(Post post) { + this.posts.add(post); + if (post.getAuthor() != this) { + post.setAuthor(this); + } + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -96,6 +100,7 @@ public class Profile { ", login='" + login + '\'' + ", password='" + password + '\'' + ", comments='" + getCommentsSize() + '\'' + + ", comments='" + getPostsSize() + '\'' + '}'; } } diff --git a/src/main/java/com/webproglabs/lab1/lab34/repository/CommentRepository.java b/src/main/java/com/webproglabs/lab1/lab34/repository/CommentRepository.java new file mode 100644 index 0000000..eeb72b8 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/repository/CommentRepository.java @@ -0,0 +1,7 @@ +package com.webproglabs.lab1.lab34.repository; + +import com.webproglabs.lab1.lab34.model.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CommentRepository extends JpaRepository { +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/repository/PostRepository.java b/src/main/java/com/webproglabs/lab1/lab34/repository/PostRepository.java new file mode 100644 index 0000000..de6fc48 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/repository/PostRepository.java @@ -0,0 +1,8 @@ +package com.webproglabs.lab1.lab34.repository; + +import com.webproglabs.lab1.lab34.model.Comment; +import com.webproglabs.lab1.lab34.model.Post; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostRepository extends JpaRepository { +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java b/src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java new file mode 100644 index 0000000..f2508b7 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java @@ -0,0 +1,7 @@ +package com.webproglabs.lab1.lab34.repository; + +import com.webproglabs.lab1.lab34.model.Profile; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProfileRepository extends JpaRepository { +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/services/CommentService.java b/src/main/java/com/webproglabs/lab1/lab34/services/CommentService.java new file mode 100644 index 0000000..14220f9 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/services/CommentService.java @@ -0,0 +1,87 @@ +package com.webproglabs.lab1.lab34.services; + +import com.webproglabs.lab1.lab34.model.Comment; +import com.webproglabs.lab1.lab34.model.Post; +import com.webproglabs.lab1.lab34.model.Profile; +import com.webproglabs.lab1.lab34.repository.CommentRepository; +import com.webproglabs.lab1.lab34.repository.PostRepository; +import com.webproglabs.lab1.lab34.repository.ProfileRepository; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import javax.persistence.EntityNotFoundException; +import javax.transaction.Transactional; +import java.util.List; +import java.util.Optional; + +@Service +public class CommentService { + + private final ProfileRepository profileRepository; + private final CommentRepository commentRepository; + + private final PostRepository postRepository; + + public CommentService(ProfileRepository profileRepository, CommentRepository commentRepository, PostRepository postRepository) { + this.profileRepository = profileRepository; + this.commentRepository = commentRepository; + this.postRepository = postRepository; + } + + @Transactional + public Comment findComment(Long id) { + final Optional comment = commentRepository.findById(id); + return comment.orElseThrow(EntityNotFoundException::new); + } + + @Transactional + public List findAllComments() { + return commentRepository.findAll(); + } + + @Transactional + public Comment addComment(String text, Long profileId, Long postId) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Comment data is null or empty"); + } + try{ + Profile user = profileRepository.findById(profileId).get(); + Post post = postRepository.findById(postId).get(); + Comment comment = new Comment(text, user, post); + user.addComment(comment); + post.addComment(comment); + profileRepository.save(user); + postRepository.save(post); + return commentRepository.save(comment); + } + catch (Exception exception) { + return null; + } + } + + @Transactional + public Comment updateComment(Long id, String text) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Comment data is null or empty"); + } + Comment currentComment = findComment(id); + currentComment.setText(text); + final Profile owner = currentComment.getOwner(); + profileRepository.save(owner); + final Post post = currentComment.getPost(); + postRepository.save(post); + return commentRepository.save(currentComment); + } + + @Transactional + public Comment deleteComment(Long id) { + final Comment currentComment = findComment(id); + commentRepository.delete(currentComment); + return currentComment; + } + @Transactional + public void deleteAllComments() { + commentRepository.deleteAll(); + } + +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/services/PostService.java b/src/main/java/com/webproglabs/lab1/lab34/services/PostService.java new file mode 100644 index 0000000..2e931a4 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/services/PostService.java @@ -0,0 +1,72 @@ +package com.webproglabs.lab1.lab34.services; + +import com.webproglabs.lab1.lab34.model.Comment; +import com.webproglabs.lab1.lab34.model.Post; +import com.webproglabs.lab1.lab34.model.Profile; +import com.webproglabs.lab1.lab34.repository.PostRepository; +import com.webproglabs.lab1.lab34.repository.ProfileRepository; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import javax.persistence.EntityNotFoundException; +import javax.transaction.Transactional; +import java.util.List; +import java.util.Optional; + +@Service +public class PostService { + private final PostRepository postRepository; + private final ProfileRepository profileRepository; + + public PostService(PostRepository postRepository, ProfileRepository profileRepository) { + this.postRepository = postRepository; + this.profileRepository = profileRepository; + } + + @Transactional + public Post findPost(Long id) { + final Optional post = postRepository.findById(id); + return post.orElseThrow(EntityNotFoundException::new); + } + + @Transactional + public List findAllPosts() { + return postRepository.findAll(); + } + + @Transactional + public Post addPost(String text, List comments, Long authorId) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Post data is null or empty"); + } + Profile author = profileRepository.findById(authorId).get(); + Post post = new Post(text, author, comments); + author.addPost(post); + profileRepository.save(author); + return postRepository.save(post); + } + + @Transactional + public Post updatePost(Long id, String text) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Post data is null or empty"); + } + final Post currentPost = findPost(id); + currentPost.setText(text); + final Profile author = currentPost.getAuthor(); + profileRepository.save(author); + return postRepository.save(currentPost); + } + + @Transactional + public Post deletePost(Long id) { + final Post currentPost = findPost(id); + postRepository.delete(currentPost); + return currentPost; + } + + @Transactional + public void deleteAllPosts() { + postRepository.deleteAll(); + } +} diff --git a/src/main/java/com/webproglabs/lab1/services/ProfileService.java b/src/main/java/com/webproglabs/lab1/lab34/services/ProfileService.java similarity index 56% rename from src/main/java/com/webproglabs/lab1/services/ProfileService.java rename to src/main/java/com/webproglabs/lab1/lab34/services/ProfileService.java index e3d7aef..933e9bb 100644 --- a/src/main/java/com/webproglabs/lab1/services/ProfileService.java +++ b/src/main/java/com/webproglabs/lab1/lab34/services/ProfileService.java @@ -1,70 +1,67 @@ -package com.webproglabs.lab1.services; +package com.webproglabs.lab1.lab34.services; -import com.webproglabs.lab1.model.Comment; -import com.webproglabs.lab1.model.Profile; +import com.webproglabs.lab1.lab34.model.Comment; +import com.webproglabs.lab1.lab34.model.Post; +import com.webproglabs.lab1.lab34.model.Profile; +import com.webproglabs.lab1.lab34.repository.CommentRepository; +import com.webproglabs.lab1.lab34.repository.ProfileRepository; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; -import javax.persistence.EntityManager; import javax.persistence.EntityNotFoundException; -import javax.persistence.PersistenceContext; import javax.transaction.Transactional; import java.util.List; +import java.util.Optional; @Service public class ProfileService { - @PersistenceContext - private EntityManager em; + + private final ProfileRepository profileRepository; + + public ProfileService(ProfileRepository profileRepository) { + this.profileRepository = profileRepository; + } @Transactional public Profile findUser(Long id) { - final Profile user = em.find(Profile.class, id); - if (user == null) { - throw new EntityNotFoundException(String.format("User with id [%s] is not found", id)); - } - return user; + final Optional profile = profileRepository.findById(id); + return profile.orElseThrow(EntityNotFoundException::new); } @Transactional public List findAllUsers() { - return em.createQuery("select a from Profile a", Profile.class).getResultList(); + return profileRepository.findAll(); } @Transactional - public Profile addUser(String login, String password, List comments) { + public Profile addUser(String login, String password, List comments, List posts) { if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) { throw new IllegalArgumentException("User data is null or empty"); } - final Profile user = new Profile(login, password, comments); - em.persist(user); - return user; + final Profile user = new Profile(login, password, comments, posts); + return profileRepository.save(user); } @Transactional - public Profile updateUser(Long id, String login, String password, List comments) { + public Profile updateUser(Long id, String login, String password) { if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) { throw new IllegalArgumentException("User data is null or empty"); } final Profile currentUser = findUser(id); currentUser.setLogin(login); currentUser.setPassword(password); - currentUser.getComments().clear(); - for (int i = 0; i < comments.size(); i++) { - currentUser.addComment(comments.get(i)); - em.merge(comments.get(i)); - } - return em.merge(currentUser); + return profileRepository.save(currentUser); } @Transactional public Profile deleteUser(Long id) { final Profile currentUser = findUser(id); - em.remove(currentUser); + profileRepository.delete(currentUser); return currentUser; } @Transactional public void deleteAllUsers() { - em.createQuery("delete from Profile").executeUpdate(); + profileRepository.deleteAll(); } } diff --git a/src/main/java/com/webproglabs/lab1/services/CommentService.java b/src/main/java/com/webproglabs/lab1/services/CommentService.java deleted file mode 100644 index 1a9cdab..0000000 --- a/src/main/java/com/webproglabs/lab1/services/CommentService.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.webproglabs.lab1.services; - -import com.webproglabs.lab1.model.Comment; -import com.webproglabs.lab1.model.Profile; -import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; - -import javax.persistence.EntityManager; -import javax.persistence.EntityNotFoundException; -import javax.persistence.PersistenceContext; -import javax.transaction.Transactional; -import java.util.List; - -@Service -public class CommentService { - @PersistenceContext - private EntityManager em; - - - @Transactional - public Comment findComment(Long id) { - final Comment comment = em.find(Comment.class, id); - if (comment == null) { - throw new EntityNotFoundException(String.format("Comment with id [%s] is not found", id)); - } - return comment; - } - - @Transactional - public List findAllComments() { - return em.createQuery("select a from Comment a", Comment.class).getResultList(); - } - - @Transactional - public Comment addComment(String text, Profile user) { - if (!StringUtils.hasText(text)) { - throw new IllegalArgumentException("Comment data is null or empty"); - } - Comment comment = new Comment(text, user); - user.addComment(comment); - em.merge(user); - em.persist(comment); - return comment; - } - - @Transactional - public Comment updateComment(Long id, String text, Profile user) { - if (!StringUtils.hasText(text)) { - throw new IllegalArgumentException("Comment data is null or empty"); - } - Comment currentComment = findComment(id); - currentComment.setText(text); - currentComment.setOwner(user); - em.merge(user); - return em.merge(currentComment); - } - - @Transactional - public Comment deleteComment(Long id) { - final Comment currentComment = findComment(id); - em.remove(currentComment); - return currentComment; - } - @Transactional - public void deleteAllComments() { - em.createQuery("delete from Comment").executeUpdate(); - } - -} diff --git a/src/main/java/com/webproglabs/lab1/services/FriendService.java b/src/main/java/com/webproglabs/lab1/services/FriendService.java deleted file mode 100644 index 832dfd3..0000000 --- a/src/main/java/com/webproglabs/lab1/services/FriendService.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.webproglabs.lab1.services; - -import com.webproglabs.lab1.model.Profile; -import org.springframework.stereotype.Service; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.transaction.Transactional; -import java.util.List; - -@Service -public class FriendService { - @PersistenceContext - private EntityManager em; - - - @Transactional - public List findProfileFriends(Profile user){ - return user.getFriends(); - } - - @Transactional - public Profile addFriend(Profile user, Profile friend){ - if (user.addFriend(friend)){ - em.merge(user); - em.merge(friend); - return friend; - } - return null; - } - - @Transactional - public Profile deleteFriend(Profile user, Profile friend) { - user.getFriends().remove(friend); - em.merge(user); - friend.getFriends().remove(user); - em.merge(friend); - return user; - } -} diff --git a/src/main/java/com/webproglabs/lab1/services/Lab3Service.java b/src/main/java/com/webproglabs/lab1/services/Lab3Service.java deleted file mode 100644 index 5839c1e..0000000 --- a/src/main/java/com/webproglabs/lab1/services/Lab3Service.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.webproglabs.lab1.services; - -import com.webproglabs.lab1.model.Comment; -import com.webproglabs.lab1.model.Profile; -import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; - -import javax.persistence.EntityManager; -import javax.persistence.EntityNotFoundException; -import javax.persistence.PersistenceContext; -import javax.transaction.Transactional; -import java.util.List; - -@Service -public class Lab3Service { - -} diff --git a/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java b/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java index 0913b43..ae10385 100644 --- a/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java +++ b/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java @@ -1,23 +1,10 @@ package com.webproglabs.lab1; -import com.webproglabs.lab1.model.Profile; -import com.webproglabs.lab1.services.CommentService; -import com.webproglabs.lab1.services.FriendService; -import com.webproglabs.lab1.services.ProfileService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import com.webproglabs.lab1.model.Comment; - -import javax.persistence.EntityNotFoundException; -import java.util.*; @SpringBootTest class Lab1ApplicationTests { - +/* private static final Logger log = LoggerFactory.getLogger(Lab1ApplicationTests.class); @Autowired @@ -193,5 +180,5 @@ class Lab1ApplicationTests { friendService.addFriend(user, friend); user = friendService.deleteFriend(user, friend); Assertions.assertEquals(friendService.findProfileFriends(user).size(), 0); - } + }*/ }