This commit is contained in:
Katerina881 2023-03-13 16:55:16 +04:00
parent ca392cb92b
commit fd8a8e32c6
11 changed files with 639 additions and 35 deletions

View File

@ -14,7 +14,11 @@ 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'
implementation 'org.springframework.boot:spring-boot-starter-validation'
} }
tasks.named('test') { tasks.named('test') {

View File

@ -2,8 +2,6 @@ package ip.labwork;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @SpringBootApplication
@ -12,36 +10,4 @@ public class LabworkApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(LabworkApplication.class, 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);
}*/
} }

View File

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

View File

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

View File

@ -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<Component> components;
@ManyToMany(mappedBy = "products", fetch = FetchType.EAGER)
private List<Ord> 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<Component> getComponents() {
return components;
}
}

View File

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

View File

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

View File

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

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

@ -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<Component> components = componentService.findAllComponent();
log.info(components.toString());
Assertions.assertEquals(components.size(), 2);
}
@Test
void testComponentReadAllEmpty() {
componentService.deleteAllComponent();
final List<Component> 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<Component> 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<Product> products = productService.findAllProduct();
log.info(products.toString());
Assertions.assertEquals(products.size(), 0);
}
@Test
void testProductReadAllEmpty() {
productService.deleteAllProduct();
final List<Product> 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<Ord> Orders = ordService.findAllOrd();
log.info(Orders.toString());
Assertions.assertEquals(Orders.size(), 2);
}
@Test
void testOrderReadAllEmpty() {
ordService.deleteAllOrd();
final List<Ord> Orders = ordService.findAllOrd();
log.info(Orders.toString());
Assertions.assertEquals(Orders.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