This commit is contained in:
Данила Мочалов 2023-03-20 20:07:29 +04:00
parent 2ba5860a70
commit ab0daa08b5
21 changed files with 4404 additions and 56 deletions

View File

@ -15,6 +15,8 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
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 'com.h2database:h2:2.1.210'
} }
tasks.named('test') { tasks.named('test') {

BIN
data.mv.db Normal file

Binary file not shown.

3828
data.trace.db Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,6 @@ package com.webproglabs.lab1;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @SpringBootApplication
public class Lab1Application { public class Lab1Application {

View File

@ -1,20 +0,0 @@
package com.webproglabs.lab1.config;
import com.webproglabs.lab1.domain.Lab2Int;
import com.webproglabs.lab1.domain.Lab2String;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Lab2Config {
@Bean(value="int")
public Lab2Int createLab2Int() {
return new Lab2Int();
}
@Bean(value="String")
public Lab2String createLab2String() {
return new Lab2String();
}
}

View File

@ -1,4 +1,4 @@
package com.webproglabs.lab1.controllers; package com.webproglabs.lab1.lab1;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;

View File

@ -0,0 +1,27 @@
package com.webproglabs.lab1.lab2.config;
import com.webproglabs.lab1.lab2.domain.Lab2Int;
import com.webproglabs.lab1.lab2.domain.Lab2String;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Lab2Config {
@Bean(value="int")
public Lab2Int createLab2Int() {
return new Lab2Int();
}
@Bean(value="String")
public Lab2String createLab2String() {
return new Lab2String();
}
}
//фронт на жсон сервер был когда то
//сделать на основе старых сущностей сущности в жпа
//делаем сущности, зависимость для жпа, сервис для круд операций,
//тесты
//фронт не нужно
//двунправленная связь один ко многим

View File

@ -1,6 +1,6 @@
package com.webproglabs.lab1.controllers; package com.webproglabs.lab1.lab2.controllers;
import com.webproglabs.lab1.services.Lab2Service; import com.webproglabs.lab1.lab2.Lab2Service;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;

View File

@ -1,6 +1,6 @@
package com.webproglabs.lab1.services; package com.webproglabs.lab1.lab2;
import com.webproglabs.lab1.domain.Lab2Interface; import com.webproglabs.lab1.lab2.domain.Lab2Interface;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,4 +1,4 @@
package com.webproglabs.lab1.domain; package com.webproglabs.lab1.lab2.domain;
public class Lab2Int implements Lab2Interface<Integer>{ public class Lab2Int implements Lab2Interface<Integer>{
// @Override // @Override

View File

@ -1,4 +1,4 @@
package com.webproglabs.lab1.domain; package com.webproglabs.lab1.lab2.domain;
public interface Lab2Interface<T> { public interface Lab2Interface<T> {
T sum(T first, T second); T sum(T first, T second);

View File

@ -1,4 +1,4 @@
package com.webproglabs.lab1.domain; package com.webproglabs.lab1.lab2.domain;
public class Lab2String implements Lab2Interface<String>{ public class Lab2String implements Lab2Interface<String>{
// @Override // @Override

View File

@ -0,0 +1,65 @@
package com.webproglabs.lab1.model;
import javax.persistence.*;
import java.util.Objects;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
@ManyToOne()
private Profile owner;
public Comment(){};
public Comment(String text, Profile owner) {
this.text = text;
this.owner = owner;
}
public Long getId() {
return id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Profile getOwner() {
return owner;
}
public void setOwner(Profile owner) {
this.owner = owner;
if (!owner.getComments().contains(this)) {
owner.getComments().add(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Comment comment = (Comment) o;
return Objects.equals(id, comment.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String ToString() {
return "Comment{" +
"id=" + id +
", text='" + text + '\'' +
", owner='" + owner.ToString() + '\'' +
'}';
}
}

View File

@ -0,0 +1,101 @@
package com.webproglabs.lab1.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name="tab_user")
public class Profile {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String login;
private String password;
@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<>();
public Profile(){}
public Profile(String login, String password, List<Comment> comments) {
this.login = login;
this.password=password;
for (int i = 0; i < comments.size(); i++) {
addComment(comments.get(i));
}
};
public Long getId() {
return id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Comment> getComments() { return comments; }
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 void addComment(Comment comment) {
this.comments.add(comment);
if (comment.getOwner() != this) {
comment.setOwner(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Profile user = (Profile) o;
return Objects.equals(id, user.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String ToString() {
return "User{" +
"id=" + id +
", login='" + login + '\'' +
", password='" + password + '\'' +
", comments='" + getCommentsSize() + '\'' +
'}';
}
}

View File

@ -0,0 +1,69 @@
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

@ -0,0 +1,40 @@
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

@ -0,0 +1,17 @@
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

@ -0,0 +1,70 @@
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 ProfileService {
@PersistenceContext
private EntityManager em;
@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;
}
@Transactional
public List<Profile> findAllUsers() {
return em.createQuery("select a from Profile a", Profile.class).getResultList();
}
@Transactional
public Profile addUser(String login, String password, List<Comment> comments) {
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;
}
@Transactional
public Profile updateUser(Long id, String login, String password, List<Comment> comments) {
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);
}
@Transactional
public Profile deleteUser(Long id) {
final Profile currentUser = findUser(id);
em.remove(currentUser);
return currentUser;
}
@Transactional
public void deleteAllUsers() {
em.createQuery("delete from Profile").executeUpdate();
}
}

View File

@ -1 +1,11 @@
spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

View File

@ -1,63 +1,197 @@
package com.webproglabs.lab1; package com.webproglabs.lab1;
import com.webproglabs.lab1.services.Lab2Service; 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.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 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);
@Autowired @Autowired
Lab2Service lab2Service; ProfileService profileService;
@Autowired
CommentService commentService;
@Autowired
FriendService friendService;
@Test @Test
void testSumInt() { void testUserCreate() {
final int res = (Integer)lab2Service.sum(3, 5, "int"); commentService.deleteAllComments();
Assertions.assertEquals(8, res); profileService.deleteAllUsers();
final Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.toString());
Assertions.assertNotNull(profileService.findUser(user.getId()));
} }
@Test @Test
void testSumString() { void testUserRead() {
final String res = (String)lab2Service.sum("Hello", "World", "String"); commentService.deleteAllComments();
Assertions.assertEquals("HelloWorld", res); profileService.deleteAllUsers();
final Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.toString());
final Profile findUser = profileService.findUser(user.getId());
log.info(findUser.toString());
Assertions.assertEquals(user, findUser);
} }
@Test @Test
void testBigInt() { void testUserReadNotFound() {
final int res = (Integer)lab2Service.makeItBigger(4, "int"); commentService.deleteAllComments();
Assertions.assertEquals(16, res); profileService.deleteAllUsers();
Assertions.assertThrows(EntityNotFoundException.class, () -> profileService.findUser(-1L));
} }
@Test @Test
void testBigString() { void testUserReadAll() {
final String res = (String)lab2Service.makeItBigger("hello", "String"); commentService.deleteAllComments();
Assertions.assertEquals("HELLO", res); profileService.deleteAllUsers();
profileService.addUser("Danya", "123", new ArrayList<>());
profileService.addUser("Vanya", "456", new ArrayList<>());
final List<Profile> users = profileService.findAllUsers();
log.info(users.toString());
Assertions.assertEquals(users.size(), 2);
} }
@Test @Test
void testReverseInt() { void testUserReadAllEmpty(){
final int res = (Integer)lab2Service.reverse(17, "int"); commentService.deleteAllComments();
Assertions.assertEquals(-17, res); profileService.deleteAllUsers();
final List<Profile> users = profileService.findAllUsers();
log.info(users.toString());
Assertions.assertEquals(users.size(), 0);
} }
@Test @Test
void testReverseString() { void testCommentCreate(){
final String res = (String)lab2Service.reverse("hello", "String"); commentService.deleteAllComments();
Assertions.assertEquals("olleh", res); profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Comment comment = commentService.addComment("Hello World", user);
log.info(comment.ToString());
Assertions.assertEquals(comment.getOwner(), user);
} }
@Test @Test
void testLenghtInt() { void testCommentRead(){
final int res = (Integer)lab2Service.lenght(17, "int"); commentService.deleteAllComments();
Assertions.assertEquals(17, res); profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Comment comment = commentService.addComment("Hello World", user);
log.info(comment.ToString());
Comment findComment = commentService.findComment(comment.getId());
log.info(findComment.ToString());
Assertions.assertEquals(comment, findComment);
} }
@Test @Test
void testLenghtString() { void testCommentReadNotFound(){
final String res = (String)lab2Service.lenght("hello", "String"); commentService.deleteAllComments();
Assertions.assertEquals("5", res); profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Comment comment = commentService.addComment("Hello World", user);
log.info(comment.ToString());
commentService.deleteAllComments();
Assertions.assertEquals(commentService.findAllComments().size(), 0);
} }
@Test
void testCommentsReadAll(){
commentService.deleteAllComments();
profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Comment comment1 = commentService.addComment("Hello World", user);
log.info(comment1.ToString());
Comment comment2 = commentService.addComment("Hello World", user);
log.info(comment2.ToString());
Assertions.assertEquals(commentService.findAllComments().size(), 2);
}
@Test
void testUserCommendsRead(){
commentService.deleteAllComments();
profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Comment comment1 = commentService.addComment("Hello World", user);
log.info(comment1.ToString());
Comment comment2 = commentService.addComment("Hello World", user);
log.info(comment2.ToString());
Assertions.assertEquals(profileService.findUser(user.getId()).getCommentsSize(), 2);
}
@Test
void testDeleteUserComment(){
commentService.deleteAllComments();
profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Comment comment1 = commentService.addComment("Hello World", user);
log.info(comment1.ToString());
Comment comment2 = commentService.addComment("Hello World", user);
log.info(comment2.ToString());
commentService.deleteComment(comment2.getId());
Assertions.assertEquals(profileService.findUser(user.getId()).getCommentsSize(), 1);
}
@Test
void testReadProfileFriend(){
commentService.deleteAllComments();
profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Profile friend = profileService.addUser("Friend", "245", new ArrayList<>());
log.info(friend.ToString());
friendService.addFriend(user, friend);
Assertions.assertEquals(user.getFriends().size(), 1);
}
@Test
void testAddProfileFriend(){
commentService.deleteAllComments();
profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Profile friend = profileService.addUser("Friend", "245", new ArrayList<>());
log.info(friend.ToString());
friendService.addFriend(user, friend);
Assertions.assertEquals(friendService.findProfileFriends(user).size(), 1);
}
@Test
void testDeleteProfileFriend(){
commentService.deleteAllComments();
profileService.deleteAllUsers();
Profile user = profileService.addUser("Danya", "123", new ArrayList<>());
log.info(user.ToString());
Profile friend = profileService.addUser("Friend", "245", new ArrayList<>());
log.info(friend.ToString());
friendService.addFriend(user, friend);
user = friendService.deleteFriend(user, friend);
Assertions.assertEquals(friendService.findProfileFriends(user).size(), 0);
}
} }

View File

@ -0,0 +1,6 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop