This is the third chapter of the SpringBoot enterprise wechat ordering system

Source code address: github.com/cachecats/s…

SpringBoot enterprise wechat ordering system actual combat: environment construction and database design

SpringBoot enterprise wechat ordering system combat two: log configuration, product category development

I. Commodity information

1.1 DTO for Product information

Write the corresponding entity class ProductInfo according to the database fields. @data is a Lombok annotation that automatically generates getters, setters, and toString methods.

@Entity
@Data
public class ProductInfo {

    @Id
    private String productId;

    // Product name
    private String productName;

    // Commodity prices
    private BigDecimal productPrice;

    // Inventory of goods
    private Integer productStock;

    // Product description
    private String productDescription;

    // Item status
    private Integer productStatus;

    // Product icon
    private String productIcon;

    // Product category number
    private Integer categoryType;

    public ProductInfo(a) {}}Copy the code

1.2 Commodity information Repository

A new method, findByProductStatus, has been added to query all listed items based on their status.

package com.solo.sell.repository;

import com.solo.sell.dto.ProductInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface ProductInfoRepository extends JpaRepository<ProductInfo.String>{

    List<ProductInfo> findByProductStatus(Integer status);
}
Copy the code

1.3 Commodity Information Service

Temporarily think of the following methods, do not add enough. Add inventory and reduce inventory later to achieve.

package com.solo.sell.service;

