Commit 228ddf82 authored by Cao Thành Phát's avatar Cao Thành Phát
Browse files

[update] shop, product database

parent f4dd67cf
......@@ -10,8 +10,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.shop.entity.Product;
import com.example.shop.ressource.FilterDTProduct;
import com.example.shop.ressource.ProductRess;
import com.example.shop.service.LoadShopDataService;
import com.example.shop.service.ProductService;
......@@ -23,11 +24,12 @@ public class ProductController {
LoadShopDataService loadShopDataService;
@Autowired ProductService productService;
@CrossOrigin(origins = "*", allowedHeaders = "*")
@GetMapping("/productList")
public Page <Product> showListProduct( @RequestParam("page") Optional<Integer> page , @RequestParam("size") Optional<Integer> size, @RequestParam Map<String,String> all) {
public Page <ProductRess> showListProduct( @RequestParam("page") Optional<Integer> page , @RequestParam("size") Optional<Integer> size, @RequestParam Map<String,String> all) {
int currentPage = page.orElse(1);
int pageSize = size.orElse(5);
return productService.getDataProduct(currentPage-1,pageSize,all);
int pageSize = size.orElse(4);System.err.println(" dadadadadadadadad");
return productService.getDataProduct(currentPage-1,pageSize,all);
}
@GetMapping("/productFilterForm")
public FilterDTProduct getFilterUI(){
......
......@@ -5,6 +5,10 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.shop.repository.GetShopInfoRepository;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
......@@ -15,6 +19,8 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class Product {
@Id
@Column(name = "id")
private int id;
......@@ -36,5 +42,5 @@ public class Product {
private int status;
@Column (name ="date")
private Long date;
}
}
......@@ -11,6 +11,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.example.shop.entity.ShopInfo;
import com.example.shop.entity.ShopInformation;
@Repository
@Transactional
......@@ -20,5 +21,6 @@ public interface GetShopInfoRepository extends JpaRepository<ShopInfo, Integer>{
@Query("SELECT c from ShopInfo c WHERE c.name like %?1% and c.category like %?2% " )
Page<ShopInfo> findShopByLocationAndCategory1(String name, String category, Pageable pageable);
ShopInfo findById(int id);
}
\ No newline at end of file
......@@ -19,6 +19,10 @@ import com.example.shop.entity.ShopInfo;
public interface ProductRepository extends JpaRepository<Product, Integer>{
List <Product> findByIdShop(int id);
@Query("SELECT c from Product c WHERE c.productName like %?1% and c.category like %?2% " )
Page<Product> getListProduct(String name, String category, Pageable pageable);
@Query("SELECT c from Product c WHERE c.productName like %?1% and c.category like %?2% and c.status= ?3 " )
List<Product> getListProduct(String name, String category, int status);
@Query("SELECT c from Product c WHERE c.productName like %?1% and c.category like %?2% " )
List<Product> getListProduct(String name, String category);
}
\ No newline at end of file
package com.example.shop.ressource;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
......
package com.example.shop.ressource;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.shop.entity.Product;
import com.example.shop.repository.GetShopInfoRepository;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductRess {
private int id;
private String picture;
private String productName;
private String category;
private int idShop;
private int views;
private int favorite;
private int triedTime;
private int status;
private Long date;
private String shopName;
public ProductRess(Product product , GetShopInfoRepository getShopInfoRepository) {
this.id =product.getId();
this.picture = product.getPicture();
this.productName = product.getProductName();
this.category = product.getCategory();
this.idShop = product.getIdShop();
this.views = product.getViews();
this.favorite = product.getFavorite();
this.triedTime =product.getTriedTime();
this.status = product.getStatus();
this.date = product.getDate();
this.shopName = getShopInfoRepository.findById(idShop).getName();
}
}
......@@ -4,6 +4,7 @@ import java.util.List;
import com.example.shop.entity.BankAccount;
import com.example.shop.entity.Product;
import com.example.shop.entity.ShopInfo;
import com.example.shop.entity.ShopInformation;
import lombok.AllArgsConstructor;
......
package com.example.shop.service;
import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
......@@ -73,10 +72,11 @@ public FilterDTS getDataShop(){
return new FilterDTS(cate,loc,status);
}
public ShopUI getShopUI(int idShop) {
ShopInformation shopInfo =shopInformationRepository.findByShopID(idShop);
ShopInformation shopInfo = shopInformationRepository.findByShopID(idShop);
List <Product> listProduct =productRepository.findByIdShop(idShop);
List <BankAccount> listBankAccount = bankAccountRepository.findByShopID(idShop);
return new ShopUI(listBankAccount, shopInfo, listProduct);
return new ShopUI(listBankAccount, shopInfo , listProduct);
}
}
package com.example.shop.service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.example.shop.entity.Product;
import com.example.shop.repository.GetShopInfoRepository;
import com.example.shop.repository.ProductRepository;
import com.example.shop.ressource.FilterDTProduct;
import com.example.shop.ressource.ProductRess;
@Service
public class ProductService {
......@@ -20,35 +25,44 @@ public class ProductService {
@Autowired
ProductRepository productRepository;
public Page<Product> getDataProduct( int page, int size,Map<String,String> all){
@Autowired
GetShopInfoRepository getShopInfoRepository;
public Page <ProductRess> getDataProduct( int page, int size,Map<String,String> all){
String category = checkNull(all.get("category"));
String name = checkNull(all.get("name"));
return productRepository.getListProduct(name, category,PageRequest.of(page,size));
Pageable pageable = PageRequest.of(page,size);
System.err.println(" dadadadadadadadad");
try {
int status= Integer.parseInt(checkNull(all.get("status")));
List<Product> products = productRepository.getListProduct(name, category,status);
List <ProductRess> productRess = parseProductToProductRss(products);
System.err.println(" dadadadadadadadad");
return new PageImpl <ProductRess> (productRess,pageable, productRess.size());
}
catch(NumberFormatException e) {
List<Product> products = productRepository.getListProduct(name, category);
List <ProductRess> productRess = parseProductToProductRss(products);
System.err.println(" dadadadadadadadad");
return new PageImpl <ProductRess> (productRess, pageable, productRess.size() );
}
}
public List <ProductRess> parseProductToProductRss ( List<Product> products) {
List<ProductRess> productresses = new ArrayList<ProductRess>();
if (products== null)
return productresses;
// products.forEach( product -> productresses.add(new ProductRess(product)));
for(Product product : products) {
productresses.add(new ProductRess(product,getShopInfoRepository));
}
return productresses;
}
String checkNull(String a) {
if( a == null) return "%%";
return a;
String checkNull(String input) {
if( input == null) return "%%";
return input;
}
//public Page <ShopInfo> getDataShopFilter( int page, int size, Map<String, String> all){
// String category = checkNull(all.get("category"));
// String name = checkNull(all.get("name"));
// try {
// int status= Integer.parseInt(checkNull(all.get("status")));
// ArrayList<ShopInfo> listShop = getShopInfoRepository.findShopByLocationAndCategory(name, category,status);
// return new PageImpl<ShopInfo> (listShop,PageRequest.of(page,size), listShop.size());
// }
// catch(NumberFormatException e) {
// ArrayList<ShopInfo> listShop = getShopInfoRepository.findShopByLocationAndCategory(name, category);
// return new PageImpl<ShopInfo> (listShop,PageRequest.of(page,size), listShop.size());
// }
//
//}
//
public FilterDTProduct getDataProduct(){
Set <String> cate = new HashSet <String>();
Set <String> loc = new HashSet <String>();
......@@ -60,13 +74,6 @@ public FilterDTProduct getDataProduct(){
});
return new FilterDTProduct(cate,status);
}
//
// public ShopUI getShopUI(int idShop) {
// ShopInformation shopInfo =shopInformationRepository.findByShopID(idShop);
// List <Product> listProduct =productRepository.findByIdShop(idShop);
// List <BankAccount> listBankAccount = bankAccountRepository.findByShopID(idShop);
// return new ShopUI(listBankAccount, shopInfo, listProduct);
// }
}
......@@ -38,10 +38,10 @@ function getDataFilter(sizeParam,pageParam,statusParam,categoryParam,nameParam)
page : pageParam,
status : statusParam,
category : categoryParam,
name : nameParam},function (data,success,status) {
var text = "" ,x,previous = 0,next = 0;
name : nameParam}
,function (data,success,status) {
var x,previous = 0,next = 0;
$("#tbShop tbody,.pagination").empty();
if (success) {
for (x in data.content) {
console.log(pageParam);
......@@ -62,6 +62,7 @@ function getDataFilter(sizeParam,pageParam,statusParam,categoryParam,nameParam)
} else {
previous = pageParam - 1;
}
if (pageParam != data.totalPages )
next = Number(pageParam) + 1;
$('<li class="page-item"><a class="page-link" data-page = '+previous+'>Previous</a></li>').appendTo('.pagination');
......
$.getJSON('http://192.168.1.150:1917/api/productFilterForm',function (data) {
var category ="" ,x, status;
for (x in data.listCategory) {
category += `<option>`+ data.listCategory[x] +`</option>`;
}
$("#category").append(category);
for (x in data.status) {
status += `<option>`+ data.status[x] +`</option>`;
}
$("#status").append(status);
});
function loadPage(sizeParam,pageParam,statusParam,categoryParam,nameParam)
{
$.get('http://192.168.1.150:1916/api/productList',{
size : 3,
page : 3,
name : nameParam,
category: categoryParam,
status:statusParam }
,function (data,success,status) {
var x,previous = 0,next = 0;
$("#table_product tbody,.pagination").empty();
if (success) {
for (x in data.content) {
var tr = "<tr>";
tr+= "<td>"+ data.content[x].id + "</td>";
tr+= "<td>"+ data.content[x].picture + "</td>";
tr+= "<td>"+ data.content[x].productName + "</td>";
tr+= "<td>"+ data.content[x].category + "</td>";
tr+= "<td>"+ data.content[x].shopName + "</td>";
tr+= "<td>"+ data.content[x].views + "</td>";
tr+= "<td>"+ data.content[x].favorite + "</td>";
tr+= "<td>"+ data.content[x].triedTime + "</td>";
tr+= "<td>"+ data.content[x].status + "</td>";
tr+= "<td>"+ data.content[x].date + "</td> </tr>";
$("#table_product").append(tr);
}
if (pageParam == 1) previous = 0;
else previous = pageParam - 1;
if (pageParam != data.totalPages )
next = Number(pageParam) + 1;
$('<li class="page-item"><a class="page-link" data-page = '+previous+'>Previous</a></li>').appendTo('.pagination');
for(let i = 1;i <= data.totalPages;i++)
{
$('<li class="page-item "><a class="page-link " data-page = '+i+'>'+ i +'</a></li>').appendTo('.pagination');
}
$('<li class="page-item "><a class="page-link " data-page = '+next+'>Next</a></li>').appendTo('.pagination');
$('[data-page='+ pageParam+']').parent().addClass("active");
}
});
}
$(document).ready(function () {
$(".pagination").on("click","a",function(){
var status = 1;
var productName = $("#product_name").val();
var categoryProduct = $("#category option:selected").val();
var size = 3;
loadPage(size, $(this).attr("data-page"));
})
});
$("#submit_filter").on("click", function(){
var status = 1;
var productName = $("#product_name").val();
var categoryProduct = $("#category option:selected").val();
var size = 3;
loadPage(size, 1 , status,categoryProduct,productName);
});
\ No newline at end of file
......@@ -24,7 +24,7 @@
</head>
<body id="page-top">
<body id="page-top" onload="loadPage(3)">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
......@@ -241,296 +241,142 @@
<p id='searchtxt' class="col-11">Search</p>
<p id='arrow-ic' class="col-1"> &#11167</p>
</div>
<form action="/productsearch.html" class=" col-12 ml-1 collapse" id='form-search'>
<form class=" col-12 ml-1 collapse" id='form-search'>
<div class="row">
<div class="d-flex col-6">
<label> Products name</label>
<div class="col-7 ml-1">
<input type="text" class="form-control " style="height: 30px;">
<input id="product_name" type="text" class="form-control "
style="height: 30px;">
</div>
</div>
<div class=" d-flex col-6 justify-content-end">
<div class=" d-flex col-6 justify-content-end">
<div>
<label>Categories</label>
</div>
<select id="category" class="ml-4 col-7 rounded">
<option selected="true" > All(120)</option>
<option value="summer">Summer</option>
<option value="aut">Autumn</option>
<option value="winter">Winter</option>
<option value="spring">Spring</option>
<select id="category"
class="col-7 rounded btn btn-secondary dropdown-toggle text-left bg-gray-300 bg-gray-200 border-0 text-dark">
</select>
</div>
</div>
</div>
<div class="row mt-3">
<div class=" d-flex col-6 ">
<div>
<label>Product status</label>
</div>
<select id="category1" class="ml-3 col-7 rounded">
<option selected="true" > All(120)</option>
<option value="summer">Summer</option>
<option value="aut">Autumn</option>
<option value="winter">Winter</option>
<option value="spring">Spring</option>
<select id="status"
class="col-7 rounded btn btn-secondary dropdown-toggle text-left bg-gray-300 bg-gray-200 border-0 text-dark">
</select>
</div>
<div class=" d-flex col-6 pr-10 justify-content-end">
<div>
<label> Sort</label>
</div>
<select id="category2" class="col-7 rounded" style="margin-left: 70px;">
<option selected="true" > All(120)</option>
<option value="summer">Summer</option>
<option value="aut">Autumn</option>
<option value="winter">Winter</option>
<option value="spring">Spring</option>
<select id="category2"
class="col-7 rounded btn btn-secondary dropdown-toggle text-left bg-gray-300 bg-gray-200 border-0 text-dark"
style="margin-left: 70px;">
<option>Newest to oldest</option>
<option>Oldest to newest</option>
<option>Top viewed</option>
<option>Top tried</option>
<option>Top favorited</option>
</select>
</div>
</div>
<div class=" row justify-content-center mt-3">
<button type ="submit" class=" btn rounded btn-success mb-4 justify-content-center ">Submit</button>
<button id ="submit_filter"
class=" btn rounded btn-success mb-4 justify-content-center ">Submit</button>
</div>
</form>
</div>
<!-- end -->
<div class="table-responsive p-4">
<table class="table table-bordered " width="100%" cellspacing="0">
<table id="table_product" class="table table-bordered " width="100%" cellspacing="0">
<thead>
<tr class="font-weight-bold">
<th>ID</th>
<th>Picture</th>
<th>Shop's name</th>
<th>Location</th>
<th>Product name</th>
<th>Category</th>
<th>Posted products</th>
<th>Pending products</th>
<th>Post by</th>
<th>Views</th>
<th>Favorite</th>
<th>Tried time</th>
<th>Status</th>
<th>Date</th>
</tr>
</thead>
<tbody class="text-center ">
<tr>
<td>1</td>
<td>xxad</td>
<td>Shop1</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-success">Activte</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>2</td>
<td>xxad</td>
<td>Shop2</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-primary">Pending</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>3</td>
<td>xxad</td>
<td>Shop3</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-danger">Blocked</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>4</td>
<td>xxad</td>
<td>Shop2</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-danger">Blocked</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>5</td>
<td>xxad</td>
<td>Shop5</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-primary">Pending</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>6</td>
<td>xxad</td>
<td>Shop2</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-primary">Pending</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>7</td>
<td>xxad</td>
<td>Shop3</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-danger">Blocked</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>8</td>
<td>xxad</td>
<td>Shop2</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-primary">Pending</td>
<td>10-1-2020</td>
</tr>
<tr>
<td>9</td>
<td>xxad</td>
<td>Shop3</td>
<td>HCM</td>
<td>Cate1</td>
<td>prod1</td>
<td>pProduct1</td>
<td class="badge badge-pill badge-danger">Blocked</td>