Fix filtration on catalog

hotfix/hotfix-mysql-error
Ernest Litvinenko 2024-03-23 23:25:54 +03:00
parent 41c1da9c6e
commit 9937d8df67
5 changed files with 16 additions and 10 deletions

View File

@ -1,10 +1,11 @@
package endpoints package endpoints
import ( import (
"github.com/gin-gonic/gin"
"relynolli-server/models" "relynolli-server/models"
"relynolli-server/services" "relynolli-server/services"
"strconv" "strconv"
"github.com/gin-gonic/gin"
) )
func (h *handlers) GetCartItems(c *gin.Context) { func (h *handlers) GetCartItems(c *gin.Context) {

View File

@ -2,10 +2,11 @@ package endpoints
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin"
"net/http" "net/http"
"relynolli-server/models" "relynolli-server/models"
"relynolli-server/services" "relynolli-server/services"
"github.com/gin-gonic/gin"
) )
type createCartItemRequest struct { type createCartItemRequest struct {

View File

@ -2,11 +2,12 @@ package endpoints
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"net/http" "net/http"
"relynolli-server/models" "relynolli-server/models"
"relynolli-server/services" "relynolli-server/services"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
) )
type handlers struct{} type handlers struct{}

View File

@ -2,10 +2,11 @@ package endpoints
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin"
"net/http" "net/http"
"relynolli-server/models" "relynolli-server/models"
"relynolli-server/services" "relynolli-server/services"
"github.com/gin-gonic/gin"
) )
type handlers struct{} type handlers struct{}

View File

@ -3,12 +3,13 @@ package storage
import ( import (
"context" "context"
"fmt" "fmt"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/uptrace/bun"
"relynolli-server/internal" "relynolli-server/internal"
"relynolli-server/models/catalog" "relynolli-server/models/catalog"
filters2 "relynolli-server/models/filters" filters2 "relynolli-server/models/filters"
"slices" "slices"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/uptrace/bun"
) )
type StorageCatalog interface { type StorageCatalog interface {
@ -43,16 +44,17 @@ func buildFilterGroup(ctx context.Context, q *bun.SelectQuery, filters *cmap.Con
availableFilters := new([]filters2.DBFilter) availableFilters := new([]filters2.DBFilter)
//Get filters //Get filters
db.NewSelect().Model(availableFilters).Where("code in (?)", bun.In(filters.Keys())).Scan(ctx) db.NewSelect().Model(availableFilters).Scan(ctx)
for _, filter := range filters.Keys() { for _, filter := range filters.Keys() {
filter = filter[0 : len(filter)-2]
if !slices.ContainsFunc(*availableFilters, func(elem filters2.DBFilter) bool { if !slices.ContainsFunc(*availableFilters, func(elem filters2.DBFilter) bool {
return elem.Code == filter return elem.Code == filter
}) { }) {
continue continue
} }
q = q.WhereGroup(" AND ", func(query *bun.SelectQuery) *bun.SelectQuery { q = q.WhereGroup(" AND ", func(query *bun.SelectQuery) *bun.SelectQuery {
values, _ := filters.Get(filter) values, _ := filters.Get(filter + "[]")
for _, val := range values { for _, val := range values {
query = q.WhereOr(fmt.Sprintf("properties->>'$.%s' = ?", filter), val) query = q.WhereOr(fmt.Sprintf("properties->>'$.%s' = ?", filter), val)
} }
@ -67,7 +69,7 @@ func (s *storage) GetCatalogItems(ctx context.Context, filters cmap.ConcurrentMa
db := internal.InitDatabase().GetInstance() db := internal.InitDatabase().GetInstance()
model := new([]catalog.DBCatalog) model := new([]catalog.DBCatalog)
filterQuery := db.NewSelect().Model(model).Where("is_active = 1").Where("available_quantity > 0") filterQuery := db.NewSelect().Model(model).Where("is_active = 1").Where("available_quantity > 0")
count, _ := buildFilterGroup(ctx, filterQuery, &filters).Limit(limit).Offset(offset).ScanAndCount(ctx) count, _ := buildFilterGroup(ctx, filterQuery, &filters).Limit(limit).Offset(offset).Order("code").ScanAndCount(ctx)
return count, model, nil return count, model, nil
} }