Lab4 no front

This commit is contained in:
Данила Мочалов 2023-03-30 21:12:38 +04:00
parent ab0daa08b5
commit 62b2c3d4f0
21 changed files with 576 additions and 184 deletions

View File

@ -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') {

Binary file not shown.

View File

@ -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();
}
}

View File

@ -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;}
}

View File

@ -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();
}
}

View File

@ -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;}
}

View File

@ -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();
}
}

View File

@ -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;}
}

View File

@ -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() + '\'' +
'}';
}
}

View 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() + '\'' +
'}';
}
}

View File

@ -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<Comment> comments = new ArrayList<Comment>();
@OneToMany(mappedBy = "friends", fetch = FetchType.EAGER)
private List<Profile> friends = new ArrayList<>();
@OneToMany(mappedBy = "author", orphanRemoval = true, fetch = FetchType.EAGER)
private List<Post> posts = new ArrayList<Post>();
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.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<Comment> getComments() { return comments; }
public List<Post> getPosts() {return posts; }
public int getCommentsSize() {
if (comments == null) return 0;
else return comments.size();
}
public List<Profile> 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() + '\'' +
'}';
}
}

View File

@ -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> {
}

View File

@ -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> {
}

View File

@ -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> {
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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> profile = profileRepository.findById(id);
return profile.orElseThrow(EntityNotFoundException::new);
}
@Transactional
public List<Profile> findAllUsers() {
return em.createQuery("select a from Profile a", Profile.class).getResultList();
return profileRepository.findAll();
}
@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)) {
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<Comment> 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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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 {
}

View File

@ -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);
}
}*/
}