update repo

This commit is contained in:
Chantha
2020-05-16 17:20:48 +07:00
parent 647e42507e
commit 4afcb55d8a
41 changed files with 418 additions and 125 deletions

View File

@@ -1,9 +1,12 @@
package com.chantha.jdbc.controller
import com.chantha.jdbc.jpa.model.Customer
import com.chantha.jdbc.jpa.model.Order
import com.chantha.jdbc.jpa.service.customer.CustomerService
import org.apache.catalina.User
import com.chantha.jdbc.jpa.view.ViewsInvoice
import org.apache.tomcat.util.json.JSONParser
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

View File

@@ -9,8 +9,5 @@ import org.springframework.web.bind.annotation.RestController
@RestController
class OrderController @Autowired constructor(private val orderService: OrderService) {
@GetMapping("/")
fun findAllOrder():List<Order>{
return orderService.findAll().toList()
}
}

View File

@@ -0,0 +1,19 @@
package com.chantha.jdbc.controller
import com.chantha.jdbc.jpa.model.Product
import com.chantha.jdbc.jpa.model.view.ProductWithOrderDetail
import com.chantha.jdbc.jpa.repo.ProductRepo
import com.chantha.jdbc.jpa.service.product.ProductService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
public class ProductController @Autowired constructor(private val productService: ProductService) {
@GetMapping("/product")
fun product():List<ProductWithOrderDetail>{
return productService.fetchAllProducts()
}
}

View File

