Skip to content
Snippets Groups Projects
GoodsController.java 2.89 KiB
Newer Older
Josh's avatar
Josh committed
package com.example.freshonline.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
Josh's avatar
Josh committed
import com.example.freshonline.constants.Constants;
import com.example.freshonline.enums.respVerifyRule.VerifyRule;
import com.example.freshonline.model.StockedGoods;
import com.example.freshonline.service.GoodsService;
Josh's avatar
Josh committed
import com.example.freshonline.utils.RespBuilder;
Josh's avatar
Josh committed
import com.example.freshonline.utils.ValidationChecker;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

Josh's avatar
Josh committed
/**
 * @author Josh Sun
 */
Josh's avatar
Josh committed
@RestController
public class GoodsController {

Josh's avatar
Josh committed
    /**
     * @author Josh Sun
     * @param price_low_req
     * @param price_high_req
     * @param brands
     * @param sort_type_req
     * @param keyword
     * @param page_req
     * @param category_id_req
     * @return
Josh's avatar
Josh committed
     */
Josh's avatar
Josh committed
    @GetMapping("/goods")
    public JSONObject getSearch(@RequestParam(value = "price_low", required = false) String price_low_req,
                                    @RequestParam(value = "price_high", required = false) String price_high_req,
                                    @RequestParam(value = "brands", required = false) String brands,
                                    @RequestParam(value = "sort_type", required = false) String sort_type_req,
                                    @RequestParam(value = "keyword", required = false) String keyword,
                                    @RequestParam(value = "page", required = false) String page_req,
                                    @RequestParam(value = "category_id", required = false) String category_id_req){
Josh's avatar
Josh committed

        JSONObject param = new JSONObject();
        ValidationChecker vc = new ValidationChecker();
        param.put("page", vc.str2int(page_req, 1));
        if (price_low_req != null) param.put("price_low", vc.str2int(price_low_req, 0));
        if (price_high_req != null) param.put("price_high", vc.str2int(price_high_req, 10000));
        /**
Josh's avatar
Josh committed
         * brands:brand1, brand2 逗号分隔
         */
        if (brands != null) param.put("brands", brands);
        if (keyword != null) param.put("keyword", keyword);
        if ( (sort_type_req != null) && (vc.str2int(sort_type_req, 0) != 0) ){
            param.put("sort_type", vc.str2int(sort_type_req, 0));
        }
        if ( (category_id_req != null) && (vc.str2int(category_id_req, 0) != 0) ){
            param.put("category_id", vc.str2int(category_id_req, 0));
        }
Josh's avatar
Josh committed

        GoodsService gs = new GoodsService();
Josh's avatar
Josh committed
        JSONObject output = gs.getSearch(param);
Josh's avatar
Josh committed

Josh's avatar
Josh committed
        return RespBuilder.create(output, VerifyRule.NOT_NULL, Constants.OPERATE_SUCCESS, Constants.OPERATE_FAIL);
Josh's avatar
Josh committed
    }

Josh's avatar
Josh committed
//    /**
//     * @author Josh Sun
//     */
//    @GetMapping("*")
//    public void defaultMappingTest(){
//        System.out.println("default mapping");
//    }
Josh's avatar
Josh committed
}