mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add product status filtering to product list and request handling
This commit is contained in:
parent
bc77fe942c
commit
a86d9a7513
@ -313,6 +313,7 @@ type ProductListFilter struct {
|
|||||||
PriceMin *int64
|
PriceMin *int64
|
||||||
PriceMax *int64
|
PriceMax *int64
|
||||||
PresaleType *int32
|
PresaleType *int32
|
||||||
|
Status *productv1.ProductStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
// CategoryRecord is the persistence model for product.catalog_category.
|
// CategoryRecord is the persistence model for product.catalog_category.
|
||||||
@ -3387,6 +3388,9 @@ func productListWhere(filter ProductListFilter, scope StoreScope) (string, []any
|
|||||||
if filter.PresaleType != nil {
|
if filter.PresaleType != nil {
|
||||||
add("p.presale_type = $%d", *filter.PresaleType)
|
add("p.presale_type = $%d", *filter.PresaleType)
|
||||||
}
|
}
|
||||||
|
if filter.Status != nil {
|
||||||
|
add("p.status = $%d", statusToDB(*filter.Status))
|
||||||
|
}
|
||||||
return strings.Join(predicates, " AND "), args
|
return strings.Join(predicates, " AND "), args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,8 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEscapeLikeLiteral(t *testing.T) {
|
func TestEscapeLikeLiteral(t *testing.T) {
|
||||||
@ -74,3 +76,14 @@ func TestProductListWhereUsesBusinessProductCategoryTree(t *testing.T) {
|
|||||||
t.Fatalf("args = %#v, want [1001 2001]", args)
|
t.Fatalf("args = %#v, want [1001 2001]", args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProductListWhereFiltersStatus(t *testing.T) {
|
||||||
|
productStatus := productv1.ProductStatus_PRODUCT_STATUS_INVENTORY
|
||||||
|
where, args := productListWhere(ProductListFilter{Status: &productStatus}, StoreScope{StoreID: 1001})
|
||||||
|
if !strings.Contains(where, "p.status = $2") {
|
||||||
|
t.Fatalf("status predicate missing:\n%s", where)
|
||||||
|
}
|
||||||
|
if len(args) != 2 || args[1] != int16(1) {
|
||||||
|
t.Fatalf("args = %#v, want [1001 1]", args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -9,11 +9,13 @@ import (
|
|||||||
func TestProductListFilterFromRequestNormalizesImageFilters(t *testing.T) {
|
func TestProductListFilterFromRequestNormalizesImageFilters(t *testing.T) {
|
||||||
priceMin := int64(0)
|
priceMin := int64(0)
|
||||||
priceMax := int64(1999)
|
priceMax := int64(1999)
|
||||||
|
productStatus := productv1.ProductStatus_PRODUCT_STATUS_INVENTORY
|
||||||
filter, err := productListFilterFromRequest(&productv1.ListProductsRequest{
|
filter, err := productListFilterFromRequest(&productv1.ListProductsRequest{
|
||||||
TitleKeyword: " coffee ",
|
TitleKeyword: " coffee ",
|
||||||
CategoryId: " 11111 ",
|
CategoryId: " 11111 ",
|
||||||
PriceMin: &priceMin,
|
PriceMin: &priceMin,
|
||||||
PriceMax: &priceMax,
|
PriceMax: &priceMax,
|
||||||
|
Status: &productStatus,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("productListFilterFromRequest() error = %v", err)
|
t.Fatalf("productListFilterFromRequest() error = %v", err)
|
||||||
@ -27,6 +29,17 @@ func TestProductListFilterFromRequestNormalizesImageFilters(t *testing.T) {
|
|||||||
if filter.PriceMin == nil || *filter.PriceMin != 0 {
|
if filter.PriceMin == nil || *filter.PriceMin != 0 {
|
||||||
t.Fatalf("PriceMin = %v, want explicit zero", filter.PriceMin)
|
t.Fatalf("PriceMin = %v, want explicit zero", filter.PriceMin)
|
||||||
}
|
}
|
||||||
|
if filter.Status == nil || *filter.Status != productv1.ProductStatus_PRODUCT_STATUS_INVENTORY {
|
||||||
|
t.Fatalf("Status = %v, want inventory", filter.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProductListFilterFromRequestRejectsInvalidStatus(t *testing.T) {
|
||||||
|
invalidStatus := productv1.ProductStatus_PRODUCT_STATUS_UNSPECIFIED
|
||||||
|
_, err := productListFilterFromRequest(&productv1.ListProductsRequest{Status: &invalidStatus})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("productListFilterFromRequest() error = nil, want invalid status")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProductListFilterFromRequestRejectsInvalidCategoryID(t *testing.T) {
|
func TestProductListFilterFromRequestRejectsInvalidCategoryID(t *testing.T) {
|
||||||
|
|||||||
@ -802,6 +802,15 @@ func productListFilterFromRequest(req *productv1.ListProductsRequest) (repositor
|
|||||||
}
|
}
|
||||||
filter.PresaleType = &presaleType
|
filter.PresaleType = &presaleType
|
||||||
}
|
}
|
||||||
|
if req.Status != nil {
|
||||||
|
statusValue := req.GetStatus()
|
||||||
|
if statusValue != productv1.ProductStatus_PRODUCT_STATUS_INVENTORY &&
|
||||||
|
statusValue != productv1.ProductStatus_PRODUCT_STATUS_ON_SALE &&
|
||||||
|
statusValue != productv1.ProductStatus_PRODUCT_STATUS_OFF_SALE {
|
||||||
|
return repository.ProductListFilter{}, errors.New("status must be 1, 2, or 3")
|
||||||
|
}
|
||||||
|
filter.Status = &statusValue
|
||||||
|
}
|
||||||
if req.SoldQuantityMin != nil {
|
if req.SoldQuantityMin != nil {
|
||||||
value := req.GetSoldQuantityMin()
|
value := req.GetSoldQuantityMin()
|
||||||
if value < 0 {
|
if value < 0 {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user