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
38 private final CategoryMapper categoryMapper;
39
40 private final ItemMapper itemMapper;
41
42 private final ProductMapper productMapper;
43
44
45
46
47
48
49
50
51
52
53
54 public CatalogService(CategoryMapper categoryMapper, ItemMapper itemMapper, ProductMapper productMapper) {
55 this.categoryMapper = categoryMapper;
56 this.itemMapper = itemMapper;
57 this.productMapper = productMapper;
58 }
59
60
61
62
63
64
65 public List<Category> getCategoryList() {
66 return categoryMapper.getCategoryList();
67 }
68
69
70
71
72
73
74
75
76
77 public Category getCategory(String categoryId) {
78 return categoryMapper.getCategory(categoryId);
79 }
80
81
82
83
84
85
86
87
88
89 public Product getProduct(String productId) {
90 return productMapper.getProduct(productId);
91 }
92
93
94
95
96
97
98
99
100
101 public List<Product> getProductListByCategory(String categoryId) {
102 return productMapper.getProductListByCategory(categoryId);
103 }
104
105
106
107
108
109
110
111
112
113 public List<Product> searchProductList(String keywords) {
114 List<Product> products = new ArrayList<>();
115 for (String keyword : keywords.split("\\s+")) {
116 products.addAll(productMapper.searchProductList("%" + keyword.toLowerCase() + "%"));
117 }
118 return products;
119 }
120
121
122
123
124
125
126
127
128
129 public List<Item> getItemListByProduct(String productId) {
130 return itemMapper.getItemListByProduct(productId);
131 }
132
133
134
135
136
137
138
139
140
141 public Item getItem(String itemId) {
142 return itemMapper.getItem(itemId);
143 }
144
145
146
147
148
149
150
151
152
153 public boolean isItemInStock(String itemId) {
154 return itemMapper.getInventoryQuantity(itemId) > 0;
155 }
156 }