View Javadoc
1   /*
2    *    Copyright 2010-2023 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   * @author Eduardo Macarron
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     * Search product list.
65     *
66     * @param keywords
67     *          the keywords
68     *
69     * @return the list
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  }