# 0531 後端
# customer bean
```
package com.ctbcbank.demo.finallab.bean;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class CustomerBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String password;
@ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@JoinTable(name="product_cart",
joinColumns = {@JoinColumn(name="customer_id")},
inverseJoinColumns = {
@JoinColumn(name = "product_id")
})
private List<ProductBean> products = new ArrayList<ProductBean>();
public CustomerBean(String name, String password) {
super();
this.name = name;
this.password = password;
// this.cart=cart;
}
}
```
# productbean
```
package com.ctbcbank.demo.finallab.bean;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class ProductBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int price;
private String filePath;
private String detail;
private Date updateDate = new Date();
@ManyToOne
@JoinColumn(name = "PRODUCTCATEGORY_ID_FK")
@JsonIgnoreProperties(ignoreUnknown = true,value = {"products"})
ProductCategoryBean productCategory;
@ManyToMany(cascade = CascadeType.ALL, mappedBy="products")
@JsonIgnore
private List<CustomerBean> customers;
public ProductBean(String name, int price, String filePath, String detail) {
super();
this.name = name;
this.price = price;
this.filePath = filePath;
this.detail = detail;
}
}
```
# productCategory bean
```
package com.ctbcbank.demo.finallab.bean;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
//@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class ProductCategoryBean{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@OneToMany(mappedBy = "productCategory", cascade = CascadeType.REMOVE)
private List<ProductBean> products = new ArrayList<>();
}
```
# customerbean controller
```
package com.ctbcbank.demo.finallab.Controller.rest;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctbcbank.demo.finallab.Repository.CustomerBeanRepository;
import com.ctbcbank.demo.finallab.Repository.ProductBeanRepository;
import com.ctbcbank.demo.finallab.bean.CustomerBean;
import com.ctbcbank.demo.finallab.bean.ProductBean;
import com.ctbcbank.demo.finallab.service.CustomerBeanService;
import com.ctbcbank.demo.finallab.service.ProductBeanService;
import lombok.extern.slf4j.Slf4j;
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/customer")
public class CustomerBeanController {
@Autowired
private CustomerBeanService service;
@Autowired
private ProductBeanService PBservice;
// 取得所有帳戶
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<CustomerBean> getAllCustomers() {
return service.getAllCustomerBean();
}
// 取得指定id
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CustomerBean getCustomerBeanById(@PathVariable long id) {
CustomerBean customerBean = service.getCustomerBeanById(id);
return customerBean;
}
// 刪除指定id
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void removeCustomerBean(@PathVariable long id) {
service.removeCustomerBean(id);
}
// 新增
@RequestMapping(value = "/all", method = RequestMethod.POST)
public String createCustomer(@RequestBody CustomerBean bean) {
service.saveOrUpdateCustomerBean(bean);
return bean.toString();
}
// 修改
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public CustomerBean modifyCustomer(@PathVariable("id") Long id, @RequestBody CustomerBean bean) {
bean.setId(id);
return service.updateCustomerBean(bean);
}
// 比對是否存在
@RequestMapping(value = "/{name}", method = RequestMethod.POST)
public Boolean compareCustomerBeanByName(@PathVariable String name) {
Boolean exist = false;
// 將現有帳戶資料查出
List<CustomerBean> allCustomerList = service.getAllCustomerBean();
// 將每筆資料與目前想加入的帳號比對
for (CustomerBean customerBean : allCustomerList) {
String customerName = customerBean.getName();
// 如果相同代表帳戶已存在
if (name.equals(customerName)) {
System.out.println("使用者:" + name + "已存在");
exist = true;
}
}
return exist;
}
// 新增
@RequestMapping(value = "/{customer_id}/{product_id}", method = RequestMethod.POST)
public CustomerBean addProduct(@PathVariable("customer_id") Long customer_id, @PathVariable("product_id") Long product_id,
@RequestBody ProductBean bean) {
CustomerBean customer1 = service.getCustomerBeanById(customer_id);
ProductBean product1 = PBservice.getProductBeanById(product_id);
List<ProductBean> products = new ArrayList<ProductBean>();
products.add(product1);
customer1.getProducts().add(product1);
service.saveOrUpdateCustomerBean(customer1);
return service.saveOrUpdateCustomerBean(customer1);
}
}
```
# productbeancontroller
```
package com.ctbcbank.demo.finallab.Controller.rest;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctbcbank.demo.finallab.Repository.ProductBeanRepository;
import com.ctbcbank.demo.finallab.bean.CustomerBean;
import com.ctbcbank.demo.finallab.bean.ProductBean;
import com.ctbcbank.demo.finallab.service.CustomerBeanService;
import com.ctbcbank.demo.finallab.service.ProductBeanService;
import com.ctbcbank.demo.finallab.service.ProductCategoryBeanService;
import lombok.extern.slf4j.Slf4j;
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/product")
public class ProductBeanController {
@Autowired
private ProductBeanService service;
@Autowired
private ProductCategoryBeanService categoryService;
@Autowired
private CustomerBeanService customerService;
// 取得所有
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<ProductBean> getAllProducts() {
return service.getAllProductBean();
}
// 取得指定id
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ProductBean getProductBeanById(@PathVariable long id) {
ProductBean productBean = service.getProductBeanById(id);
return productBean;
}
// 刪除指定id
@RequestMapping(value = "{customer_id}/{id}", method = RequestMethod.DELETE)
public void removeProductBean(@PathVariable long customer_id,@PathVariable long id) {
CustomerBean customer1 = customerService.getCustomerBeanById(customer_id);
ProductBean product1 = service.getProductBeanById(id);
customer1.getProducts().remove(product1);
service.saveOrUpdateProductBean(product1);
}
// 新增
@RequestMapping(value = "/all/{id}", method = RequestMethod.POST)
public String createProduct(@RequestBody ProductBean bean, @PathVariable("id") Long id) {
bean.setProductCategory(categoryService.getProductCategoryBeanById(id));
service.saveOrUpdateProductBean(bean);
return bean.toString();
}
// 修改
@RequestMapping(value = "/{id}/{cate_id}", method = RequestMethod.PUT)
public ProductBean modifyProduct(@PathVariable("id") Long id, @PathVariable("cate_id") Long cate_id,
@RequestBody ProductBean bean) {
bean.setProductCategory(categoryService.getProductCategoryBeanById(cate_id));
bean.setId(id);
return service.saveOrUpdateProductBean(bean);
}
// 比對商品是否存在
@RequestMapping(value = "{name}", method = RequestMethod.POST)
public Boolean compareProductBeanByName(@PathVariable String name) {
Boolean exist =false;
//將現有資料查出
List<ProductBean> allCartList = service.getAllProductBean();
//將每筆資料與目前想加入的商品名稱比對
for (ProductBean productBean : allCartList) {
String productName = productBean.getName();
//如果相同代表已存在購物車了
if(name.equals(productName)) {
System.out.println("商品:"+name+"已存在");
exist=true;
}
}
return exist;
}
// 刪除全部
@RequestMapping(value = "{customer_id}/all", method = RequestMethod.DELETE)
public void removeAllCartBean(@PathVariable long customer_id) {
CustomerBean customer1 = customerService.getCustomerBeanById(customer_id);
List<ProductBean> product1 = service.getAllProductBean();
customer1.getProducts().removeAll(product1);
customerService.saveOrUpdateCustomerBean(customer1);
}
}
```
# productCategorybean controller
```
package com.ctbcbank.demo.finallab.Controller.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctbcbank.demo.finallab.bean.ProductCategoryBean;
import com.ctbcbank.demo.finallab.service.ProductCategoryBeanService;
import lombok.extern.slf4j.Slf4j;
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/productCategory")
public class ProductCategoryBeanController {
@Autowired
private ProductCategoryBeanService service;
// 取得所有
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<ProductCategoryBean> getAllProducts() {
return service.getAllProductCategoryBean();
}
// 取得指定id
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ProductCategoryBean getProductCategeryBeanById(@PathVariable long id) {
ProductCategoryBean productCategoryBean = service.getProductCategoryBeanById(id);
return productCategoryBean;
}
// 刪除指定id
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void removeProductCategeryBean(@PathVariable long id) {
System.out.println("DeleteNum=>" + id);
service.removeProductCategoryBean(id);
}
// 新增
@RequestMapping(value = "/all", method = RequestMethod.POST)
public String createProductCategery(@RequestBody ProductCategoryBean bean) {
service.saveOrUpdateProductCategoryBean(bean);
return bean.toString();
}
// 修改
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ProductCategoryBean modifyProductCategory(@PathVariable("id") Long id, @RequestBody ProductCategoryBean bean) {
bean.setId(id);
return service.updateProductCategoryBean(bean);
}
}
```
# productbeanservicejpaimplement
```
package com.ctbcbank.demo.finallab.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.ctbcbank.demo.finallab.Repository.ProductBeanRepository;
import com.ctbcbank.demo.finallab.bean.ProductBean;
@Service
public class ProductBeanServiceJPAImplement implements ProductBeanService {
@Autowired
private ProductBeanRepository repository;
@Override
public List<ProductBean> getAllProductBean() {
return repository.findAll();
}
@Override
public ProductBean getProductBeanById(long id) {
return repository.getReferenceById(id);
}
@Override
public ProductBean saveOrUpdateProductBean(ProductBean bean) {
return repository.save(bean);
}
@Override
public void removeProductBean(long id) {
repository.deleteById(id);
}
@Override
public ProductBean updateProductBean(ProductBean bean) {
return repository.save(bean);
}
public void removeAllCartBean() {
repository.deleteAll(getAllProductBean());
}
}
```