import com.solo.sell.dto.ProductInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface ProductInfoService {

    /** * Query a single item by id *@param id
     * @return* /
    ProductInfo findById(String id);

    /** * Query the products on shelves *@return* /
    List<ProductInfo> findUpAll(a);

    /** * query all items with pagination *@param pageable
     * @return* /
    Page<ProductInfo> findAll(Pageable pageable);

    /** * Save an item *@param productInfo
     * @return* /
    ProductInfo save(ProductInfo productInfo);

    / / add inventory

    / / inventory reduction

}
Copy the code

The implementation class ProductInfoServiceImpl

package com.solo.sell.service.impl;

import com.solo.sell.dto.ProductInfo;
import com.solo.sell.enums.ProductStatusEnum;
import com.solo.sell.repository.ProductInfoRepository;
import com.solo.sell.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository repository;

    @Override
    public ProductInfo findById(String id) {
        return repository.findById(id).get();
    }

    @Override
    public List<ProductInfo> findUpAll(a) {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        returnrepository.save(productInfo); }}Copy the code

Ii. Commodity information testing

The repository and service of product information are done, so get into the habit of testing.

2.1 the repository test

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductInfoRepositoryTest {

    @Autowired
    private ProductInfoRepository repository;

    @Test
    public void add(a){
        ProductInfo info = new ProductInfo();
        info.setProductId("haha123");
        info.setProductName("Fish with pickled cabbage");
        info.setProductPrice(new BigDecimal(36));
        info.setProductDescription("Delicious pickles and fish, not to be missed.");
        info.setProductIcon("http://abc.png");
        info.setProductStatus(0);
        info.setProductStock(10);
        info.setCategoryType(3);
        ProductInfo result = repository.save(info);
        Assert.assertNotNull(result);
    }

    @Test
    public void findOne(a){
        ProductInfo info = repository.findById("haha123").get();
        Assert.assertEquals("haha123", info.getProductId());
    }

    @Test
    public void findByProductStatus(a) {
        List<ProductInfo> list = repository.findByProductStatus(0);
        Assert.assertNotEquals(0, list.size()); }}Copy the code

2.2 the service test

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductInfoServiceImplTest {

    @Autowired
    private ProductInfoServiceImpl service;

    @Test
    public void findById(a) {
        ProductInfo info = service.findById("haha123");
        Assert.assertEquals("haha123", info.getProductId());
    }

    @Test
    public void findUpAll(a) {
        List<ProductInfo> all = service.findUpAll();
        Assert.assertNotEquals(0, all.size());
    }

    @Test
    public void findAll(a) {
        PageRequest pageRequest = PageRequest.of(0.3);
        Page<ProductInfo> productInfoPage = service.findAll(pageRequest);
        Assert.assertNotEquals(0, productInfoPage.getTotalElements());
    }

    @Test
    public void save(a) {
        ProductInfo info = new ProductInfo();
        info.setProductId("haha456");
        info.setProductName("Leathery shrimp");
        info.setProductPrice(new BigDecimal(49));
        info.setProductDescription("Shrimps, let's go.");
        info.setProductIcon("http://abc.png");
        info.setProductStatus(0);
        info.setProductStock(99);
        info.setCategoryType(4); ProductInfo save = service.save(info); Assert.assertNotNull(save); }}Copy the code

Iii. Development of buyer’s PRODUCT API

Once the product categories and product information are developed, the buyer API can be developed.

One of the simplest requirements for buyers is to query the list of products on the shelves, and implement this function first.

3.1 Analysis of returned results

Originally, the data format returned should be determined by the back end, but this project is the front end, which has specified the data format, let’s see what the data format is

{
    "code": 0."msg": "Success"."data": [{"name": "Hot list"."type": 1,
            "foods": [{"id": "123456"."name": "Preserved egg porridge"."price": 1.2."description": "Delicious congee with preserved eggs."."icon": "http://xxx.com",}]}, {"name": "Yummy"."type": 2."foods": [{"id": "123457"."name": "Mousse cake."."price": 10.9."description": "Delicious and refreshing."."icon": "http://xxx.com",}]}]}Copy the code

Make sure that all objects returned to the front end are in the VO package, and the object name ends in VO for easy differentiation. VO stands for View Object.

The return result has three layers of objects. The outermost layer should be the unified data structure for the whole project, which we define as ResultVO. ProdoctVO is the product category in the data field, and the foods field in each category is the specific product ProductInfoVO.

Results are returned ResultVO

package com.solo.sell.VO;

import lombok.Data;

/** * Returns a uniform result format *@param <T>
 */
@Data
public class ResultVO<T> {

    /** Status code */
    private Integer code;

    /** Returns a message */
    private String msg;

    /** Returns data */
    private T data;

    public ResultVO(a) {}}Copy the code

Returns the commodity object ProductVO

package com.solo.sell.VO;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.List;

/** * Product information (including category) */
@Data
public class ProductVO {

    @JsonProperty("name")
    private String categoryName;

    @JsonProperty("type")
    private Integer categoryType;

    @JsonProperty("foods")
    private List<ProductInfoVO> productInfoVOList ;
}
Copy the code

The name of the property should be as obvious as possible. Do not just call it name, because you do not know which name to refer to over time.

But what if the returned data is called name? Add an annotation @jsonProperty (“name”) to the property so that the corresponding property name is name.

Return product information ProductInfoVO

package com.solo.sell.VO;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class ProductInfoVO {

    @JsonProperty("id")
    private String productId;

    @JsonProperty("name")
    private String productName;

    @JsonProperty("price")
    private BigDecimal productPrice;

    @JsonProperty("description")
    private String productDescription;

    @JsonProperty("icon")
    private String productIcon;

}
Copy the code

3.2 Buyer commodity Controller

Now that we know the format of the return result, we can write the controller logic.

All the controllers are going to be in the Controller package, create a new Controller package, and then create BuyerProductController

package com.solo.sell.controller;

import com.solo.sell.VO.ProductInfoVO;
import com.solo.sell.VO.ProductVO;
import com.solo.sell.VO.ResultVO;
import com.solo.sell.dto.ProductCategory;
import com.solo.sell.dto.ProductInfo;
import com.solo.sell.service.ProductCategoryService;
import com.solo.sell.service.ProductInfoService;
import com.solo.sell.utils.ResultVOUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
@Api("Buyer side takes goods")
public class BuyerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService categoryService;

    @ApiOperation(value = "Get all listings", notes = "Get all items on shelves except for items that have been removed.")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultVO list(a) {

        // 1. Query all available items
        List<ProductInfo> productInfoList = productInfoService.findUpAll();

        // 2. Query category (one-time query)
        // get all types of shelves using java8 features
        List<Integer> categoryTypes = productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypes);

        List<ProductVO> productVOList = new ArrayList<>();
        // Data assembly
        for (ProductCategory category : productCategoryList) {
            ProductVO productVO = new ProductVO();
            // Attribute copy
            BeanUtils.copyProperties(category, productVO);
            // Add the item that matches the type
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(category.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        returnResultVOUtils.success(productVOList); }}Copy the code

Annotations are written in detail without much explanation, but are stitched together in the format required by the front end.

@API and @apiOperation annotations can be ignored for now, which integrates Swagger and makes debugging interface more convenient. The next article will be devoted to the integration of Swagger and Spring Boot.

3.3 validation

Open the browser, enter the email address http://127.0.0.1:8080/sell/buyer/product/list can see returned to the data


That’s all for this section. See you next time

Source code address: github.com/cachecats/s…

SpringBoot enterprise wechat ordering system actual combat: environment construction and database design

SpringBoot enterprise wechat ordering system combat two: log configuration, product category development