Lab3 is done

This commit is contained in:
Katerina881 2023-03-07 12:31:12 +04:00
parent 8315a23855
commit 2096215195
16 changed files with 570 additions and 226 deletions

View File

@ -14,6 +14,9 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@ -1,50 +0,0 @@
package np.something.controllers;
import np.something.services.OperationsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MathController {
private final OperationsService operationsService;
public MathController(OperationsService operationsService) {
this.operationsService = operationsService;
}
@GetMapping("/sum")
public Object sum(
@RequestParam(value = "value1", defaultValue = "0") String value1,
@RequestParam(value = "value2", defaultValue = "0") String value2,
@RequestParam(value = "ops", defaultValue = "intops") String ops
) {
return operationsService.add(value1, value2, ops);
}
@GetMapping("/sub")
public Object sub(
@RequestParam(value = "value1", defaultValue = "0") String value1,
@RequestParam(value = "value2", defaultValue = "0") String value2,
@RequestParam(value = "ops", defaultValue = "intops") String ops
) {
return operationsService.sub(value1, value2, ops);
}
@GetMapping("/mul")
public Object mul(
@RequestParam(value = "value", defaultValue = "0") String value,
@RequestParam(value = "count", defaultValue = "0") int count,
@RequestParam(value = "ops", defaultValue = "intops") String ops
) {
return operationsService.mul(value, count, ops);
}
@GetMapping("/invert")
public Object div(
@RequestParam(value = "value", defaultValue = "0") String value,
@RequestParam(value = "ops", defaultValue = "intops") String ops
) {
return operationsService.invert(value, ops);
}
}

View File

@ -0,0 +1,68 @@
package np.something.model;
import jakarta.persistence.*;
import java.util.Objects;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String content;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="customer_fk")
private Customer customer;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="post_fk")
private Post post;
public Comment() {
}
public Comment(Customer customer, Post post, String content) {
this.customer = customer;
this.post = post;
this.content = content;
}
public Long getId() {
return id;
}
public Post getPost() {
return post;
}
public Customer getCustomer() {
return customer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@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);
}
}

View File

@ -0,0 +1,72 @@
package np.something.model;
import jakarta.persistence.*;
import java.util.*;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String hashedPassword;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
private List<Comment> comments;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
private List<Post> posts;
public Customer() {
}
public Customer(String username, String hashedPassword) {
this.username = username;
this.hashedPassword = hashedPassword;
this.comments = new ArrayList<>();
this.posts = new ArrayList<>();
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getHashedPassword() {
return hashedPassword;
}
public List<Comment> getComments() {
return comments;
}
public List<Post> getPosts() {
return posts;
}
public void setUsername(String username) {
this.username = username;
}
public void setHashedPassword(String hashedPassword) {
this.hashedPassword = hashedPassword;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Customer customer = (Customer) obj;
return Objects.equals(id, customer.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}

View File

@ -1,25 +0,0 @@
package np.something.model;
import org.springframework.stereotype.Component;
@Component(value = "intops")
public class IntegerOperations implements Operations<Integer> {
@Override
public Integer add(Integer first, Integer second) {
return first + second;
}
@Override
public Integer sub(Integer first, Integer second) {
return first - second;
}
@Override
public Integer mul(Integer value, int count) {
return value * count;
}
@Override
public Integer invert(Integer value) {
return ~value;
}
}

View File

@ -1,8 +0,0 @@
package np.something.model;
public interface Operations<T> {
T add(T first, T second);
T sub(T first, T second);
T mul(T value, int count);
T invert(T value);
}

View File

@ -0,0 +1,80 @@
package np.something.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String title;
@Column
private String content;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "customer_fk")
private Customer customer;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "post", cascade = CascadeType.ALL)
private List<Comment> comments;
public Post() {
}
public Post(Customer customer, String title, String content) {
this.customer = customer;
this.title = title;
this.content = content;
this.comments = new ArrayList<>();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Post post = (Post) o;
return id.equals(post.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<Comment> getComments() {
return comments;
}
public Customer getCustomer() {
return customer;
}
public Long getId() {
return id;
}
}

View File

@ -1,30 +0,0 @@
package np.something.model;
import org.springframework.stereotype.Component;
@Component(value = "stringops")
public class StringOperations implements Operations<String>{
@Override
public String add(String first, String second) {
return first + second;
}
@Override
public String sub(String first, String second) {
return first.replace(second, "");
}
@Override
public String mul(String value, int count) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < count; i++) {
result.append(value);
}
return result.toString();
}
@Override
public String invert(String value) {
return new StringBuilder(value).reverse().toString();
}
}

