Skip to content

Commit

Permalink
search function have bug on mac os
Browse files Browse the repository at this point in the history
  • Loading branch information
Ming Hung authored and Ming Hung committed Nov 4, 2019
1 parent 9b524c5 commit 540229f
Show file tree
Hide file tree
Showing 19 changed files with 761 additions and 260 deletions.
30 changes: 26 additions & 4 deletions src/main/java/shopping/controller/Mall.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -22,6 +24,8 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;

Expand Down Expand Up @@ -71,7 +75,8 @@ public class Mall {
OrderItemService orderItemService;
@Autowired
GroupBuyService groupBuyService;

@Autowired
ServletContext context;
String account, pwd;
OrderBean orderBean = null;
Product product = null;
Expand Down Expand Up @@ -300,6 +305,19 @@ public String helloWorld() {
}
}

// 食材搜尋
@RequestMapping(value = "api/search", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
public @ResponseBody String searchProduct(@RequestParam(value = "search") String search) {
String json = null;
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
DataSource ds = ctx.getBean("dataSource", DataSource.class);
List<IngredientProduct> ingredientProducts = ingredientProductService.getIngredientProductBySearch(ds, search);
ToJson<IngredientProduct> toJson = new ToJson<IngredientProduct>();
json = toJson.getArrayJson(ingredientProducts);
System.out.println(json);
return json;
}

// 食材加入購物車 (團購)
@RequestMapping(value = "api/shopCart/groupAdd/{group}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
public @ResponseBody String ingredientProductCartGroup(@RequestParam(value = "category") String category,
Expand All @@ -310,7 +328,7 @@ public String helloWorld() {

GroupBuyBean groupBuyBean = groupBuyService.queryGroupBuyByAlias(group);
if (groupBuyBean.getStatus() != 0) {
return null;
return "(redirect:/shopMaterial)";
}
;

Expand Down Expand Up @@ -723,7 +741,10 @@ public String groupBillCartItem(@RequestParam("subscriberName") String subscribe
// Map<String, String> status = new HashMap<String, String>();
// status.put("status", "false");
// json = new ToJson<Map<String, String>>().getJson(status);
return null;
Map<String, String> map = new HashMap<String, String>();
map.put("id", null);
json = new ToJson<Map<String, String>>().getJson(map);
return json;
}

}
Expand Down Expand Up @@ -817,7 +838,7 @@ public String groupBuyJoin(@PathVariable("group") String group, HttpServletReque
public @ResponseBody String groupBuyQuery(@PathVariable("group") String group, HttpServletRequest req) {

GroupBuyBean groupBuyBeanInit = groupBuyService.queryGroupBuyByAlias(group);
if (groupBuyBeanInit == null) {
if (groupBuyBeanInit == null || groupBuyBeanInit.getStatus() != 0) {
Map<String, String> map = new HashMap<String, String>();
map.put("valid", "false");
Gson gson = new Gson();
Expand Down Expand Up @@ -865,6 +886,7 @@ public String groupBuyJoin(@PathVariable("group") String group, HttpServletReque
} else {
ToJson<GroupBuyBean> toJson = new ToJson<GroupBuyBean>();
String json = toJson.getArrayJson(groupBeans);
System.out.println(groupBeans.size());
return json;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/shopping/repository/IngredientProductDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import javax.sql.DataSource;

import shopping.model.IngredientProduct;
import shopping.model.ProductCategory;

Expand All @@ -12,6 +14,8 @@ public interface IngredientProductDao {

public List<IngredientProduct> getIngredientProductsByCategory(ProductCategory category);

List<IngredientProduct> getIngredientProductBySearch(DataSource ds, String search);

public void insertFakeData(List<IngredientProduct> ingredientProducts);

public IngredientProduct getIngredientProductById(Integer id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package shopping.repository.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Query;
import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand Down Expand Up @@ -74,4 +80,65 @@ public IngredientProduct getIngredientProductById(Integer id) {
}
return ingredientProduct;
}

// @Override
// public List<IngredientProduct> getIngredientProductBySearch(String search) {
// List<IngredientProduct> results = null;
// Session session = factory.getCurrentSession();
// try {
// String sql = "select a.id,a.name,a.price,a.fileName,b.unit from product as a join IngredientProduct "
// + "as b on (a.id=b.id) where match (a.name , a.introduction) against "
// + "(?) and FK_ProductCategory between 4 and 10;";
//
// results = session.createSQLQuery(sql).setParameter(1, search).getResultList();
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// return results;
// }

@Override
public List<IngredientProduct> getIngredientProductBySearch(DataSource ds, String search) {
List<IngredientProduct> ingredientProducts = null;
try {
Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(getQuerySqlFor(search));
System.out.println(rs);
ingredientProducts = getMaterial(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ingredientProducts;
}

private List<IngredientProduct> getMaterial(ResultSet rs) {
List<IngredientProduct> ingredientProducts = new ArrayList<IngredientProduct>();
try {
while (rs.next()) {

IngredientProduct ingredientProduct = new IngredientProduct();
ingredientProduct.setId(rs.getInt("id"));
ingredientProduct.setName(rs.getString("name"));
ingredientProduct.setUnit(rs.getString("unit"));
ingredientProduct.setFileName(rs.getString("fileName"));
ingredientProduct.setPrice(rs.getInt("price"));

ingredientProducts.add(ingredientProduct);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ingredientProducts;
}

private String getQuerySqlFor(String search) {
// TODO Auto-generated method stub
return "select a.id,a.name,a.price,a.fileName,b.unit from product as a join IngredientProduct "
+ "as b on (a.id=b.id) where match (a.name) against ('" + search
+ "') and FK_ProductCategory between 4 and 10;";
}
}
15 changes: 11 additions & 4 deletions src/main/java/shopping/repository/impl/OrderDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ public OrderBean query(Integer orderId, MemberBean memberBean) {
@Override
public OrderBean query(GroupBuyBean groupBuyBean, MemberBean memberBean) {
Session session = factory.getCurrentSession();
OrderBean orderBean = null;
orderBean = (OrderBean) session
.createQuery("from OrderBean where memberBean =:FK_MemberID and groupBuyBean =:groupBuyBean")
.setParameter("FK_MemberID", memberBean).setParameter("groupBuyBean", groupBuyBean).getSingleResult();
OrderBean orderBean;
orderBean = null;
try {
orderBean = (OrderBean) session
.createQuery("from OrderBean where memberBean =:FK_MemberID and groupBuyBean =:groupBuyBean")
.setParameter("FK_MemberID", memberBean).setParameter("groupBuyBean", groupBuyBean)
.getSingleResult();
} catch (NoResultException e) {
// TODO Auto-generated catch block
System.out.println("沒有該筆訂單");
}
return orderBean;
}
}
4 changes: 4 additions & 0 deletions src/main/java/shopping/service/IngredientProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import javax.sql.DataSource;

import shopping.model.IngredientProduct;

public interface IngredientProductService {
Expand All @@ -10,6 +12,8 @@ public interface IngredientProductService {

public List<IngredientProduct> getIngredientProductByCategory(String category);

public List<IngredientProduct> getIngredientProductBySearch(DataSource ds ,String search);

public IngredientProduct getIngredientProductById(Integer id);

public void insertFakeData(List<IngredientProduct> ingredientProducts);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package shopping.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -34,7 +40,7 @@ public List<IngredientProduct> getAllIngredientProducts() {
}
return ingredientProducts;
}

@Transactional
@Override
public List<IngredientProduct> getIngredientProductByCategory(String category) {
Expand Down Expand Up @@ -72,4 +78,11 @@ public IngredientProduct getIngredientProductById(Integer id) {

return ingredientProduct;
}

@Override
public List<IngredientProduct> getIngredientProductBySearch(DataSource ds, String search) {

return ingredientDaoImpl.getIngredientProductBySearch(ds, search);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/update.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE `Product` ADD FULLTEXT INDEX `product_name_ft_index` (`name`,`introduction`) WITH PARSER ngram;
ALTER TABLE `Product` ADD FULLTEXT INDEX `product_name_ft_index` (`name`) WITH PARSER ngram;
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/views/js/groupBuying_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function generateView() {
</div>
<div class="text-center">
如已建立揪團資料,請至「訂單管理」查看明細 <br />
<a href="">前往訂單管理</a>
<a href="orders">前往訂單管理</a>
</div>`;
$(mainBody).append(viewData);
// 複製參加連結
Expand Down
Loading

0 comments on commit 540229f

Please sign in to comment.