diff --git a/backend/ipLab/build.gradle b/backend/ipLab/build.gradle index 280d2a5..dec8f5c 100644 --- a/backend/ipLab/build.gradle +++ b/backend/ipLab/build.gradle @@ -22,6 +22,7 @@ dependencies { implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.hibernate.validator:hibernate-validator' } tasks.named('test') { diff --git a/backend/ipLab/data.mv.db b/backend/ipLab/data.mv.db index 5f57590..a4a777a 100644 Binary files a/backend/ipLab/data.mv.db and b/backend/ipLab/data.mv.db differ diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/CustomerController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/CustomerController.java new file mode 100644 index 0000000..1da68d3 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/CustomerController.java @@ -0,0 +1,56 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.CustomerDTO; +import com.example.ipLab.StoreDataBase.Model.Customer; +import com.example.ipLab.StoreDataBase.Service.CustomerService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/customer") +public class CustomerController { + private final CustomerService customerService; + + public CustomerController(CustomerService customerService){ + this.customerService = customerService; + } + + @GetMapping("/{id}") + public CustomerDTO getCustomer(@PathVariable Long id){ + return new CustomerDTO(customerService.getCustomer(id)); + } + + @GetMapping + public List getCustomers(){ + return customerService.getAllCustomers().stream() + .map(CustomerDTO::new) + .toList(); + } + + @PostMapping + public CustomerDTO createCustomer(@RequestParam("customerLastName") String customerLastName, + @RequestParam("customerFirstName") String customerFirstName, + @RequestParam("customerMiddleName") String customerMiddleName){ + final Customer customer = customerService.addCustomer(customerLastName, customerFirstName, customerMiddleName); + return new CustomerDTO(customer); + } + + @PutMapping("/{id}") + public CustomerDTO updateCustomer(@RequestParam("customerLastName") String customerLastName, + @RequestParam("customerFirstName") String customerFirstName, + @RequestParam("customerMiddleName") String customerMiddleName, + @PathVariable Long id){ + return new CustomerDTO(customerService.updateCustomer(id, customerLastName, customerFirstName, customerMiddleName)); + } + + @DeleteMapping("/{id}") + public CustomerDTO deleteCustomer(@PathVariable Long id){ + return new CustomerDTO(customerService.deleteCustomer(id)); + } + + @DeleteMapping() + public void deleteAllCustomers(){ + customerService.deleteAllCustomers(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/OrderedController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/OrderedController.java new file mode 100644 index 0000000..93e7da5 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/OrderedController.java @@ -0,0 +1,58 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.OrderedDTO; +import com.example.ipLab.StoreDataBase.Model.Ordered; +import com.example.ipLab.StoreDataBase.Service.CustomerService; +import com.example.ipLab.StoreDataBase.Service.OrderService; +import com.example.ipLab.StoreDataBase.Service.ProductService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +public class OrderedController { + private final OrderService orderedService; + private final ProductService productService; + private final CustomerService customerService; + + public OrderedController(OrderService orderedService, ProductService productService, CustomerService customerService){ + this.productService = productService; + this.customerService = customerService; + this.orderedService = orderedService; + } + + @GetMapping("/{id}") + public OrderedDTO getOrdered(@PathVariable Long id){ + return new OrderedDTO(orderedService.getOrder(id)); + } + + @GetMapping + public List getOrdereds(){ + return orderedService.getAllOrders().stream() + .map(OrderedDTO::new) + .toList(); + } + + @PostMapping + public OrderedDTO createOrdered(@RequestParam("productId") Long productId, + @RequestParam("customerId") Long customerId, + @RequestParam("quantity") Integer quantity){ + final Ordered ordered = orderedService.addOrder(productService.getProduct(productId), customerService.getCustomer(customerId), quantity); + return new OrderedDTO(ordered); + } + + @PutMapping("/{id}") + public OrderedDTO updateOrdered(@RequestParam("quantity") Integer quantity, + @PathVariable Long id){ + return new OrderedDTO(orderedService.updateOrder(id, quantity)); + } + + @DeleteMapping("/{id}") + public OrderedDTO deleteOrdered(@PathVariable Long id){ + return new OrderedDTO(orderedService.deleteOrder(id)); + } + + @DeleteMapping() + public void deleteAllOrdereds(){ + orderedService.deleteAllOrders(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/ProductController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/ProductController.java new file mode 100644 index 0000000..624a5c7 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/ProductController.java @@ -0,0 +1,52 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.ProductDTO; +import com.example.ipLab.StoreDataBase.Model.Product; +import com.example.ipLab.StoreDataBase.Service.ProductService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/product") +public class ProductController { + private final ProductService productService; + + public ProductController(ProductService productService){ + this.productService = productService; + } + + @GetMapping("/{id}") + public ProductDTO getProduct(@PathVariable Long id){ + return new ProductDTO(productService.getProduct(id)); + } + + @GetMapping + public List getProducts(){ + return productService.getAllProducts().stream() + .map(ProductDTO::new) + .toList(); + } + + @PostMapping + public ProductDTO createProduct(@RequestParam("productName") String productName){ + final Product product = productService.addProduct(productName); + return new ProductDTO(product); + } + + @PutMapping("/{id}") + public ProductDTO updateProduct(@RequestParam("productName") String productName, + @PathVariable Long id){ + return new ProductDTO(productService.updateProduct(id, productName)); + } + + @DeleteMapping("/{id}") + public ProductDTO deleteProduct(@PathVariable Long id){ + return new ProductDTO(productService.deleteProduct(id)); + } + + @DeleteMapping() + public void deleteAllProducts(){ + productService.deleteAllProducts(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/StoreController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/StoreController.java new file mode 100644 index 0000000..2d3373a --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/StoreController.java @@ -0,0 +1,62 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.CustomerDTO; +import com.example.ipLab.StoreDataBase.DTO.ProductDTO; +import com.example.ipLab.StoreDataBase.DTO.StoreDTO; +import com.example.ipLab.StoreDataBase.Model.Customer; +import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Service.CustomerService; +import com.example.ipLab.StoreDataBase.Service.StoreService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/store") +public class StoreController { + private final StoreService storeService; + + public StoreController(StoreService storeService){ + this.storeService = storeService; + } + + @GetMapping("/{id}") + public StoreDTO getStore(@PathVariable Long id){ + return new StoreDTO(storeService.getStore(id)); + } + + @GetMapping + public List getStores(){ + return storeService.getAllStores().stream() + .map(StoreDTO::new) + .toList(); + } + + @PostMapping + public StoreDTO createStore(@RequestParam("storeName") String storeName){ + final Store store = storeService.addStore(storeName); + return new StoreDTO(store); + } + + @PutMapping("/{id}") + public StoreDTO updateStore(@RequestParam("storeName") String storeName, + @PathVariable Long id){ + return new StoreDTO(storeService.updateStore(id, storeName)); + } + + @PutMapping("/{storeId}-{productId}") + public ProductDTO addProduct(@PathVariable Long storeId, + @PathVariable Long productId){ + return new ProductDTO(storeService.addProduct(storeId, productId)); + } + + @DeleteMapping("/{id}") + public StoreDTO deleteStore(@PathVariable Long id){ + return new StoreDTO(storeService.deleteStore(id)); + } + + @DeleteMapping() + public void deleteAllStores(){ + storeService.deleteAllStores(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/CustomerDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/CustomerDTO.java new file mode 100644 index 0000000..92ad085 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/CustomerDTO.java @@ -0,0 +1,41 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Customer; + +import java.util.List; + +public class CustomerDTO { + public final Long id; + public final String lastname; + public final String firstname; + public final String middleName; + public final List orders; + + public CustomerDTO(Customer customer){ + this.id = customer.getId(); + this.lastname = customer.getLastName(); + this.firstname = customer.getFirstName(); + this.middleName = customer.getMiddleName(); + this.orders = customer.getOrders().stream().map(OrderedDTO::new).toList(); + } + + public Long getId() { + return id; + } + + public String getLastname() { + return lastname; + } + + public String getFirstname() { + return firstname; + } + + public String getMiddleName() { + return middleName; + } + + public List getOrders() { + return orders; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/OrderedDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/OrderedDTO.java new file mode 100644 index 0000000..df6872c --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/OrderedDTO.java @@ -0,0 +1,33 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Ordered; + +public class OrderedDTO { + public final Long id; + public final int quantity; + public final ProductDTO product; + public final CustomerDTO customer; + + public OrderedDTO(Ordered ordered){ + this.id = ordered.getId(); + this.quantity = ordered.getQuantity(); + this.product = new ProductDTO(ordered.getProduct()); + this.customer = new CustomerDTO(ordered.getCustomer()); + } + + public Long getId() { + return id; + } + + public int getQuantity() { + return quantity; + } + + public ProductDTO getProduct() { + return product; + } + + public CustomerDTO getCustomer() { + return customer; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/ProductDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/ProductDTO.java new file mode 100644 index 0000000..bd8552a --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/ProductDTO.java @@ -0,0 +1,35 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Product; + +import java.util.List; + +public class ProductDTO { + public final Long id; + public final String name; + public final StoreDTO store; + public final List orders; + + public ProductDTO(Product product){ + this.id = product.getId(); + this.name = product.getName(); + this.store = new StoreDTO(product.getStore()); + this.orders = product.getOrders().stream().map(OrderedDTO::new).toList(); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public StoreDTO getStore() { + return store; + } + + public List getOrders() { + return orders; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/StoreDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/StoreDTO.java new file mode 100644 index 0000000..a03e101 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/StoreDTO.java @@ -0,0 +1,29 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Store; + +import java.util.List; + +public class StoreDTO { + public final Long id; + public final String storeName; + public final List products; + + public StoreDTO(Store store){ + this.id = store.getId(); + this.storeName = store.getStoreName(); + this.products = store.getProducts().stream().map(ProductDTO::new).toList(); + } + + public Long getId() { + return id; + } + + public String getStoreName() { + return storeName; + } + + public List getProducts() { + return products; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/CustomerNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/CustomerNotFoundException.java new file mode 100644 index 0000000..469aa78 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/CustomerNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class CustomerNotFoundException extends RuntimeException{ + public CustomerNotFoundException(Long id){ + super(String.format("Customer with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/OrderedNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/OrderedNotFoundException.java new file mode 100644 index 0000000..cc42154 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/OrderedNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class OrderedNotFoundException extends RuntimeException{ + public OrderedNotFoundException(Long id){ + super(String.format("Order with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/ProductNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/ProductNotFoundException.java new file mode 100644 index 0000000..0356019 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/ProductNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class ProductNotFoundException extends RuntimeException{ + public ProductNotFoundException(Long id){ + super(String.format("Product with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/StoreNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/StoreNotFoundException.java new file mode 100644 index 0000000..4ffdcfc --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/StoreNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class StoreNotFoundException extends RuntimeException{ + public StoreNotFoundException(Long id){ + super(String.format("Store with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java index 6605858..4743383 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java @@ -1,6 +1,7 @@ package com.example.ipLab.StoreDataBase.Model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; @@ -12,10 +13,13 @@ public class Customer { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column + @NotBlank(message = "Customer's last name can't be empty") private String lastName; @Column + @NotBlank(message = "Customer's first name can't be empty") private String firstName; @Column + @NotBlank(message = "Customer's middle name can't be empty") private String middleName; @OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL) private List orders; diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java index 653ebd9..0eeeb44 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java @@ -9,9 +9,6 @@ public class Ordered { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name="store_fk") - private Store store; - @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name="customer_fk") private Customer customer; @ManyToOne(fetch = FetchType.EAGER) @@ -28,9 +25,6 @@ public class Ordered { return id; } - public Store getStore() { - return store; - } public Customer getCustomer() { return customer; @@ -55,13 +49,6 @@ public class Ordered { this.quantity = quantity; } - public void setStore(Store store) { - this.store = store; - if (!store.getOrders().contains(this)){ - store.AddOrdered(this); - } - } - public void setCustomer(Customer customer) { this.customer = customer; if (!customer.getOrders().contains(this)){ diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java index 3136b10..fe0cac5 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java @@ -1,6 +1,7 @@ package com.example.ipLab.StoreDataBase.Model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; @@ -12,6 +13,7 @@ public class Product { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column + @NotBlank(message = "Product's name can't be empty") private String name; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "store_fk") diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java index b9ddadb..d3d7652 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java @@ -2,6 +2,7 @@ package com.example.ipLab.StoreDataBase.Model; import com.example.ipLab.StoreDataBase.Service.ProductService; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; @@ -13,15 +14,13 @@ public class Store { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column + @NotBlank(message = "Store's name can't be empty") private String storeName; @OneToMany(fetch = FetchType.EAGER, mappedBy = "store", cascade = CascadeType.ALL, orphanRemoval = true) private List products; - @OneToMany(fetch = FetchType.EAGER, mappedBy = "store", cascade = CascadeType.ALL) - private List orders; public Store(){} public Store(String storeName){ this.storeName = storeName; - this.orders = new ArrayList<>(); this.products = new ArrayList<>(); } @@ -33,10 +32,6 @@ public class Store { return storeName; } - public List getOrders() { - return orders; - } - public List getProducts() { return products; } @@ -46,12 +41,6 @@ public class Store { product.setStore(this); } } - public void AddOrdered(Ordered ordered){ - this.orders.add(ordered); - if (ordered.getStore() != this){ - ordered.setStore(this); - } - } public void setStoreName(String storeName) { this.storeName = storeName; diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/CustomerRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/CustomerRepository.java new file mode 100644 index 0000000..2858381 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/CustomerRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Customer; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CustomerRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/OrderedRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/OrderedRepository.java new file mode 100644 index 0000000..45a6129 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/OrderedRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Ordered; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface OrderedRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/ProductRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/ProductRepository.java new file mode 100644 index 0000000..47ea075 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/ProductRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Product; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProductRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/StoreRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/StoreRepository.java new file mode 100644 index 0000000..530be3c --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/StoreRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Store; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface StoreRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java index 084074c..a701a1f 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java @@ -1,6 +1,9 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.CustomerNotFoundException; import com.example.ipLab.StoreDataBase.Model.Customer; +import com.example.ipLab.StoreDataBase.Repositories.CustomerRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -12,53 +15,50 @@ import java.util.List; @Service public class CustomerService { - @PersistenceContext - private EntityManager em; + private final CustomerRepository customerRepository; + private final ValidatorUtil validatorUtil; + + public CustomerService(CustomerRepository customerRepository, + ValidatorUtil validatorUtil){ + this.customerRepository = customerRepository; + this.validatorUtil = validatorUtil; + } @Transactional public Customer addCustomer(String customerLastName, String customerFirstName, String customerMiddleName){ - if (!StringUtils.hasText(customerLastName) || !StringUtils.hasText(customerFirstName) || !StringUtils.hasText(customerMiddleName)){ - throw new IllegalArgumentException("Customer name is null or empty"); - } - final Customer customer = new Customer(customerLastName, customerFirstName, customerMiddleName); - em.persist(customer); - return customer; + Customer customer = new Customer(customerLastName, customerFirstName, customerMiddleName); + validatorUtil.validate(customer); + return customerRepository.save(customer); } @Transactional() public Customer getCustomer(Long id){ - Customer customer = em.find(Customer.class, id); - if (customer == null){ - throw new EntityNotFoundException(String.format("Customer with id = %s isn't found", id)); - } - return customer; + return customerRepository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id)); } @Transactional public List getAllCustomers(){ - return em.createQuery("SELECT c from Customer c", Customer.class).getResultList(); + return customerRepository.findAll(); } @Transactional public Customer updateCustomer(Long id, String customerLastName, String customerFirstName, String customerMiddleName){ - if (!StringUtils.hasText(customerLastName) || !StringUtils.hasText(customerFirstName) || !StringUtils.hasText(customerMiddleName)){ - throw new IllegalArgumentException("Customer name is null or empty"); - } - final Customer customer = getCustomer(id); + Customer customer = getCustomer(id); customer.setLastName(customerLastName); customer.setFirstName(customerFirstName); customer.setMiddleName(customerMiddleName); - return em.merge(customer); + validatorUtil.validate(customer); + return customerRepository.save(customer); } @Transactional public Customer deleteCustomer(Long id){ - final Customer customer = getCustomer(id); - em.remove(customer); + Customer customer = getCustomer(id); + customerRepository.delete(customer); return customer; } @Transactional public void deleteAllCustomers(){ - em.createQuery("delete from Customer"); + customerRepository.deleteAll(); } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java index b376f00..acbe26c 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java @@ -1,115 +1,81 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.OrderedNotFoundException; import com.example.ipLab.StoreDataBase.Model.Customer; import com.example.ipLab.StoreDataBase.Model.Ordered; import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Repositories.OrderedRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; +import jakarta.persistence.criteria.Order; import jakarta.transaction.Transactional; -import org.aspectj.weaver.ast.Or; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; -import java.util.ArrayList; import java.util.List; @Service public class OrderService { - @PersistenceContext - private EntityManager em; + private final OrderedRepository orderedRepository; + private final ValidatorUtil validatorUtil; + + public OrderService(OrderedRepository orderedRepository, + ValidatorUtil validatorUtil, + ProductService productService){ + this.productService = productService; + this.orderedRepository = orderedRepository; + this.validatorUtil = validatorUtil; + } private ProductService productService; - public OrderService(ProductService productService){ - this.productService = productService; - } - @Transactional - public Ordered addOrder(Store store, Product product, Customer customer, int quantity){ + public Ordered addOrder(Product product, Customer customer, int quantity){ final Ordered order = new Ordered(quantity); + validatorUtil.validate(order); product.AddOrdered(order); customer.AddOrdered(order); - store.AddOrdered(order); - em.persist(order); + orderedRepository.save(order); return order; } @Transactional() public Ordered getOrder(Long id){ - Ordered order = em.find(Ordered.class, id); - if (order == null){ - throw new EntityNotFoundException(String.format("Ordered with id = %s isn't found", id)); - } - return order; - } - @Transactional - public List getOrdersWithProduct(Long productId, int minQuantity, int maxQuantity){ - return em.createQuery("SELECT o FROM Ordered o WHERE o.product.id = ?1 AND o.quantity >= ?2 AND o.quantity <= ?3",Ordered.class) - .setParameter(1, productId) - .setParameter(2, minQuantity) - .setParameter(3, maxQuantity) - .getResultList(); + return orderedRepository.findById(id).orElseThrow(() -> new OrderedNotFoundException(id)); } @Transactional public List getAllOrders(){ - return em.createQuery("SELECT o FROM Ordered o", Ordered.class).getResultList(); + return orderedRepository.findAll(); } @Transactional public Ordered updateOrder(Long id, int quantity){ final Ordered order = getOrder(id); order.setQuantity(quantity); - return em.merge(order); + validatorUtil.validate(order); + return orderedRepository.save(order); } @Transactional public Ordered deleteOrder(Long id){ final Ordered order = getOrder(id); - order.getStore().getOrders().remove(order); order.getCustomer().getOrders().remove(order); - em.remove(order); + orderedRepository.delete(order); return order; } @Transactional public void deleteAllOrders(){ - em.createQuery("delete from Ordered").executeUpdate(); + orderedRepository.deleteAll(); } //product section @Transactional() - public Product getProduct(Long productId, Long orderId){ - Ordered order = em.find(Ordered.class, orderId); - if (order != null) { - return order.getProduct(); - } - else throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - - @Transactional - public Product updateProduct(Long orderId, Long productId, String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } - final Product product = getProduct(productId, orderId); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - product.setName(productName); - return em.merge(product); - } - - @Transactional - public Product deleteProduct(Long orderId, Long productId){ - final Product product = getProduct(productId, orderId); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - var order = product.getOrders().stream().filter(or -> or.getId() == orderId).findFirst(); - if (order.isPresent()) order.get().setProduct(null); - else throw new EntityNotFoundException(String.format("Order with id = %s isn't found", orderId)); - return product; + public Product getProduct(Long orderId){ + Ordered order = getOrder(orderId); + return order.getProduct(); } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java index 286aa54..748c713 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java @@ -1,9 +1,12 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.ProductNotFoundException; import com.example.ipLab.StoreDataBase.Model.Customer; import com.example.ipLab.StoreDataBase.Model.Ordered; import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Repositories.ProductRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -14,59 +17,49 @@ import org.springframework.util.StringUtils; import java.util.List; @Service public class ProductService { - @PersistenceContext - private EntityManager em; + private final ProductRepository productRepository; + private final ValidatorUtil validatorUtil; + + public ProductService(ProductRepository productRepository, ValidatorUtil validatorUtil){ + this.productRepository = productRepository; + this.validatorUtil = validatorUtil; + } @Transactional public Product addProduct(String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } final Product product = new Product(productName); - em.persist(product); + validatorUtil.validate(product); + productRepository.save(product); return product; } @Transactional() public Product getProduct(Long id){ - Product product = em.find(Product.class, id); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); - } - em.persist(product); - return product; + return productRepository.findById(id).orElseThrow(() -> new ProductNotFoundException(id)); } @Transactional public List getAllProducts(){ - return em.createQuery("SELECT p FROM Product p", Product.class).getResultList(); + return productRepository.findAll(); } @Transactional public Product updateProduct(Long id, String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } final Product product = getProduct(id); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); - } product.setName(productName); - return em.merge(product); + validatorUtil.validate(product); + return productRepository.save(product); } @Transactional public Product deleteProduct(Long id){ final Product product = getProduct(id); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); - } Store store = product.getStore(); if (store != null) store.getProducts().remove(product); - em.remove(product); + productRepository.delete(product); return product; } @Transactional public void deleteAllProducts(){ - em.createQuery("delete from Product"); + productRepository.deleteAll(); } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java index 63229c6..b1b1dd0 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java @@ -1,7 +1,10 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.StoreNotFoundException; import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Repositories.StoreRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -14,57 +17,51 @@ import java.util.List; @Service public class StoreService { - @PersistenceContext - private EntityManager em; + private final StoreRepository storeRepository; + private final ValidatorUtil validatorUtil; private ProductService productService; - public StoreService(ProductService productService){ + public StoreService(StoreRepository storeRepository, ValidatorUtil validatorUtil, ProductService productService){ + this.storeRepository = storeRepository; + this.validatorUtil = validatorUtil; this.productService = productService; } @Transactional public Store addStore(String storeName){ - if (!StringUtils.hasText(storeName)){ - throw new IllegalArgumentException("Store name is null or empty"); - } final Store store = new Store(storeName); - em.persist(store); + validatorUtil.validate(store); + storeRepository.save(store); return store; } @Transactional() public Store getStore(Long id){ - Store store = em.find(Store.class, id); - if (store == null){ - throw new EntityNotFoundException(String.format("Store with id = %s isn't found", id)); - } - return store; + return storeRepository.findById(id).orElseThrow(() -> new StoreNotFoundException(id)); } @Transactional public List getAllStores(){ - return em.createQuery("SELECT s FROM Store s", Store.class).getResultList(); + return storeRepository.findAll(); } @Transactional public Store updateStore(Long id, String storeName){ - if (!StringUtils.hasText(storeName)){ - throw new IllegalArgumentException("Store name is null or empty"); - } final Store store = getStore(id); store.setStoreName(storeName); - return em.merge(store); + validatorUtil.validate(store); + return storeRepository.save(store); } @Transactional public Store deleteStore(Long id){ final Store store = getStore(id); - em.remove(store); + storeRepository.delete(store); return store; } @Transactional public void deleteAllStores(){ - em.createQuery("delete from Store"); + storeRepository.deleteAll(); } //product section @@ -73,7 +70,7 @@ public class StoreService { Store store = getStore(storeId); Product product = productService.getProduct(productId); store.AddProduct(product); - em.persist(store); + storeRepository.save(store); return product; } @@ -98,7 +95,7 @@ public class StoreService { Store store = getStore(storeId); Product product = getProductFromStore(productId, storeId); store.getProducts().remove(product); - em.remove(product); + product.setStore(null); return product; } @Transactional @@ -109,7 +106,6 @@ public class StoreService { storeProducts) { pr.setStore(null); store.getProducts().remove(pr); - em.remove(pr); } } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/error/AdviceController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/error/AdviceController.java new file mode 100644 index 0000000..d9a5aa5 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/error/AdviceController.java @@ -0,0 +1,42 @@ +package com.example.ipLab.StoreDataBase.util.error; + +import com.example.ipLab.StoreDataBase.Exceptions.CustomerNotFoundException; +import com.example.ipLab.StoreDataBase.Exceptions.OrderedNotFoundException; +import com.example.ipLab.StoreDataBase.Exceptions.ProductNotFoundException; +import com.example.ipLab.StoreDataBase.Exceptions.StoreNotFoundException; +import com.example.ipLab.StoreDataBase.util.validation.ValidationException; +import org.springframework.context.support.DefaultMessageSourceResolvable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +import java.util.stream.Collectors; + +@ControllerAdvice +public class AdviceController { + @ExceptionHandler({ + CustomerNotFoundException.class, + OrderedNotFoundException.class, + ProductNotFoundException.class, + StoreNotFoundException.class + }) + public ResponseEntity handleException(Throwable e){ + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleBindException(MethodArgumentNotValidException e) { + final ValidationException validationException = new ValidationException( + e.getBindingResult().getAllErrors().stream() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .collect(Collectors.toSet())); + return handleException(validationException); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleUnknownException(Throwable e) { + e.printStackTrace(); + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidationException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidationException.java new file mode 100644 index 0000000..41bbcac --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidationException.java @@ -0,0 +1,9 @@ +package com.example.ipLab.StoreDataBase.util.validation; + +import java.util.Set; + +public class ValidationException extends RuntimeException{ + public ValidationException(Set errors){ + super(String.join("\n", errors)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidatorUtil.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidatorUtil.java new file mode 100644 index 0000000..2d9bb68 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidatorUtil.java @@ -0,0 +1,30 @@ +package com.example.ipLab.StoreDataBase.util.validation; + +import jakarta.validation.ConstraintViolation; +import jakarta.validation.Validation; +import jakarta.validation.Validator; +import jakarta.validation.ValidatorFactory; +import org.springframework.stereotype.Component; + +import java.util.Set; +import java.util.stream.Collectors; + +@Component +public class ValidatorUtil { + private final Validator validator; + + public ValidatorUtil(){ + try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()){ + this.validator = factory.getValidator(); + } + } + + public void validate(T object) { + final Set> errors = validator.validate(object); + if (!errors.isEmpty()) { + throw new ValidationException(errors.stream() + .map(ConstraintViolation::getMessage) + .collect(Collectors.toSet())); + } + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java deleted file mode 100644 index d0b8da2..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.ipLab.TypesCalc.Configuration; - -import com.example.ipLab.TypesCalc.domen.TypeCalcInteger; -import com.example.ipLab.TypesCalc.domen.TypeCalcString; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class CalcConfiguration { - private final Logger log = LoggerFactory.getLogger(CalcConfiguration.class); - - @Bean(value="int") - public TypeCalcInteger createTypeCalcInteger(){ - return new TypeCalcInteger(); - } - - @Bean(value="string") - public TypeCalcString createTypeCalcString(){ - return new TypeCalcString(); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java deleted file mode 100644 index 306cfc2..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.example.ipLab.TypesCalc.Service; - -import com.example.ipLab.TypesCalc.domen.TypeCalc; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Service; - -@Service -public class CalcService { - private final ApplicationContext applicationContext; - - public CalcService(ApplicationContext applicationContext){this.applicationContext = applicationContext;} - - public Object Sum(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Sum(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Sum(obj1, obj2); - } - public Object Dif(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Dif(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Dif(obj1, obj2); - } - public Object Multiply(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Multiply(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Multiply(obj1, obj2); - } - public Object Div(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Div(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Div(obj1, obj2); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java deleted file mode 100644 index 55decec..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.example.ipLab.TypesCalc.controller; - -import com.example.ipLab.TypesCalc.Service.CalcService; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class TypeCalcController { - private final CalcService calcService; - - public TypeCalcController(CalcService calcService) {this.calcService = calcService;} - - @GetMapping("/CalcSum") - public Object calcSum(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Sum(obj1, obj2, type); - } - - @GetMapping("/CalcDif") - public Object calcDif(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Dif(obj1, obj2, type); - } - - @GetMapping("/CalcMultiply") - public Object calcMultiply(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Multiply(obj1, obj2, type); - } - - @GetMapping("/CalcDiv") - public Object calcDiv(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Div(obj1, obj2, type); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java deleted file mode 100644 index b5f4038..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.ipLab.TypesCalc.domen; - -public interface TypeCalc { - T Sum(T obj1, T obj2); - T Dif(T obj1, T obj2); - T Multiply(T obj1, T obj2); - T Div(T obj1, T obj2); -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java deleted file mode 100644 index 4fe348d..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.example.ipLab.TypesCalc.domen; - -import org.springframework.stereotype.Component; - -public class TypeCalcInteger implements TypeCalc{ - @Override - public Integer Sum(Integer num1, Integer num2) { - return num1 + num2; - } - - @Override - public Integer Dif(Integer num1, Integer num2) { - return num1 - num2; - } - - @Override - public Integer Multiply(Integer num1, Integer num2) { - return num1 * num2; - } - - @Override - public Integer Div(Integer num1, Integer num2) { - try { - return num1 / num2; - } - catch (ArithmeticException ex){ - return 0; - } - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java deleted file mode 100644 index 969cecb..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.example.ipLab.TypesCalc.domen; - -import org.springframework.stereotype.Component; - -public class TypeCalcString implements TypeCalc { - @Override - public String Sum(String s1, String s2) { - return s1 + s2; - } - - @Override - public String Dif(String s1, String s2) { - String res = ""; - for (char c : s1.toCharArray()){ - boolean foundInOther = false; - for (int i = 0; i < s2.length(); i++){ - if (c == s2.charAt(i)) { - foundInOther = true; - break; - } - } - if (!foundInOther) res += c; - } - return res; - } - - @Override - public String Multiply(String s1, String s2) { - String res = ""; - for (char c : s1.toCharArray()){ - boolean foundInOther = false; - for (int i = 0; i < s2.length(); i++){ - if (c == s2.charAt(i)) { - foundInOther = true; - break; - } - } - if (foundInOther) res += c; - } - return res; - } - - @Override - public String Div(String s1, String s2) { - StringBuilder res = new StringBuilder(); - int maxLength = Integer.max(s1.length(), s2.length()); - for (int i = 0; i < maxLength; i++){ - if (i < s1.length()){ - res.append(s1.charAt(i)); - } - if (i < s2.length()){ - res.append(s2.charAt(i)); - } - } - return res.toString(); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java b/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java deleted file mode 100644 index 9c624de..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.example.ipLab.controllers; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class CalcController { - - @GetMapping("/second") - public int second(@RequestParam(value = "num") int num){ - return num*num; - } - - @GetMapping("/root") - public double root(@RequestParam(value = "num") int num){ - return Math.sqrt(num); - } - - @GetMapping("/fact") - public int fact(@RequestParam(value = "num") int num){ - int res = 1; - for (int i = 2; i <= num; i++) { - res *= i; - } - return res; - } - - @GetMapping("/digit") - public int digit(@RequestParam(value = "num") int num){ - if (num < 0) num *= -1; - int sum = 0; - while (num > 0) { - sum += num % 10; - num /= 10; - } - return sum; - } -}