@@ -1,9 +1,6 @@
package com.chantha.jdbc.jpa.model;
import com.chantha.jdbc.jpa.view.Views;
import com.chantha.jdbc.utils.CustomGenderSerializer;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import javax.persistence.*;
import java.io.Serializable;
@@ -11,6 +8,8 @@ import java.util.List;
@Entity
@Table(name = "tbCustomer")
@JsonPropertyOrder({"id","cusName","gender","orders"})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Customer implements Serializable {
@Id

View File

@@ -31,7 +31,7 @@ public class Order implements Serializable {
@JoinColumn(name = "cusId",nullable = false,referencedColumnName = "id")
private Customer customer;
@JsonManagedReference
@OneToMany(mappedBy = "order")
private List<OrderDetail> orderDetails;
@@ -68,7 +68,7 @@ public class Order implements Serializable {
this.customer = customer;
}
@JsonIgnore
@JsonManagedReference
public List<OrderDetail> getOrderDetails() {
return orderDetails;
}

View File

@@ -3,11 +3,13 @@ package com.chantha.jdbc.jpa.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class OrderDetail {
public class OrderDetail implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long orderDetailId;
@@ -16,7 +18,8 @@ public class OrderDetail {
private double price;
@JsonManagedReference
@JsonBackReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "orderId",nullable = false)
private Order order;

View File

@@ -1,13 +1,14 @@
package com.chantha.jdbc.jpa.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.*;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Entity
public class Product {
//@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "productId")
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -59,7 +60,8 @@ public class Product {
this.orderDetails=orderDetails;
}
@JsonManagedReference
// @JsonManagedReference
@JsonProperty("orderDetails")
@OneToMany(fetch = FetchType.EAGER,mappedBy = "product")
private List<OrderDetail> orderDetails;
}

View File

@@ -0,0 +1,31 @@
package com.chantha.jdbc.jpa.model.view;
public class OrderDetailView {
public Long getOrderDetailId() {
return orderDetailId;
}
public void setOrderDetailId(Long orderDetailId) {
this.orderDetailId = orderDetailId;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
private Long orderDetailId;
private int qty;
private double price;
}

View File

@@ -0,0 +1,35 @@
package com.chantha.jdbc.jpa.model.view;
import java.util.List;
public class ProductWithOrderDetail {
private Long productId;
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public List<OrderDetailView> getDetails() {
return details;
}
public void setDetails(List<OrderDetailView> details) {
this.details = details;
}
private String productName;
private List<OrderDetailView> details;
}

View File

@@ -1,15 +1,20 @@
package com.chantha.jdbc.jpa.repo;
import com.chantha.jdbc.jpa.model.Customer;
import com.chantha.jdbc.jpa.model.Order;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepo extends CrudRepository<Customer,Long> {
@Query(value = "SELECT * \n" +
"FROM tb_order o \n" +
"INNER JOIN tb_customer c\n" +
"ON (o.cus_id=c.id)",nativeQuery = true)
@Query(value = "SELECT *\n" +
" FROM tb_order o\n" +
" INNER JOIN order_detail od\n" +
" ON (o.order_id=od.order_id)\n" +
" INNER JOIN tb_customer c\n" +
" ON (o.cus_id=c.id)\n" +
" INNER JOIN product pro\n" +
" ON (pro.product_id=od.product_id)\n" +
"\n" +
"\t\t",nativeQuery = true)
@Override
Iterable<Customer> findAll();
}

View File

@@ -5,6 +5,8 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface OrderRepo extends CrudRepository<Order,Long> {
@Query(value = "SELECT * \n" +

View File

@@ -0,0 +1,13 @@
package com.chantha.jdbc.jpa.repo
import com.chantha.jdbc.jpa.model.Product
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
@Repository
interface ProductRepo : CrudRepository<Product,Long>{
@Query("SELECT * FROM product INNER JOIN order_detail ON (product.product_id=order_detail.product_id)"
, nativeQuery = true)
override fun findAll(): MutableIterable<Product>
}

View File

@@ -3,6 +3,7 @@ package com.chantha.jdbc.jpa.service.order;
import com.chantha.jdbc.jpa.model.Order;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class OrderServiceImpl implements OrderService{

View File

@@ -0,0 +1,9 @@
package com.chantha.jdbc.jpa.service.product
import com.chantha.jdbc.jpa.model.view.ProductWithOrderDetail
import org.springframework.stereotype.Service
@Service
interface ProductService {
fun fetchAllProducts(): List<ProductWithOrderDetail>
}

View File

@@ -0,0 +1,34 @@
package com.chantha.jdbc.jpa.service.product
import com.chantha.jdbc.jpa.model.OrderDetail
import com.chantha.jdbc.jpa.model.Product
import com.chantha.jdbc.jpa.model.view.OrderDetailView
import com.chantha.jdbc.jpa.model.view.ProductWithOrderDetail
import com.chantha.jdbc.jpa.repo.ProductRepo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
@Service
class ProductServiceImpl @Autowired constructor(private val productRepo: ProductRepo) : ProductService {
override fun fetchAllProducts(): List<ProductWithOrderDetail> {
return productRepo.findAll().map { toView(it) }
}
private fun toView(data: Product): ProductWithOrderDetail {
val view = ProductWithOrderDetail()
view.productId = data.productId
view.productName = data.productName
view.details = data.orderDetails.map { toOrderDetailView(it) }
return view
}
private fun toOrderDetailView(data: OrderDetail): OrderDetailView {
val view = OrderDetailView()
view.orderDetailId = data.orderDetailId
view.price = data.price
view.qty = data.qty
return view
}
}

View File

@@ -0,0 +1,18 @@
package com.chantha.jdbc.jpa.view
import com.chantha.jdbc.jpa.model.Customer
import com.chantha.jdbc.jpa.model.Gender
fun ViewsInvoice.toView():Customer{
return this.toView2()
}
fun ViewsInvoice.toView2(): Customer{
val data=Customer()
data.id=this.id
data.cusName=this.cusName
data.gender=this.gender
data.orders=this.order
return data
}

View File

@@ -1,78 +0,0 @@
package com.chantha.jdbc.jpa.view;
import com.chantha.jdbc.jpa.model.Gender;
import java.util.Date;
public class Views {
public static class Public{
private Long Id;
private String cusName;
private Gender gender;
private int orderId;
private double amount;
private Date orderDate;
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public String getCusName() {
return cusName;
}
public void setCusName(String cusName) {
this.cusName = cusName;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public Public() {
}
public Public(Long id, String cusName, Gender gender, int orderId, double amount, Date orderDate) {
Id = id;
this.cusName = cusName;
this.gender = gender;
this.orderId = orderId;
this.amount = amount;
this.orderDate = orderDate;
}
}
}

View File

@@ -0,0 +1,24 @@
package com.chantha.jdbc.jpa.view
import com.chantha.jdbc.jpa.model.Gender
import com.chantha.jdbc.jpa.model.Order
import com.fasterxml.jackson.annotation.JsonProperty
import java.util.*
class ViewsInvoice {
var id: Long? = 0
var cusName: String? = null
var gender: Gender? = Gender.OTHER
var orderId: Int? = 0
var orderDate: Date? = Calendar.getInstance().time
var amount: Double? = 0.0
var order: List<Order> ? = listOf()
}

View File

@@ -0,0 +1,2 @@
package com.chantha.jdbc.utils.constants