Третья лабораторная, сущности+ 1 сервис.
This commit is contained in:
parent
2712dcbfb0
commit
d883cf607a
@ -14,7 +14,12 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.testng:testng:7.1.0'
|
||||
|
||||
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'
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.example.lab.DataBaseLab3.Models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Cart {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@OneToOne(fetch = FetchType.EAGER, mappedBy = "cart", cascade = CascadeType.ALL)
|
||||
private Customer customer;
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "products", cascade = CascadeType.ALL)
|
||||
private List<Product> products;
|
||||
|
||||
public Cart() {
|
||||
this.products = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Cart cart = (Cart) obj;
|
||||
return Objects.equals(id, cart.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return Objects.hashCode(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.example.lab.DataBaseLab3.Models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Customer {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column
|
||||
private String firstName;
|
||||
@Column
|
||||
private String lastName;
|
||||
@Column
|
||||
private String customerAddress;
|
||||
|
||||
@OneToOne(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||
private Cart cart;
|
||||
|
||||
public Customer(){}
|
||||
|
||||
public Customer(String lastName, String firstName, String customerAddress) {
|
||||
this.lastName = lastName;
|
||||
this.firstName = firstName;
|
||||
this.customerAddress = customerAddress;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public String getCustomerAddress() {
|
||||
return customerAddress;
|
||||
}
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
public void setCustomerAddress(String customerAddress) {
|
||||
this.customerAddress = customerAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Customer customer = (Customer) obj;
|
||||
return Objects.equals(id, customer.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return Objects.hashCode(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.example.lab.DataBaseLab3.Models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String name;
|
||||
private float price;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "carts", cascade = CascadeType.ALL)
|
||||
private List<Cart> carts;
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "productCategory_fk")
|
||||
private ProductCategory productCategory;
|
||||
|
||||
public Product(){}
|
||||
|
||||
public Product(String name, float price){
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Product product = (Product) obj;
|
||||
return Objects.equals(id, product.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
return Objects.hashCode(id);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.lab.DataBaseLab3.Models;
|
||||
|
||||
public class ProductCategory {
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.example.lab.DataBaseLab3.Services;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import com.example.lab.DataBaseLab3.Models.Customer;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CustomerService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Customer addCustomer(String customerFirstName, String customerLastName, String customerAddress){
|
||||
if (!StringUtils.hasText(customerFirstName) || !StringUtils.hasText(customerLastName)){
|
||||
throw new IllegalArgumentException("Customer first and last names can't be null");
|
||||
}
|
||||
else if(!StringUtils.hasText(customerAddress)){
|
||||
throw new IllegalArgumentException("Customer address can't be null");
|
||||
}
|
||||
Customer customer = new Customer(customerLastName, customerFirstName, customerAddress);
|
||||
em.persist(customer);
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
public Customer getCustomer(Long id){
|
||||
Customer customer = em.find(Customer.class, id);
|
||||
if (customer == null){
|
||||
throw new EntityNotFoundException(String.format("Error: Customer id %s not found", id));
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Customer> getAllCustomers(){
|
||||
return em.createQuery("get c from Customer c", Customer.class).getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Customer updateCustomer(Long id, String customerLastName, String customerFirstName, String customerAddress){
|
||||
if (!StringUtils.hasText(customerLastName) || !StringUtils.hasText(customerFirstName)){
|
||||
throw new IllegalArgumentException("Customer last or/and first name is null");
|
||||
}
|
||||
if (!StringUtils.hasText(customerAddress)){
|
||||
throw new IllegalArgumentException("Customer address can't be null");
|
||||
}
|
||||
final Customer customer = getCustomer(id);
|
||||
customer.setLastName(customerLastName);
|
||||
customer.setFirstName(customerFirstName);
|
||||
customer.setCustomerAddress(customerAddress);
|
||||
return em.merge(customer);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Customer deleteCustomer(Long id){
|
||||
final Customer customer = getCustomer(id);
|
||||
em.remove(customer);
|
||||
return customer;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllCustomers(){
|
||||
em.createQuery("Delete from Customer");
|
||||
}
|
||||
}
|
@ -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=user
|
||||
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
|
||||
|
@ -0,0 +1,6 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=user
|
||||
spring.datasource.password=password
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
Loading…
Reference in New Issue
Block a user