View File

@ -0,0 +1,72 @@
package np.something.services;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import np.something.model.Comment;
import np.something.model.Customer;
import np.something.model.Post;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
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 c from Comment c", Comment.class).getResultList();
}
@Transactional
public Comment addComment(Customer customer, Post post, String content) {
if (customer == null || post == null) {
throw new IllegalArgumentException("Invalid customer or post");
}
if (!StringUtils.hasText(content)) {
throw new IllegalArgumentException("Invalid comment's content");
}
final Comment comment = new Comment(customer, post, content);
comment.getCustomer().getComments().add(comment);
comment.getPost().getComments().add(comment);
em.persist(comment);
return comment;
}
@Transactional
public Comment updateComment(Long id, String content) {
if (!StringUtils.hasText(content)) {
throw new IllegalArgumentException("Comment's content is empty");
}
final Comment comment = findComment(id);
comment.setContent(content);
return em.merge(comment);
}
@Transactional
public Comment deleteComment(Long id) {
final Comment currentComment = findComment(id);
currentComment.getPost().getComments().remove(currentComment);
currentComment.getCustomer().getComments().remove(currentComment);
em.remove(currentComment);
return currentComment;
}
@Transactional
public void deleteAllComment() {
em.createQuery("delete from Comment").executeUpdate();
}
}

View File

@ -0,0 +1,64 @@
package np.something.services;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import np.something.model.Customer;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class CustomerService {
@PersistenceContext
private EntityManager em;
@Transactional
public Customer findCustomer(Long id) {
final Customer customer = em.find(Customer.class, id);
if (customer == null) {
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
}
return customer;
}
@Transactional
public List<Customer> findAllCustomers() {
return em.createQuery("select c from Customer c", Customer.class).getResultList();
}
@Transactional
public Customer addCustomer(String username, String password) {
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer's username or password is empty");
}
final Customer customer = new Customer(username, password);
em.persist(customer);
return customer;
}
@Transactional
public Customer updateCustomer(Long id, String username, String password) {
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer's username or password is empty");
}
final Customer customer = findCustomer(id);
customer.setUsername(username);
customer.setHashedPassword(password);
return em.merge(customer);
}
@Transactional
public Customer deleteCustomer(Long id) {
final Customer currentCustomer = findCustomer(id);
em.remove(currentCustomer);
return currentCustomer;
}
@Transactional
public void deleteAllCustomers() {
em.createQuery("delete from Customer").executeUpdate();
}
}

View File

@ -1,45 +0,0 @@
package np.something.services;
import np.something.model.Operations;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class OperationsService {
private final ApplicationContext applicationContext;
public OperationsService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object add(Object first, Object second, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.add(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()));
}
return operations.add(first, second);
}
public Object sub(Object first, Object second, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.sub(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()));
}
return operations.sub(first, second);
}
public Object mul(Object value, int count, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.mul(Integer.parseInt(value.toString()), count);
}
return operations.mul(value, count);
}
public Object invert(Object value, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.invert(Integer.parseInt(value.toString()));
}
return operations.invert(value);
}
}

View File

