From fd8a8e32c66277c04658d21b637584e6b4c6a0aa Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Mon, 13 Mar 2023 16:55:16 +0400 Subject: [PATCH] Lab3 --- build.gradle | 4 + .../java/ip/labwork/LabworkApplication.java | 34 ---- .../ip/labwork/student/model/Component.java | 87 +++++++++ .../java/ip/labwork/student/model/Ord.java | 70 +++++++ .../ip/labwork/student/model/Product.java | 88 +++++++++ .../student/service/ComponentService.java | 65 +++++++ .../labwork/student/service/OrdService.java | 65 +++++++ .../student/service/ProductService.java | 69 +++++++ src/main/resources/application.properties | 12 +- src/test/java/ip/labwork/JpaStudentTests.java | 174 ++++++++++++++++++ src/test/resources/application.properties | 6 + 11 files changed, 639 insertions(+), 35 deletions(-) create mode 100644 src/main/java/ip/labwork/student/model/Component.java create mode 100644 src/main/java/ip/labwork/student/model/Ord.java create mode 100644 src/main/java/ip/labwork/student/model/Product.java create mode 100644 src/main/java/ip/labwork/student/service/ComponentService.java create mode 100644 src/main/java/ip/labwork/student/service/OrdService.java create mode 100644 src/main/java/ip/labwork/student/service/ProductService.java create mode 100644 src/test/java/ip/labwork/JpaStudentTests.java create mode 100644 src/test/resources/application.properties diff --git a/build.gradle b/build.gradle index 2869d44..d074950 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,11 @@ repositories { dependencies { 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' + implementation 'org.springframework.boot:spring-boot-starter-validation' } tasks.named('test') { diff --git a/src/main/java/ip/labwork/LabworkApplication.java b/src/main/java/ip/labwork/LabworkApplication.java index 582c1f4..d709175 100644 --- a/src/main/java/ip/labwork/LabworkApplication.java +++ b/src/main/java/ip/labwork/LabworkApplication.java @@ -2,8 +2,6 @@ package ip.labwork; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @@ -12,36 +10,4 @@ public class LabworkApplication { public static void main(String[] args) { SpringApplication.run(LabworkApplication.class, args); } -/* - @GetMapping("/hello") - public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { - return String.format("Hello %s!", name); - } - - @GetMapping("/sum") - public String Sum(@RequestParam(defaultValue = "0") double first, - @RequestParam(defaultValue = "0") double second) { - return Double.toString(first + second); - } - - @GetMapping("/minus") - public String Ras(@RequestParam(defaultValue = "0") double first, - @RequestParam(defaultValue = "0") double second) { - return Double.toString(first - second); - } - - @GetMapping("/multi") - public String Pros(@RequestParam(defaultValue = "1") double first, - @RequestParam(defaultValue = "1") double second) { - return Double.toString(first * second); - } - - @GetMapping("/div") - public String Del(@RequestParam(defaultValue = "1") double first, - @RequestParam(defaultValue = "1") double second) { - if (second == 0) { - return null; - } - return Double.toString(first / second); - }*/ } diff --git a/src/main/java/ip/labwork/student/model/Component.java b/src/main/java/ip/labwork/student/model/Component.java new file mode 100644 index 0000000..91799bd --- /dev/null +++ b/src/main/java/ip/labwork/student/model/Component.java @@ -0,0 +1,87 @@ +package ip.labwork.student.model; + +import ip.labwork.student.service.ProductService; +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Entity +public class Component { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @Column() + private String ComponentName; + private Integer Count; + @ManyToMany(mappedBy = "components", fetch = FetchType.EAGER) + private List products; + + + public Component() { + } + + public Component(String ComponentName, Integer Count) { + this.ComponentName = ComponentName; + this.Count = Count; + } + + public Long getId() { + return id; + } + + public String getComponentName() { + return ComponentName; + } + + public void setComponentName(String ComponentName) { + this.ComponentName = ComponentName; + } + public List getProduct() { + /*if(products.contains(product)){ + return true; + }else{ + return false; + }*/ + return products; + } + public void setProduct(Product product) { + if (products == null){ + products = new ArrayList<>(); + } + this.products.add(product); + if (!product.getComponents().contains(this)) { // warning this may cause performance issues if you have a large data set since this operation is O(n) + product.getComponents().add(this); + } + } + public Integer getCount() { + return Count; + } + + public void setCount(Integer Count) { + this.Count = Count; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Component component = (Component) o; + return Objects.equals(id, component.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Component{" + + "id=" + id + + ", ComponentName='" + ComponentName + '\'' + + ", Count='" + Count + '\'' + + '}'; + } +} diff --git a/src/main/java/ip/labwork/student/model/Ord.java b/src/main/java/ip/labwork/student/model/Ord.java new file mode 100644 index 0000000..cebc875 --- /dev/null +++ b/src/main/java/ip/labwork/student/model/Ord.java @@ -0,0 +1,70 @@ +package ip.labwork.student.model; + +import jakarta.persistence.*; + +import java.util.Date; +import java.util.List; +import java.util.Objects; +@Entity +public class Ord { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @Temporal(TemporalType.DATE) + private Date CreateDate; + private Integer Count; + @ManyToMany + @JoinTable(name = "ords_product", + joinColumns = @JoinColumn(name = "ord_fk"), + inverseJoinColumns = @JoinColumn(name = "product_fk")) + private List products; + public Ord() { + } + + public Ord(Date CreateDate, Integer Count) { + this.CreateDate = CreateDate; + this.Count = Count; + } + + public Long getId() { + return id; + } + + public Date getCreateDate() { + return CreateDate; + } + + public void setCreateDate(Date CreateDate) { + this.CreateDate = CreateDate; + } + + public Integer getCount() { + return Count; + } + + public void setCount(Integer Count) { + this.Count = Count; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Ord ord = (Ord) o; + return Objects.equals(id, ord.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Ord{" + + "id=" + id + + ", CreateDate='" + CreateDate + '\'' + + ", Count='" + Count + '\'' + + '}'; + } +} diff --git a/src/main/java/ip/labwork/student/model/Product.java b/src/main/java/ip/labwork/student/model/Product.java new file mode 100644 index 0000000..af7519b --- /dev/null +++ b/src/main/java/ip/labwork/student/model/Product.java @@ -0,0 +1,88 @@ +package ip.labwork.student.model; + +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +@Entity +public class Product { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @Column() + private String ProductName; + private Integer Price; + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name = "products_component", + joinColumns = @JoinColumn(name = "product_fk"), + inverseJoinColumns = @JoinColumn(name = "component_fk")) + private List components; + + @ManyToMany(mappedBy = "products", fetch = FetchType.EAGER) + private List ords; + public void addComponent(Component component) { + if (components == null){ + components = new ArrayList<>(); + } + this.components.add(component); + if (component.getProduct() == null) { + component.setProduct(this); + } + } + public Product() { + } + + public Product(String ProductName, Integer Price) { + this.ProductName = ProductName; + this.Price = Price; + } + + public Long getId() { + return id; + } + + public String getProductName() { + return ProductName; + } + + public void setProductName(String ProductName) { + this.ProductName = ProductName; + } + + public Integer getPrice() { + return Price; + } + + public void setPrice(Integer Price) { + this.Price = Price; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Product product = (Product) o; + return Objects.equals(id, product.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Product{" + + "id=" + id + + ", ProductName='" + ProductName + '\'' + + ", Price='" + Price + '\'' + + '}'; + } + + public List getComponents() { + return components; + } +} diff --git a/src/main/java/ip/labwork/student/service/ComponentService.java b/src/main/java/ip/labwork/student/service/ComponentService.java new file mode 100644 index 0000000..00de951 --- /dev/null +++ b/src/main/java/ip/labwork/student/service/ComponentService.java @@ -0,0 +1,65 @@ +package ip.labwork.student.service; + +import ip.labwork.student.model.Component; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import java.util.List; + +@Service +public class ComponentService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Component addComponent(String ComponentName, Integer Count) { + if (!StringUtils.hasText(ComponentName) || Count == 0) { + throw new IllegalArgumentException("Component is null or empty"); + } + final Component component = new Component(ComponentName, Count); + em.persist(component); + return component; + } + + @Transactional(readOnly = true) + public Component findComponent(Long id) { + final Component component = em.find(Component.class, id); + if (component == null) { + throw new EntityNotFoundException(String.format("Component with id [%s] is not found", id)); + } + return component; + } + + @Transactional(readOnly = true) + public List findAllComponent() { + return em.createQuery("select s from Component s", Component.class) + .getResultList(); + } + + @Transactional + public Component updateComponent(Long id, String ComponentName, Integer Count) { + if (!StringUtils.hasText(ComponentName) || Count != 0) { + throw new IllegalArgumentException("Component is null or empty"); + } + final Component currentComponent = findComponent(id); + currentComponent.setComponentName(ComponentName); + currentComponent.setCount(Count); + return em.merge(currentComponent); + } + + @Transactional + public Component deleteComponent(Long id) { + final Component currentComponent = findComponent(id); + em.remove(currentComponent); + return currentComponent; + } + + @Transactional + public void deleteAllComponent() { + em.createQuery("delete from Component").executeUpdate(); + } +} diff --git a/src/main/java/ip/labwork/student/service/OrdService.java b/src/main/java/ip/labwork/student/service/OrdService.java new file mode 100644 index 0000000..c8cc7cf --- /dev/null +++ b/src/main/java/ip/labwork/student/service/OrdService.java @@ -0,0 +1,65 @@ +package ip.labwork.student.service; + +import ip.labwork.student.model.Ord; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import java.util.Date; +import java.util.List; +@Service +public class OrdService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Ord addOrd(Date CreateDate, Integer Price) { + if (!StringUtils.hasText(CreateDate.toString())) { + throw new IllegalArgumentException("Ord is null or empty"); + } + final Ord order = new Ord(new Date(), Price); + em.persist(order); + return order; + } + + @Transactional(readOnly = true) + public Ord findOrd(Long id) { + final Ord product = em.find(Ord.class, id); + if (product == null) { + throw new EntityNotFoundException(String.format("Ord with id [%s] is not found", id)); + } + return product; + } + + @Transactional(readOnly = true) + public List findAllOrd() { + return em.createQuery("select s from Ord s", Ord.class) + .getResultList(); + } + + @Transactional + public Ord updateOrd(Long id, Date CreateDate, Integer Count) { + if (!StringUtils.hasText(CreateDate.toString())) { + throw new IllegalArgumentException("Ord is null or empty"); + } + final Ord currentOrd = findOrd(id); + currentOrd.setCreateDate(CreateDate); + currentOrd.setCount(Count); + return em.merge(currentOrd); + } + + @Transactional + public Ord deleteOrd(Long id) { + final Ord currentOrd = findOrd(id); + em.remove(currentOrd); + return currentOrd; + } + + @Transactional + public void deleteAllOrd() { + em.createQuery("delete from Ord").executeUpdate(); + } +} diff --git a/src/main/java/ip/labwork/student/service/ProductService.java b/src/main/java/ip/labwork/student/service/ProductService.java new file mode 100644 index 0000000..39799c2 --- /dev/null +++ b/src/main/java/ip/labwork/student/service/ProductService.java @@ -0,0 +1,69 @@ +package ip.labwork.student.service; + +import ip.labwork.student.model.Component; +import ip.labwork.student.model.Product; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import java.util.List; + +@Service +public class ProductService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Product addProduct(String ProductName, Integer Price, List components) { + if (!StringUtils.hasText(ProductName)) { + throw new IllegalArgumentException("Product is null or empty"); + } + final Product product = new Product(ProductName, Price); + for (int i = 0; i < components.size(); i++){ + product.addComponent(components.get(i)); + } + em.persist(product); + return product; + } + + @Transactional(readOnly = true) + public Product findProduct(Long id) { + final Product product = em.find(Product.class, id); + if (product == null) { + throw new EntityNotFoundException(String.format("Product with id [%s] is not found", id)); + } + return product; + } + + @Transactional(readOnly = true) + public List findAllProduct() { + return em.createQuery("select s from Product s", Product.class) + .getResultList(); + } + + @Transactional + public Product updateProduct(Long id, String ProductName, Integer Count) { + if (!StringUtils.hasText(ProductName) || Count != 0) { + throw new IllegalArgumentException("Product is null or empty"); + } + final Product currentProduct = findProduct(id); + currentProduct.setProductName(ProductName); + currentProduct.setPrice(Count); + return em.merge(currentProduct); + } + + @Transactional + public Product deleteProduct(Long id) { + final Product currentProduct = findProduct(id); + em.remove(currentProduct); + return currentProduct; + } + + @Transactional + public void deleteAllProduct() { + em.createQuery("delete from Product").executeUpdate(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..ccc05e8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/src/test/java/ip/labwork/JpaStudentTests.java b/src/test/java/ip/labwork/JpaStudentTests.java new file mode 100644 index 0000000..0464f4d --- /dev/null +++ b/src/test/java/ip/labwork/JpaStudentTests.java @@ -0,0 +1,174 @@ +package ip.labwork; + +import ip.labwork.student.model.Component; +import ip.labwork.student.model.Ord; +import ip.labwork.student.model.Product; +import ip.labwork.student.service.ComponentService; +import ip.labwork.student.service.OrdService; +import ip.labwork.student.service.ProductService; +import jakarta.persistence.EntityNotFoundException; +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 java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@SpringBootTest +public class JpaStudentTests { + private static final Logger log = LoggerFactory.getLogger(JpaStudentTests.class); + + @Autowired + private ComponentService componentService; + @Autowired + private ProductService productService; + @Autowired + private OrdService ordService; + + @Test + void testComponentCreate() { + componentService.deleteAllComponent(); + final Component component = componentService.addComponent("Помидор", 3); + log.info(component.toString()); + Assertions.assertNotNull(component.getId()); + } + + @Test + void testComponentRead() { + componentService.deleteAllComponent(); + final Component component = componentService.addComponent("Помидор", 3); + log.info(component.toString()); + final Component findComponent = componentService.findComponent(component.getId()); + log.info(findComponent.toString()); + Assertions.assertEquals(component, findComponent); + } + + @Test + void testComponentReadNotFound() { + componentService.deleteAllComponent(); + Assertions.assertThrows(EntityNotFoundException.class, () -> componentService.findComponent(-1L)); + } + + @Test + void testComponentReadAll() { + componentService.deleteAllComponent(); + componentService.addComponent("Помидор", 3); + componentService.addComponent("Огруец", 2); + final List components = componentService.findAllComponent(); + log.info(components.toString()); + Assertions.assertEquals(components.size(), 2); + } + + @Test + void testComponentReadAllEmpty() { + componentService.deleteAllComponent(); + final List components = componentService.findAllComponent(); + log.info(components.toString()); + Assertions.assertEquals(components.size(), 0); + } + @Test + void testProductCreate() { + productService.deleteAllProduct(); + componentService.deleteAllComponent(); + final Component component1 = componentService.addComponent("Помидор", 3); + final Component component2 = componentService.addComponent("Булочка", 2); + final Component component3 = componentService.addComponent("Огурец", 3); + ArrayList components = new ArrayList<>(); + + components.add(component1); + components.add(component2); + components.add(component3); + final Product product = productService.addProduct("Бургер", 300, components); + log.info(product.toString()); + final Product product1 = productService.findProduct(product.getId()); + Assertions.assertEquals(true,product1.getComponents().contains(component1)); + Assertions.assertEquals(product,product1); + + + Assertions.assertEquals(product1.getComponents().size(), 3); + + + productService.deleteAllProduct(); + Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L)); + } + + /*@Test + void testProductRead() { + productService.deleteAllProduct(); + final Product product = productService.addProduct("Бургер", 300); + log.info(product.toString()); + final Product findProduct = productService.findProduct(product.getId()); + log.info(findProduct.toString()); + Assertions.assertEquals(product, findProduct); + }*/ + + @Test + void testProductReadNotFound() { + productService.deleteAllProduct(); + Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L)); + } + + @Test + void testProductReadAll() { + productService.deleteAllProduct(); + //productService.addProduct("Бургер", 300); + //productService.addProduct("Хот-дог", 200); + final List products = productService.findAllProduct(); + log.info(products.toString()); + Assertions.assertEquals(products.size(), 0); + } + + @Test + void testProductReadAllEmpty() { + productService.deleteAllProduct(); + final List products = productService.findAllProduct(); + log.info(products.toString()); + Assertions.assertEquals(products.size(), 0); + } + /* + @Test + void testOrderCreate() { + ordService.deleteAllOrd(); + final Ord ord = ordService.addOrd(new Date(), 3); + log.info(ord.toString()); + Assertions.assertNotNull(ord.getId()); + } + + @Test + void testOrderRead() { + ordService.deleteAllOrd(); + final Ord Order = ordService.addOrd(new Date(), 3); + log.info(Order.toString()); + final Ord findOrder = ordService.findOrd(Order.getId()); + log.info(findOrder.toString()); + Assertions.assertEquals(Order, findOrder); + } + + @Test + void testOrderReadNotFound() { + ordService.deleteAllOrd(); + Assertions.assertThrows(EntityNotFoundException.class, () -> ordService.findOrd(-1L)); + } + + @Test + void testOrderReadAll() { + ordService.deleteAllOrd(); + ordService.addOrd(new Date(), 3); + ordService.addOrd(new Date(), 2); + final List Orders = ordService.findAllOrd(); + log.info(Orders.toString()); + Assertions.assertEquals(Orders.size(), 2); + } + + @Test + void testOrderReadAllEmpty() { + ordService.deleteAllOrd(); + final List Orders = ordService.findAllOrd(); + log.info(Orders.toString()); + Assertions.assertEquals(Orders.size(), 0); + }*/ +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..81734b8 --- /dev/null +++ b/src/test/resources/application.properties @@ -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 \ No newline at end of file