1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore.service;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.mybatis.jpetstore.domain.Category;
22 import org.mybatis.jpetstore.domain.Item;
23 import org.mybatis.jpetstore.domain.Product;
24 import org.mybatis.jpetstore.mapper.CategoryMapper;
25 import org.mybatis.jpetstore.mapper.ItemMapper;
26 import org.mybatis.jpetstore.mapper.ProductMapper;
27 import org.springframework.stereotype.Service;
28
29
30
31
32
33
34 @Service
35 public class CatalogService {
36
37 private final CategoryMapper categoryMapper;
38 private final ItemMapper itemMapper;
39 private final ProductMapper productMapper;
40
41 public CatalogService(CategoryMapper categoryMapper, ItemMapper itemMapper, ProductMapper productMapper) {
42 this.categoryMapper = categoryMapper;
43 this.itemMapper = itemMapper;
44 this.productMapper = productMapper;
45 }
46
47 public List<Category> getCategoryList() {
48 return categoryMapper.getCategoryList();
49 }
50
51 public Category getCategory(String categoryId) {
52 return categoryMapper.getCategory(categoryId);
53 }
54
55 public Product getProduct(String productId) {
56 return productMapper.getProduct(productId);
57 }
58
59 public List<Product> getProductListByCategory(String categoryId) {
60 return productMapper.getProductListByCategory(categoryId);
61 }
62
63
64
65
66
67
68
69
70
71 public List<Product> searchProductList(String keywords) {
72 List<Product> products = new ArrayList<>();
73 for (String keyword : keywords.split("\\s+")) {
74 products.addAll(productMapper.searchProductList("%" + keyword.toLowerCase() + "%"));
75 }
76 return products;
77 }
78
79 public List<Item> getItemListByProduct(String productId) {
80 return itemMapper.getItemListByProduct(productId);
81 }
82
83 public Item getItem(String itemId) {
84 return itemMapper.getItem(itemId);
85 }
86
87 public boolean isItemInStock(String itemId) {
88 return itemMapper.getInventoryQuantity(itemId) > 0;
89 }
90 }