@ -0,0 +1,71 @@
package np.something.services;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import np.something.model.Comment;
import np.something.model.Customer;
import np.something.model.Post;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class PostService {
@PersistenceContext
private EntityManager em;
@Transactional
public Post findPost(Long id) {
final Post post = em.find(Post.class, id);
if (post == null) {
throw new EntityNotFoundException(String.format("Post with id [%s] is not found", id));
}
return post;
}
@Transactional
public List<Post> findAllPosts() {
return em.createQuery("select p from Post p", Post.class).getResultList();
}
@Transactional
public Post addPost(Customer customer, String title, String content) {
if (customer == null) {
throw new IllegalArgumentException("Invalid customer");
}
if (!StringUtils.hasText(title) | !StringUtils.hasText(content)) {
throw new IllegalArgumentException("Invalid post's content or title");
}
final Post post = new Post(customer, title, content);
post.getCustomer().getPosts().add(post);
em.persist(post);
return post;
}
@Transactional
public Post updatePost(Long id, String title, String content) {
if (!StringUtils.hasText(content) | !StringUtils.hasText(title)) {
throw new IllegalArgumentException("Post's content or title is empty");
}
final Post post = findPost(id);
post.setTitle(title);
post.setContent(content);
return em.merge(post);
}
@Transactional
public Post deletePost(Long id) {
final Post currentPost = findPost(id);
currentPost.getCustomer().getPosts().remove(currentPost);
em.remove(currentPost);
return currentPost;
}
@Transactional
public void deleteAllPosts() {
em.createQuery("delete from Post").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,67 +0,0 @@
package np.something;
import np.something.services.OperationsService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class OperationsServiceTests {
@Autowired
OperationsService operationsService;
@Test
void stringSum() {
Assertions.assertEquals("Hello, World!", operationsService.add("Hello,", " World!", "stringops"));
Assertions.assertEquals("Hello, World!", operationsService.add("", "Hello, World!", "stringops"));
}
@Test
void stringSub() {
Assertions.assertEquals("Яблк", operationsService.sub("Яблоко", "о", "stringops"));
Assertions.assertEquals("Тлвизор", operationsService.sub("Телевизор", "е", "stringops"));
Assertions.assertEquals("Rewte", operationsService.sub("Rewrite", "ri", "stringops"));
}
@Test
void stringMul() {
Assertions.assertEquals("ДеревоДерево", operationsService.mul("Дерево", 2, "stringops"));
Assertions.assertEquals("ЛоЛоЛо", operationsService.mul("Ло", 3, "stringops"));
Assertions.assertEquals("", operationsService.mul("Слово", 0, "stringops"));
}
@Test
void stringInvert() {
Assertions.assertEquals("оволС", operationsService.invert("Слово", "stringops"));
Assertions.assertEquals("droW", operationsService.invert("Word", "stringops"));
Assertions.assertEquals("", operationsService.invert("", "stringops"));
}
@Test
void intSum() {
Assertions.assertEquals(22, operationsService.add(11, 11, "intops"));
Assertions.assertEquals(-10, operationsService.add(15, -25, "intops"));
}
@Test
void intSub() {
Assertions.assertEquals(0, operationsService.sub(10, 10, "intops"));
Assertions.assertEquals(100, operationsService.sub(100, 0, "intops"));
Assertions.assertEquals(3, operationsService.sub(-3, -6, "intops"));
}
@Test
void intMul() {
Assertions.assertEquals(0, operationsService.mul(0, 10, "intops"));
Assertions.assertEquals(18, operationsService.mul(6, 3, "intops"));
Assertions.assertEquals(-4, operationsService.mul(-1, 4, "intops"));
}
@Test
void intInvert() {
Assertions.assertEquals(0, operationsService.invert(-1, "intops"));
Assertions.assertEquals(-2, operationsService.invert(1, "intops"));
Assertions.assertEquals(-3, operationsService.invert(2, "intops"));
}
}

View File

@ -0,0 +1,123 @@
package np.something;
import np.something.model.*;
import np.something.services.CommentService;
import np.something.services.CustomerService;
import np.something.services.PostService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SocialNetworkTest {
@Autowired
CustomerService customerService;
@Autowired
CommentService commentService;
@Autowired
PostService postService;
@Test
void testCustomers() {
commentService.deleteAllComment();
postService.deleteAllPosts();
customerService.deleteAllCustomers();
Customer c1 = customerService.addCustomer("first", "1");
Customer c2 = customerService.addCustomer("second", "2");
Customer c3 = customerService.addCustomer("third", "3");
Assertions.assertEquals("first", c1.getUsername());
Assertions.assertEquals("second", c2.getUsername());
Assertions.assertEquals("third", c3.getUsername());
Assertions.assertEquals(c1, customerService.findCustomer(c1.getId()));
customerService.deleteCustomer(c2.getId());
Assertions.assertEquals(2, customerService.findAllCustomers().size());
Customer c4 = customerService.updateCustomer(c3.getId(), "fourth", "4");
Assertions.assertNotEquals(c3.getUsername(), c4.getUsername());
Assertions.assertNotEquals(c3.getHashedPassword(), c4.getHashedPassword());
commentService.deleteAllComment();
postService.deleteAllPosts();
customerService.deleteAllCustomers();
}
@Test
void testPost() {
commentService.deleteAllComment();
postService.deleteAllPosts();
customerService.deleteAllCustomers();
Customer c1 = customerService.addCustomer("first", "1");
Customer c2 = customerService.addCustomer("second", "2");
Post p1 = postService.addPost(c1, "first title", "nonsense");
Post p2 = postService.addPost(c2, "second title", "ordinal");
Assertions.assertEquals(2, postService.findAllPosts().size());
Assertions.assertEquals(p1.getCustomer(), c1);
Assertions.assertEquals(p2.getCustomer(), c2);
Assertions.assertEquals(c1.getPosts().get(0), p1);
Assertions.assertEquals(c2.getPosts().get(0), p2);
Assertions.assertEquals(p1, postService.findPost(p1.getId()));
Assertions.assertEquals(p2, postService.findPost(p2.getId()));
Post p3 = postService.addPost(c1, "asdf", "asd");
postService.deletePost(p1.getId());
Assertions.assertEquals(1, customerService.findCustomer(c1.getId()).getPosts().size());
Post p4 = postService.updatePost(p2.getId(), "third title", "wow");
Assertions.assertNotEquals(p2.getTitle(), p4.getTitle());
Assertions.assertNotEquals(p2.getContent(), p4.getContent());
commentService.deleteAllComment();
postService.deleteAllPosts();
customerService.deleteAllCustomers();
}
@Test
void testComment() {
commentService.deleteAllComment();
postService.deleteAllPosts();
customerService.deleteAllCustomers();
Customer c1 = customerService.addCustomer("first", "1");
Customer c2 = customerService.addCustomer("second", "2");
Post p1 = postService.addPost(c1, "first title", "nonsense");
Post p2 = postService.addPost(c2, "second title", "ordinal");
Assertions.assertEquals(2, postService.findAllPosts().size());
Comment com1 = commentService.addComment(c1, p2, "What");
Comment com2 = commentService.addComment(c2, p1, "How");
Assertions.assertEquals(c1, p2.getComments().get(0).getCustomer());
Assertions.assertEquals(c2, p1.getComments().get(0).getCustomer());
Comment com3 = commentService.addComment(c1, p1, "Really");
Assertions.assertEquals(com2, commentService.findComment(p1.getComments().get(0).getId()));
Comment com4 = commentService.updateComment(com3.getId(), "Not really");
Assertions.assertNotEquals(com3.getContent(), com4.getContent());
Assertions.assertEquals(com3.getCustomer().getId(), com4.getCustomer().getId());
commentService.deleteAllComment();
postService.deleteAllPosts();
customerService.deleteAllCustomers();
}
}

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