From 13226e0fa9499aa4aaa3de95a7ff63ae4345b3d3 Mon Sep 17 00:00:00 2001 From: dasha Date: Mon, 13 Mar 2023 18:15:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B8=D1=87=D0=B5=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 5 + data.mv.db | Bin 0 -> 40960 bytes .../controller/CustomerController.java | 45 ++++++ .../labwork1/app/student/model/Customer.java | 73 +++++++++ .../com/labwork1/app/student/model/Order.java | 95 +++++++++++ .../labwork1/app/student/model/Ticket.java | 103 ++++++++++++ .../app/student/service/CustomerService.java | 65 ++++++++ .../app/student/service/OrderService.java | 68 ++++++++ .../app/student/service/TicketService.java | 65 ++++++++ src/main/resources/application.properties | 12 +- .../com/labwork1/app/JpaCustomerTests.java | 149 ++++++++++++++++++ src/test/resources/application.properties | 6 + 12 files changed, 685 insertions(+), 1 deletion(-) create mode 100644 data.mv.db create mode 100644 src/main/java/com/labwork1/app/student/controller/CustomerController.java create mode 100644 src/main/java/com/labwork1/app/student/model/Customer.java create mode 100644 src/main/java/com/labwork1/app/student/model/Order.java create mode 100644 src/main/java/com/labwork1/app/student/model/Ticket.java create mode 100644 src/main/java/com/labwork1/app/student/service/CustomerService.java create mode 100644 src/main/java/com/labwork1/app/student/service/OrderService.java create mode 100644 src/main/java/com/labwork1/app/student/service/TicketService.java create mode 100644 src/test/java/com/labwork1/app/JpaCustomerTests.java create mode 100644 src/test/resources/application.properties diff --git a/build.gradle b/build.gradle index 6e9b6dc..97d8b86 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,11 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'com.h2database:h2:2.1.210' + implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/data.mv.db b/data.mv.db new file mode 100644 index 0000000000000000000000000000000000000000..bc80d1b8773734b3d3c479a313b4983bd81393d0 GIT binary patch literal 40960 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 literal 0 HcmV?d00001 diff --git a/src/main/java/com/labwork1/app/student/controller/CustomerController.java b/src/main/java/com/labwork1/app/student/controller/CustomerController.java new file mode 100644 index 0000000..163312c --- /dev/null +++ b/src/main/java/com/labwork1/app/student/controller/CustomerController.java @@ -0,0 +1,45 @@ +package com.labwork1.app.student.controller; + +import com.labwork1.app.student.model.Customer; +import com.labwork1.app.student.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 studentService) { + this.customerService = studentService; + } + + @GetMapping("/{id}") + public Customer getCustomer(@PathVariable Long id) { + return customerService.findCustomer(id); + } + + @GetMapping("/") + public List gesCustomers() { + return customerService.findAllCustomers(); + } + + @PostMapping("/") + public Customer createCustomer(@RequestParam("login") String login, + @RequestParam("password") String password) { + return customerService.addCustomer(login, password); + } + + @PatchMapping("/{id}") + public Customer updateStudent(@PathVariable Long id, + @RequestParam("login") String login, + @RequestParam("password") String password) { + return customerService.updateCustomer(id, login, password); + } + + @DeleteMapping("/{id}") + public Customer deleteCustomer(@PathVariable Long id) { + return customerService.deleteCustomer(id); + } +} \ No newline at end of file diff --git a/src/main/java/com/labwork1/app/student/model/Customer.java b/src/main/java/com/labwork1/app/student/model/Customer.java new file mode 100644 index 0000000..2d366b5 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/Customer.java @@ -0,0 +1,73 @@ +package com.labwork1.app.student.model; + +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Entity +public class Customer { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @Column + private String login; + @Column + private String password; + @OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL) + private List orders; + + public Customer() { + } + + public Customer(String firstName, String password) { + this.login = firstName; + this.password = password; + this.orders = new ArrayList<>(); + } + public List getOrders() { + return orders; + } + public Long getId() { + return id; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Customer customer = (Customer) o; + return Objects.equals(id, customer.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Customer{" + + "id=" + id + + ", login='" + login + '\'' + + ", password='" + password + '\'' + + '}'; + } +} diff --git a/src/main/java/com/labwork1/app/student/model/Order.java b/src/main/java/com/labwork1/app/student/model/Order.java new file mode 100644 index 0000000..fe07af9 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/Order.java @@ -0,0 +1,95 @@ +package com.labwork1.app.student.model; + +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Objects; + +@Entity +@Table(name = "tab_order") +public class Order { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @Column + @Temporal(TemporalType.DATE) + private Date dateOfPurchase; + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name="customer_fk") + private Customer customer; + @ManyToMany(mappedBy = "orders") + private List tickets; + public Order(Customer customer, Date dateOfPurchase) { + this.customer = customer; + this.dateOfPurchase = dateOfPurchase; + this.tickets = new ArrayList<>(); + } + public void addTicket(Ticket ticket) { + if (tickets == null){ + tickets = new ArrayList<>(); + } + this.tickets.add(ticket); + if (ticket.getOrders() == null) { + ticket.setOrder(this); + } + } + public Order() { + + } + + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public Date getDateOfPurchase() { + return dateOfPurchase; + } + + public void setDateOfPurchase(Date dateOfPurchase) { + this.dateOfPurchase = dateOfPurchase; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public List getTickets() { + return tickets; + } + + public void setTickets(List tickets) { + this.tickets = tickets; + } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Order customer = (Order) o; + return Objects.equals(id, customer.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "OrderItem={" + + "id=" + id + + ", date='" + dateOfPurchase.toString() + '\'' + + ", customer='" + customer.toString() + '\'' + + ", tickets='" + tickets.toString() + '\'' + + '}'; + } +} diff --git a/src/main/java/com/labwork1/app/student/model/Ticket.java b/src/main/java/com/labwork1/app/student/model/Ticket.java new file mode 100644 index 0000000..c69d1f6 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/model/Ticket.java @@ -0,0 +1,103 @@ +package com.labwork1.app.student.model; + +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Objects; + +@Entity +public class Ticket { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @Column + private String name; + @Column + private Double price; + @Column + @Temporal(TemporalType.TIMESTAMP) + private Date date; + @ManyToMany + @JoinTable(name = "tickets_orders", + joinColumns = @JoinColumn(name = "ticket_fk"), + inverseJoinColumns = @JoinColumn(name = "order_fk")) + private List orders; + public Ticket(String name, Double price, Date date) { + this.name = name; + this.price = price; + this.date = date; + this.orders = new ArrayList<>(); + } + public void setOrder(Order order) { + if (orders == null){ + orders = new ArrayList<>(); + } + this.orders.add(order); + if (!order.getTickets().contains(this)) { + order.getTickets().add(this); + } + } + public Ticket() { + + } + + public List getOrders() { + return orders; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Ticket customer = (Ticket) o; + return Objects.equals(id, customer.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Ticket={" + + "id=" + id + + ", name='" + name + '\'' + + ", price='" + price + '\'' + + ", date='" + date.toString() + '\'' + + '}'; + } +} diff --git a/src/main/java/com/labwork1/app/student/service/CustomerService.java b/src/main/java/com/labwork1/app/student/service/CustomerService.java new file mode 100644 index 0000000..a749406 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/service/CustomerService.java @@ -0,0 +1,65 @@ +package com.labwork1.app.student.service; + +import com.labwork1.app.student.model.Customer; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import java.util.List; + +@Service +public class CustomerService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Customer addCustomer(String login, String password) { + if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) { + throw new IllegalArgumentException("Customer login/password is null or empty"); + } + final Customer customer = new Customer(login, password); + em.persist(customer); + return customer; + } + + @Transactional(readOnly = true) + public Customer findCustomer(Long id) { + final Customer customer = em.find(Customer.class, id); + if (customer == null) { + throw new EntityNotFoundException(String.format("Customer with id [%s] is not found", id)); + } + return customer; + } + + @Transactional(readOnly = true) + public List findAllCustomers() { + return em.createQuery("select s from Customer s", Customer.class) + .getResultList(); + } + + @Transactional + public Customer updateCustomer(Long id, String login, String password) { + if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) { + throw new IllegalArgumentException("Customer login/password is null or empty"); + } + final Customer currentStudent = findCustomer(id); + currentStudent.setLogin(login); + currentStudent.setPassword(password); + return em.merge(currentStudent); + } + + @Transactional + public Customer deleteCustomer(Long id) { + final Customer currentCustomer = findCustomer(id); + em.remove(currentCustomer); + return currentCustomer; + } + + @Transactional + public void deleteAllCustomers() { + em.createQuery("delete from Customer").executeUpdate(); + } +} diff --git a/src/main/java/com/labwork1/app/student/service/OrderService.java b/src/main/java/com/labwork1/app/student/service/OrderService.java new file mode 100644 index 0000000..d59879a --- /dev/null +++ b/src/main/java/com/labwork1/app/student/service/OrderService.java @@ -0,0 +1,68 @@ +package com.labwork1.app.student.service; + +import com.labwork1.app.student.model.Customer; +import com.labwork1.app.student.model.Order; +import com.labwork1.app.student.model.Ticket; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; + +@Service +public class OrderService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Order addOrder(Customer customer, Date dateOfPurchase) { + if (customer==null || dateOfPurchase==null ) { + throw new IllegalArgumentException("addOrder empty fields"); + } + final Order order = new Order(customer, dateOfPurchase); + em.persist(order); + return order; + } + + @Transactional(readOnly = true) + public Order findOrder(Long id) { + final Order order = em.find(Order.class, id); + if (order == null) { + throw new EntityNotFoundException(String.format("OrderItem with id [%s] is not found", id)); + } + return order; + } + + @Transactional(readOnly = true) + public List findAllOrders() { + return em.createQuery("select s from Order s", Order.class) + .getResultList(); + } + + @Transactional + public Order updateOrder(Long id, Date dateOfPurchase, List tickets) { + if (id==null || dateOfPurchase==null || tickets==null) { + throw new IllegalArgumentException("updateOrder empty fields"); + } + final Order currentStudent = findOrder(id); + currentStudent.setDateOfPurchase(dateOfPurchase); + currentStudent.setTickets(tickets); + return em.merge(currentStudent); + } + + @Transactional + public Order deleteOrder(Long id) { + final Order currentCustomer = findOrder(id); + em.remove(currentCustomer); + return currentCustomer; + } + + @Transactional + public void deleteAllOrders() { + em.createQuery("delete from Order").executeUpdate(); + } + +} diff --git a/src/main/java/com/labwork1/app/student/service/TicketService.java b/src/main/java/com/labwork1/app/student/service/TicketService.java new file mode 100644 index 0000000..0885fa5 --- /dev/null +++ b/src/main/java/com/labwork1/app/student/service/TicketService.java @@ -0,0 +1,65 @@ +package com.labwork1.app.student.service; + +import com.labwork1.app.student.model.Ticket; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; + +@Service +public class TicketService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Ticket addTicket(String name, Double price, Date date) { + if (name==null|| name.equals("") || price==null || price<=0 || date==null) { + throw new IllegalArgumentException("addTicket empty fields"); + } + final Ticket ticket = new Ticket(name, price, date); + em.persist(ticket); + return ticket; + } + + @Transactional(readOnly = true) + public Ticket findTicket(Long id) { + final Ticket ticket = em.find(Ticket.class, id); + if (ticket == null) { + throw new EntityNotFoundException(String.format("Ticket with id [%s] is not found", id)); + } + return ticket; + } + + @Transactional(readOnly = true) + public List findAllTickets() { + return em.createQuery("select s from Ticket s", Ticket.class) + .getResultList(); + } + + @Transactional + public Ticket updateTicket(Long id, String name, Double price) { + if (id==null || name==null || name.equals("") || price == null || price==0) { + throw new IllegalArgumentException("updateTicket empty fields"); + } + final Ticket currentStudent = findTicket(id); + currentStudent.setName(name); + currentStudent.setPrice(price); + return em.merge(currentStudent); + } + + @Transactional + public Ticket deleteTicket(Long id) { + final Ticket currentCustomer = findTicket(id); + em.remove(currentCustomer); + return currentCustomer; + } + + @Transactional + public void deleteAllTickets() { + em.createQuery("delete from Ticket ").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/com/labwork1/app/JpaCustomerTests.java b/src/test/java/com/labwork1/app/JpaCustomerTests.java new file mode 100644 index 0000000..82d79b8 --- /dev/null +++ b/src/test/java/com/labwork1/app/JpaCustomerTests.java @@ -0,0 +1,149 @@ +package com.labwork1.app; + +import com.labwork1.app.student.model.Customer; +import com.labwork1.app.student.model.Order; +import com.labwork1.app.student.model.Ticket; +import com.labwork1.app.student.service.CustomerService; +import com.labwork1.app.student.service.OrderService; +import com.labwork1.app.student.service.TicketService; +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.Date; +import java.util.List; + +@SpringBootTest +public class JpaCustomerTests { + private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class); + + @Autowired + private CustomerService customerService; + @Autowired + private TicketService ticketService; + @Autowired + private OrderService orderService; + + @Test + void testCustomerCreate() { + customerService.deleteAllCustomers(); + final Customer customer = customerService.addCustomer("Иван", "Иванов"); + log.info(customer.toString()+"testCustomerCreate"); + Assertions.assertNotNull(customer.getId()); + } + + @Test + void testCustomerRead() { + customerService.deleteAllCustomers(); + final Customer customer = customerService.addCustomer("Иван", "Иванов"); + log.info(customer.toString()+"testCustomerRead1"); + final Customer findCustomer = customerService.findCustomer(customer.getId()); + log.info(findCustomer.toString()+"testCustomerRead2"); + Assertions.assertEquals(customer, findCustomer); + } + + @Test + void testCustomerReadNotFound() { + customerService.deleteAllCustomers(); + Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(-1L)); + log.info("testCustomerReadNotFound"); + } + + @Test + void testCustomerReadAll() { + customerService.deleteAllCustomers(); + customerService.addCustomer("Иван", "Иванов"); + customerService.addCustomer("Петр", "Петров"); + final List customers = customerService.findAllCustomers(); + log.info(customers.toString()+"testCustomerReadAll"); + Assertions.assertEquals(customers.size(), 2); + } + + @Test + void testCustomerReadAllEmpty() { + customerService.deleteAllCustomers(); + final List customers = customerService.findAllCustomers(); + log.info(customers.toString()+"testCustomerReadAllEmpty"); + Assertions.assertEquals(customers.size(), 0); + } + + @Test + void testTicketCreate() { + ticketService.deleteAllTickets(); + final Ticket ticket = ticketService.addTicket("Первый", 100.0, new Date()); + log.info(ticket.toString()+"testTicketCreate"); + Assertions.assertNotNull(ticket.getId()); + } + + @Test + void testTicketRead() { + ticketService.deleteAllTickets(); + final Ticket ticket = ticketService.addTicket("Первый", 100.0, new Date()); + log.info(ticket.toString()+"testTicketRead1"); + final Ticket findTicket = ticketService.findTicket(ticket.getId()); + log.info(findTicket.toString()+"testTicketRead2"); + Assertions.assertEquals(ticket, findTicket); + } + + @Test + void testTicketReadNotFound() { + ticketService.deleteAllTickets(); + Assertions.assertThrows(EntityNotFoundException.class, () -> ticketService.findTicket(-1L)); + log.info("testTicketReadNotFound"); + } + + @Test + void testTicketReadAll() { + ticketService.deleteAllTickets(); + ticketService.addTicket("Первый", 100.0, new Date()); + ticketService.addTicket("Второй", 100.0, new Date()); + final List tickets = ticketService.findAllTickets(); + log.info(tickets.toString()+"testTicketReadAll"); + Assertions.assertEquals(tickets.size(), 2); + } + + @Test + void testTicketReadAllEmpty() { + ticketService.deleteAllTickets(); + final List tickets = ticketService.findAllTickets(); + log.info(tickets.toString()+"testTicketReadAllEmpty"); + Assertions.assertEquals(tickets.size(), 0); + } + + @Test + void testOrder() { + customerService.deleteAllCustomers(); + orderService.deleteAllOrders(); + ticketService.deleteAllTickets(); + + final Ticket ticket1 = ticketService.addTicket("раз", 300.0, new Date()); + final Ticket ticket2 = ticketService.addTicket("два", 200.0, new Date()); + + final Customer customer1 = customerService.addCustomer("Иван", "Иванов"); + final Customer customer2 = customerService.addCustomer("Петр", "Иванов"); + + final Order order = orderService.addOrder(customer1, new Date()); + /*order.addTicket(ticket1); + order.addTicket(ticket2); + log.info(order.toString()); + + final OrderItem order1 = orderService.findOrder(order.getId()); + Assertions.assertEquals(true, order1.getTickets().contains(ticket1)); + Assertions.assertEquals(order, order1); + + Assertions.assertEquals(order1.getTickets().size(), 2); + + orderService.deleteAllOrders(); + Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L)); + + final OrderItem order2 = orderService.addOrder(customer2, new Date()); + order2.addTicket(ticket1); + order2.addTicket(ticket2); + log.info(order2.toString());*/ + + } +} 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