From 62b2c3d4f08c8c02855c46140442366b22723443 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Thu, 30 Mar 2023 21:12:38 +0400 Subject: [PATCH] Lab4 no front --- build.gradle | 2 + data.mv.db | Bin 32768 -> 61440 bytes .../lab34/controller/CommentController.java | 51 ++++++++++ .../lab1/lab34/controller/CommentDto.java | 16 ++++ .../lab1/lab34/controller/PostController.java | 56 +++++++++++ .../lab1/lab34/controller/PostDto.java | 26 +++++ .../lab34/controller/ProfileController.java | 57 +++++++++++ .../lab1/lab34/controller/ProfileDto.java | 34 +++++++ .../lab1/{ => lab34}/model/Comment.java | 20 +++- .../webproglabs/lab1/lab34/model/Post.java | 90 ++++++++++++++++++ .../lab1/{ => lab34}/model/Profile.java | 35 ++++--- .../lab34/repository/CommentRepository.java | 7 ++ .../lab1/lab34/repository/PostRepository.java | 8 ++ .../lab34/repository/ProfileRepository.java | 7 ++ .../lab1/lab34/services/CommentService.java | 87 +++++++++++++++++ .../lab1/lab34/services/PostService.java | 72 ++++++++++++++ .../{ => lab34}/services/ProfileService.java | 49 +++++----- .../lab1/services/CommentService.java | 69 -------------- .../lab1/services/FriendService.java | 40 -------- .../lab1/services/Lab3Service.java | 17 ---- .../lab1/Lab1ApplicationTests.java | 17 +--- 21 files changed, 576 insertions(+), 184 deletions(-) create mode 100644 src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java rename src/main/java/com/webproglabs/lab1/{ => lab34}/model/Comment.java (70%) create mode 100644 src/main/java/com/webproglabs/lab1/lab34/model/Post.java rename src/main/java/com/webproglabs/lab1/{ => lab34}/model/Profile.java (75%) create mode 100644 src/main/java/com/webproglabs/lab1/lab34/repository/CommentRepository.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/repository/PostRepository.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/services/CommentService.java create mode 100644 src/main/java/com/webproglabs/lab1/lab34/services/PostService.java rename src/main/java/com/webproglabs/lab1/{ => lab34}/services/ProfileService.java (56%) delete mode 100644 src/main/java/com/webproglabs/lab1/services/CommentService.java delete mode 100644 src/main/java/com/webproglabs/lab1/services/FriendService.java delete mode 100644 src/main/java/com/webproglabs/lab1/services/Lab3Service.java 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 c13974193d7e7e843a87ae0b73097f184a2c8cdd..18776d2e6fe1e8c8035a98fe84a7cfe14dcc8c79 100644 GIT binary patch literal 61440 zcmeHQU2GiJb)H?4^6H1S?8J%@2kmBU*`iC+bLZ#I%(iH?S}w^ImrH7QDa$}nVt!@8 z6iHB&Z3RJLBtcW4Kpuj|K#L-UTNg-ywt47dUkvo60g9pl0wc*ofy55_(3b>7fEIn} znfo(;J45c0lugQ45I8?ObLY&t_ntZ5x#xa&sjkdAo7?Uib&YQOcfR7+ku1xz?(6rr z-l!Yath?*md%jmkCUzVRJAoq42HU%L?LD~pO@H^^o$amgx?t1ab6@v&>qv8LtOm|G zN&FKU5E>8~5E>8~5E>8~5E>8~5E>8~5E>8~5E?iE8dwte{{+;65J6}_Xh3K{Xh3K{ zXh3K{Xh3K{Xh3K{Xh3M-;c9>m@+%fg0#If*{jEBhy=(8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BN_~+4 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); - } + }*/ }