Lab4 no front
This commit is contained in:
parent
ab0daa08b5
commit
62b2c3d4f0
@ -17,6 +17,8 @@ dependencies {
|
|||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'com.h2database:h2:2.1.210'
|
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') {
|
tasks.named('test') {
|
||||||
|
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -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<CommentDto> 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();
|
||||||
|
}
|
||||||
|
}
|
@ -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;}
|
||||||
|
}
|
@ -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<PostDto> 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();
|
||||||
|
}
|
||||||
|
}
|
@ -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<CommentDto> 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<CommentDto> getComments() {return comments;}
|
||||||
|
}
|
@ -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<ProfileDto> 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();
|
||||||
|
}
|
||||||
|
}
|
@ -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<CommentDto> comments = new ArrayList<>();
|
||||||
|
|
||||||
|
private List<PostDto> 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<CommentDto> getComments() {return comments;}
|
||||||
|
public List<PostDto> getPosts() {return posts;}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.webproglabs.lab1.model;
|
package com.webproglabs.lab1.lab34.model;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -14,10 +14,14 @@ public class Comment {
|
|||||||
@ManyToOne()
|
@ManyToOne()
|
||||||
private Profile owner;
|
private Profile owner;
|
||||||
|
|
||||||
|
@ManyToOne()
|
||||||
|
private Post post;
|
||||||
|
|
||||||
public Comment(){};
|
public Comment(){};
|
||||||
public Comment(String text, Profile owner) {
|
public Comment(String text, Profile owner, Post post) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
|
this.post = post;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@ -36,12 +40,23 @@ public class Comment {
|
|||||||
return owner;
|
return owner;
|
||||||
}
|
}
|
||||||
public void setOwner(Profile owner) {
|
public void setOwner(Profile owner) {
|
||||||
|
this.owner.getComments().remove(this);
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
if (!owner.getComments().contains(this)) {
|
if (!owner.getComments().contains(this)) {
|
||||||
owner.getComments().add(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
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
@ -60,6 +75,7 @@ public class Comment {
|
|||||||
"id=" + id +
|
"id=" + id +
|
||||||
", text='" + text + '\'' +
|
", text='" + text + '\'' +
|
||||||
", owner='" + owner.ToString() + '\'' +
|
", owner='" + owner.ToString() + '\'' +
|
||||||
|
", post='" + post.ToString() + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
90
src/main/java/com/webproglabs/lab1/lab34/model/Post.java
Normal file
90
src/main/java/com/webproglabs/lab1/lab34/model/Post.java
Normal file
@ -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<Comment> comments = new ArrayList<Comment>();
|
||||||
|
|
||||||
|
@ManyToOne()
|
||||||
|
private Profile author;
|
||||||
|
|
||||||
|
public Post(){}
|
||||||
|
public Post(String text, Profile author, List<Comment> 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<Comment> 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() + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.webproglabs.lab1.model;
|
package com.webproglabs.lab1.lab34.model;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -18,16 +18,19 @@ public class Profile {
|
|||||||
@OneToMany(mappedBy = "owner", orphanRemoval = true, fetch = FetchType.EAGER)
|
@OneToMany(mappedBy = "owner", orphanRemoval = true, fetch = FetchType.EAGER)
|
||||||
private List<Comment> comments = new ArrayList<Comment>();
|
private List<Comment> comments = new ArrayList<Comment>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "friends", fetch = FetchType.EAGER)
|
@OneToMany(mappedBy = "author", orphanRemoval = true, fetch = FetchType.EAGER)
|
||||||
private List<Profile> friends = new ArrayList<>();
|
private List<Post> posts = new ArrayList<Post>();
|
||||||
|
|
||||||
public Profile(){}
|
public Profile(){}
|
||||||
public Profile(String login, String password, List<Comment> comments) {
|
public Profile(String login, String password, List<Comment> comments, List<Post> posts) {
|
||||||
this.login = login;
|
this.login = login;
|
||||||
this.password=password;
|
this.password=password;
|
||||||
for (int i = 0; i < comments.size(); i++) {
|
for (int i = 0; i < comments.size(); i++) {
|
||||||
addComment(comments.get(i));
|
addComment(comments.get(i));
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < posts.size(); i++) {
|
||||||
|
addPost(posts.get(i));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@ -52,22 +55,16 @@ public class Profile {
|
|||||||
|
|
||||||
public List<Comment> getComments() { return comments; }
|
public List<Comment> getComments() { return comments; }
|
||||||
|
|
||||||
|
public List<Post> getPosts() {return posts; }
|
||||||
|
|
||||||
public int getCommentsSize() {
|
public int getCommentsSize() {
|
||||||
if (comments == null) return 0;
|
if (comments == null) return 0;
|
||||||
else return comments.size();
|
else return comments.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Profile> getFriends(){
|
public int getPostsSize() {
|
||||||
return friends;
|
if (posts == null) return 0;
|
||||||
}
|
else return posts.size();
|
||||||
|
|
||||||
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 void addComment(Comment comment) {
|
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
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
@ -96,6 +100,7 @@ public class Profile {
|
|||||||
", login='" + login + '\'' +
|
", login='" + login + '\'' +
|
||||||
", password='" + password + '\'' +
|
", password='" + password + '\'' +
|
||||||
", comments='" + getCommentsSize() + '\'' +
|
", comments='" + getCommentsSize() + '\'' +
|
||||||
|
", comments='" + getPostsSize() + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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<Comment, Long> {
|
||||||
|
}
|
@ -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<Post, Long> {
|
||||||
|
}
|
@ -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<Profile, Long> {
|
||||||
|
}
|
@ -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> comment = commentRepository.findById(id);
|
||||||
|
return comment.orElseThrow(EntityNotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Comment> 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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> post = postRepository.findById(id);
|
||||||
|
return post.orElseThrow(EntityNotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Post> findAllPosts() {
|
||||||
|
return postRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post addPost(String text, List<Comment> 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();
|
||||||
|
}
|
||||||
|
}
|
@ -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.lab34.model.Comment;
|
||||||
import com.webproglabs.lab1.model.Profile;
|
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.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProfileService {
|
public class ProfileService {
|
||||||
@PersistenceContext
|
|
||||||
private EntityManager em;
|
private final ProfileRepository profileRepository;
|
||||||
|
|
||||||
|
public ProfileService(ProfileRepository profileRepository) {
|
||||||
|
this.profileRepository = profileRepository;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Profile findUser(Long id) {
|
public Profile findUser(Long id) {
|
||||||
final Profile user = em.find(Profile.class, id);
|
final Optional<Profile> profile = profileRepository.findById(id);
|
||||||
if (user == null) {
|
return profile.orElseThrow(EntityNotFoundException::new);
|
||||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<Profile> findAllUsers() {
|
public List<Profile> findAllUsers() {
|
||||||
return em.createQuery("select a from Profile a", Profile.class).getResultList();
|
return profileRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Profile addUser(String login, String password, List<Comment> comments) {
|
public Profile addUser(String login, String password, List<Comment> comments, List<Post> posts) {
|
||||||
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
|
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
|
||||||
throw new IllegalArgumentException("User data is null or empty");
|
throw new IllegalArgumentException("User data is null or empty");
|
||||||
}
|
}
|
||||||
final Profile user = new Profile(login, password, comments);
|
final Profile user = new Profile(login, password, comments, posts);
|
||||||
em.persist(user);
|
return profileRepository.save(user);
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Profile updateUser(Long id, String login, String password, List<Comment> comments) {
|
public Profile updateUser(Long id, String login, String password) {
|
||||||
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
|
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
|
||||||
throw new IllegalArgumentException("User data is null or empty");
|
throw new IllegalArgumentException("User data is null or empty");
|
||||||
}
|
}
|
||||||
final Profile currentUser = findUser(id);
|
final Profile currentUser = findUser(id);
|
||||||
currentUser.setLogin(login);
|
currentUser.setLogin(login);
|
||||||
currentUser.setPassword(password);
|
currentUser.setPassword(password);
|
||||||
currentUser.getComments().clear();
|
return profileRepository.save(currentUser);
|
||||||
for (int i = 0; i < comments.size(); i++) {
|
|
||||||
currentUser.addComment(comments.get(i));
|
|
||||||
em.merge(comments.get(i));
|
|
||||||
}
|
|
||||||
return em.merge(currentUser);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Profile deleteUser(Long id) {
|
public Profile deleteUser(Long id) {
|
||||||
final Profile currentUser = findUser(id);
|
final Profile currentUser = findUser(id);
|
||||||
em.remove(currentUser);
|
profileRepository.delete(currentUser);
|
||||||
return currentUser;
|
return currentUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAllUsers() {
|
public void deleteAllUsers() {
|
||||||
em.createQuery("delete from Profile").executeUpdate();
|
profileRepository.deleteAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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<Comment> 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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<Profile> 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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -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 {
|
|
||||||
|
|
||||||
}
|
|
@ -1,23 +1,10 @@
|
|||||||
package com.webproglabs.lab1;
|
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 org.springframework.boot.test.context.SpringBootTest;
|
||||||
import com.webproglabs.lab1.model.Comment;
|
|
||||||
|
|
||||||
import javax.persistence.EntityNotFoundException;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class Lab1ApplicationTests {
|
class Lab1ApplicationTests {
|
||||||
|
/*
|
||||||
private static final Logger log = LoggerFactory.getLogger(Lab1ApplicationTests.class);
|
private static final Logger log = LoggerFactory.getLogger(Lab1ApplicationTests.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -193,5 +180,5 @@ class Lab1ApplicationTests {
|
|||||||
friendService.addFriend(user, friend);
|
friendService.addFriend(user, friend);
|
||||||
user = friendService.deleteFriend(user, friend);
|
user = friendService.deleteFriend(user, friend);
|
||||||
Assertions.assertEquals(friendService.findProfileFriends(user).size(), 0);
|
Assertions.assertEquals(friendService.findProfileFriends(user).size(), 0);
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user