提问者:小点点

根据名称、价格等过滤器进行搜索[重复]


我正在实现一个产品搜索功能,用户可以根据名称、品牌和价格搜索产品。我写了不同的endpoint来搜索不同的组合,我讨厌代码,我不能轻松添加额外的过滤器,必须为我必须添加的任何额外过滤器创建所有组合。

我的产品仓库-

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.vaibhavshrivastava.productbrowser.model.Product;

public interface ProductRepository extends JpaRepository<Product, Integer> {
//  
    public List<Product> findByName(String name);
    public List<Product> findByNameAndProductCode(String name, int productCode);
    public List<Product> findByNameAndBrand(String name, String brand);
    public List<Product> findByNameAndBrandAndProductCode(String name, String brand, int productCode);
    public List<Product> findByBrand(String brand);
    public List<Product> findByProductCode(int productCode);
    public List<Product> findByBrandAndProductCode(String brand, int productCode);
//  public int getPrice(int productCode);
    public Optional<Product> findByProductCode(Integer productCode);

}

我的产品控制器-

@RestController
@CrossOrigin
@RequestMapping("/products")
public class ProductController {

    @Autowired
    ProductRepository productRepository;
    
    @Autowired
    ProductService productService;

    @GetMapping("/nameandbrand")
    public ResponseEntity<List<Product>> getProductsByNameAndBrand(@RequestParam String name,
            @RequestParam String brand) {
        return new ResponseEntity<>(productRepository.findByNameAndBrand(name, brand), HttpStatus.OK);
    }

    @GetMapping("/nameandproductcode")
    public ResponseEntity<List<Product>> getProductsByNameAndProductCode(@RequestParam String name,
            @RequestParam int productCode) {
        return new ResponseEntity<>(productRepository.findByNameAndProductCode(name, productCode), HttpStatus.OK);
    }

    @GetMapping("/name")
    public ResponseEntity<List<Product>> getProductsByName(@RequestParam String name) {
        return new ResponseEntity<>(productRepository.findByName(name), HttpStatus.OK);
    }
    
    @GetMapping("/nameandbrandandproductcode")
    public ResponseEntity<List<Product>> getProductsByNameOrBrandOrProductCode(@RequestParam String name, @RequestParam String brand, @RequestParam int productCode){
        return new ResponseEntity<>(productRepository.findByNameAndBrandAndProductCode(name, brand, productCode), HttpStatus.OK);
    }
    
    @GetMapping("/brand")
    public ResponseEntity<List<Product>> getProductsByBrand(@RequestParam String brand){
        return new ResponseEntity<>(productRepository.findByBrand(brand), HttpStatus.OK);
    }
    
    @GetMapping(name="/productcode")
    public ResponseEntity<Optional<Product>> getProductsByProductCode(@RequestParam Integer productCode){
        return new ResponseEntity<>(productRepository.findByProductCode(productCode), HttpStatus.OK);
    }
    
    @GetMapping("/brandandproductcode")
    public ResponseEntity<List<Product>> getProductsByBrandAndProductCode(@RequestParam String brand, @RequestParam int productCode){
        return new ResponseEntity<>(productRepository.findByBrandAndProductCode(brand, productCode), HttpStatus.OK);
    }
    
    @GetMapping("/{pid}/details")
    public ResponseEntity<Product> getProductDetails(@PathVariable("pid") Integer productCode){
        System.out.println("PPPPPPPPPPRDDDDDDDCTTTT CODEEEEE" + productCode);
        Product selectedProduct = productRepository.findByProductCode(productCode).orElseThrow();
        return new ResponseEntity<>(selectedProduct, HttpStatus.OK);
    }
    
    @GetMapping("/")
    public List<Product> getProducts(){
        return productService.getProducts();
    }
    
}

我还没有添加价格过滤器,我必须添加它,所以我必须使用价格过滤器进行所有组合搜索。实现这样的东西的最佳方法是什么?

我在前端使用角发送参数。

如何将这个糟糕的代码转换为我可以轻松添加额外过滤器的东西。

我的产品实体有这些字段-

@Entity
public class Product {

    @Id
    private int productCode;

    private String name;
    private String brand;
    private String description;
    private int price;
    private String img;

(不包括哈希和getter和setter等)


共1个答案

匿名用户

使用包含过滤器字段的自定义过滤器对象创建一个endpoint,然后使用该过滤器构建规范,然后使用该规范查询数据库