Map Customer JPA API
This commit is contained in:
@@ -2,6 +2,8 @@ package com.chantha.jdbc.controller
|
||||
|
||||
import com.chantha.jdbc.jpa.model.Customer
|
||||
import com.chantha.jdbc.jpa.model.Order
|
||||
import com.chantha.jdbc.jpa.model.view.CustomerOrderView
|
||||
import com.chantha.jdbc.jpa.repo.CustomerRepo
|
||||
import com.chantha.jdbc.jpa.service.customer.CustomerService
|
||||
import com.chantha.jdbc.jpa.view.ViewsInvoice
|
||||
import org.apache.tomcat.util.json.JSONParser
|
||||
@@ -14,8 +16,9 @@ import org.springframework.web.bind.annotation.RestController
|
||||
@RestController
|
||||
class CustomerController @Autowired constructor(private val customerService: CustomerService) {
|
||||
|
||||
|
||||
@GetMapping("/customer")
|
||||
fun customer():List<Customer>{
|
||||
return customerService.findAll().toList()
|
||||
fun customer():List<CustomerOrderView>{
|
||||
return customerService.fetchAllCustomerOrder()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.chantha.jdbc.jpa.model.view;
|
||||
|
||||
import com.chantha.jdbc.jpa.model.Gender;
|
||||
import com.chantha.jdbc.jpa.model.Order;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class CustomerOrderView {
|
||||
public Long getCusID() {
|
||||
return cusID;
|
||||
}
|
||||
|
||||
public void setCusID(Long cusID) {
|
||||
this.cusID = cusID;
|
||||
}
|
||||
|
||||
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 List<OrderView> getOrderList() {
|
||||
return orderList;
|
||||
}
|
||||
|
||||
public void setOrderList(List<OrderView> orderList) {
|
||||
this.orderList = orderList;
|
||||
}
|
||||
|
||||
private Long cusID;
|
||||
private String cusName;
|
||||
private Gender gender;
|
||||
|
||||
private List<OrderView> orderList;
|
||||
|
||||
|
||||
}
|
||||
13
src/main/kotlin/com/chantha/jdbc/jpa/model/view/OrderView.kt
Normal file
13
src/main/kotlin/com/chantha/jdbc/jpa/model/view/OrderView.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.chantha.jdbc.jpa.model.view
|
||||
|
||||
import com.chantha.jdbc.utils.CustomDateSerializer
|
||||
import com.fasterxml.jackson.annotation.JsonFormat
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize
|
||||
import java.util.*
|
||||
|
||||
open class OrderView{
|
||||
var orderId:Long ?= 0
|
||||
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MMM-dd HH:mm:ss ")
|
||||
var orderDate:Date ? = Calendar.getInstance().time
|
||||
var amount:Double ? = 0.0
|
||||
}
|
||||
@@ -5,16 +5,7 @@ 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 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)
|
||||
@Query(value ="SELECT * FROM tb_customer c INNER JOIN tb_order o ON (c.id=o.cus_id)",nativeQuery = true)
|
||||
@Override
|
||||
Iterable<Customer> findAll();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
package com.chantha.jdbc.jpa.service.customer;
|
||||
|
||||
import com.chantha.jdbc.jpa.model.view.CustomerOrderView;
|
||||
import com.chantha.jdbc.jpa.repo.CustomerRepo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
public interface CustomerService extends CustomerRepo {
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public interface CustomerService {
|
||||
List<CustomerOrderView> fetchAllCustomerOrder();
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.chantha.jdbc.jpa.service.customer;
|
||||
|
||||
import com.chantha.jdbc.jpa.model.Customer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
@Override
|
||||
public <S extends Customer> S save(S s) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends Customer> Iterable<S> saveAll(Iterable<S> iterable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Customer> findById(Long aLong) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsById(Long aLong) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Customer> findAll() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Customer> findAllById(Iterable<Long> iterable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Long aLong) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Customer customer) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll(Iterable<? extends Customer> iterable) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.chantha.jdbc.jpa.service.customer
|
||||
|
||||
|
||||
import com.chantha.jdbc.jpa.model.Customer
|
||||
import com.chantha.jdbc.jpa.model.Order
|
||||
import com.chantha.jdbc.jpa.model.view.CustomerOrderView
|
||||
import com.chantha.jdbc.jpa.model.view.OrderDetailView
|
||||
import com.chantha.jdbc.jpa.model.view.OrderView
|
||||
import com.chantha.jdbc.jpa.repo.CustomerRepo
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class CustomerServiceImpl @Autowired constructor(private val customerRepo: CustomerRepo) : CustomerService {
|
||||
override fun fetchAllCustomerOrder(): List<CustomerOrderView> {
|
||||
return customerRepo.findAll().map { toView(it) }
|
||||
}
|
||||
|
||||
private fun toView(data:Customer):CustomerOrderView{
|
||||
val view=CustomerOrderView()
|
||||
view.cusID=data.id
|
||||
view.cusName=data.cusName
|
||||
view.gender=data.gender
|
||||
view.orderList=data.orders.map { toCustomerOrderView(it) }
|
||||
return view
|
||||
}
|
||||
|
||||
private fun toCustomerOrderView(data:Order):OrderView{
|
||||
val view=OrderView()
|
||||
view.orderId=data.orderId
|
||||
view.orderDate=data.orderDate
|
||||
view.amount=data.amount
|
||||
return view
|
||||
}
|
||||
|
||||
// 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
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user