diff --git a/backend/lab/build.gradle b/backend/lab/build.gradle index c5a0cf0..329a229 100644 --- a/backend/lab/build.gradle +++ b/backend/lab/build.gradle @@ -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' } diff --git a/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Cart.java b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Cart.java new file mode 100644 index 0000000..f072d7f --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Cart.java @@ -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 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); + } +} diff --git a/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Customer.java b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Customer.java new file mode 100644 index 0000000..36e4145 --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Customer.java @@ -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); + } +} diff --git a/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Product.java b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Product.java new file mode 100644 index 0000000..2ebc73e --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/Product.java @@ -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 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); + } + +} diff --git a/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/ProductCategory.java b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/ProductCategory.java new file mode 100644 index 0000000..ed33e50 --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Models/ProductCategory.java @@ -0,0 +1,5 @@ +package com.example.lab.DataBaseLab3.Models; + +public class ProductCategory { + +} diff --git a/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Services/CustomerService.java b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Services/CustomerService.java new file mode 100644 index 0000000..95fbcf5 --- /dev/null +++ b/backend/lab/src/main/java/com/example/lab/DataBaseLab3/Services/CustomerService.java @@ -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 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"); + } +} \ No newline at end of file diff --git a/backend/lab/src/main/resources/application.properties b/backend/lab/src/main/resources/application.properties index 8b13789..4834c9a 100644 --- a/backend/lab/src/main/resources/application.properties +++ b/backend/lab/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=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 diff --git a/backend/lab/src/test/java/resources/application.properties b/backend/lab/src/test/java/resources/application.properties new file mode 100644 index 0000000..9485082 --- /dev/null +++ b/backend/lab/src/test/java/resources/application.properties @@ -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 \ No newline at end of file