View Javadoc
1   /*
2    *    Copyright 2010-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * The Class CatalogService.
31   */
32  @Service
33  public class CatalogService {
34  
35    /** The category mapper. */
36    private final CategoryMapper categoryMapper;
37    /** The item mapper. */
38    private final ItemMapper itemMapper;
39    /** The product mapper. */
40    private final ProductMapper productMapper;
41  
42    /**
43     * Instantiates a new catalog service.
44     *
45     * @param categoryMapper
46     *          the category mapper
47     * @param itemMapper
48     *          the item mapper
49     * @param productMapper
50     *          the product mapper
51     */
52    public CatalogService(CategoryMapper categoryMapper, ItemMapper itemMapper, ProductMapper productMapper) {
53      this.categoryMapper = categoryMapper;
54      this.itemMapper = itemMapper;
55      this.productMapper = productMapper;
56    }
57  
58    /**
59     * Gets the category list.
60     *
61     * @return the category list
62     */
63    public List<Category> getCategoryList() {
64      return categoryMapper.getCategoryList();
65    }
66  
67    /**
68     * Get category.
69     *
70     * @param categoryId
71     *          the category id
72     *
73     * @return the category
74     */
75    public Category getCategory(String categoryId) {
76      return categoryMapper.getCategory(categoryId);
77    }
78  
79    /**
80     * Get product.
81     *
82     * @param productId
83     *          the product id
84     *
85     * @return the product
86     */
87    public Product getProduct(String productId) {
88      return productMapper.getProduct(productId);
89    }
90  
91    /**
92     * Get product list by category.
93     *
94     * @param categoryId
95     *          the category id
96     *
97     * @return the list
98     */
99    public List<Product> getProductListByCategory(String categoryId) {
100     return productMapper.getProductListByCategory(categoryId);
101   }
102 
103   /**
104    * Search product list.
105    *
106    * @param keywords
107    *          the keywords
108    *
109    * @return the list
110    */
111   public List<Product> searchProductList(String keywords) {
112     List<Product> products = new ArrayList<>();
113     for (String keyword : keywords.split("\\s+")) {
114       products.addAll(productMapper.searchProductList("%" + keyword.toLowerCase() + "%"));
115     }
116     return products;
117   }
118 
119   /**
120    * Get item list by product.
121    *
122    * @param productId
123    *          the product id
124    *
125    * @return the list
126    */
127   public List<Item> getItemListByProduct(String productId) {
128     return itemMapper.getItemListByProduct(productId);
129   }
130 
131   /**
132    * Get item.
133    *
134    * @param itemId
135    *          the item id
136    *
137    * @return the item
138    */
139   public Item getItem(String itemId) {
140     return itemMapper.getItem(itemId);
141   }
142 
143   /**
144    * Is item in stock.
145    *
146    * @param itemId
147    *          the item id
148    *
149    * @return true, if successful
150    */
151   public boolean isItemInStock(String itemId) {
152     return itemMapper.getInventoryQuantity(itemId) > 0;
153   }
154 }