fix(niki): fix get all kindbox and kindboxreq with filter param

This commit is contained in:
Hamed Xamani 2024-11-09 19:45:34 +03:30
parent 69d4a3ec2b
commit 61c943c0ac
10 changed files with 31 additions and 16 deletions

View File

@ -19,7 +19,7 @@ import (
// @Param filter_id query int false "Filter by ID"
// @Param filter_kind_box_req_id query int false "Filter by KindBox request ID"
// @Param filter_benefactor_id query int false "Filter by benefactor ID"
// @Param filter_kind_box_type query string false "Filter by KindBox type" Enums(on-table,cylindrical,stand-up)
// @Param filter_type query string false "Filter by KindBox type" Enums(on-table,cylindrical,stand-up)
// @Param filter_amount query int false "Filter by amount"
// @Param filter_serial_number query string false "Filter by serial number"
// @Param filter_status query string false "Filter by status" Enums(delivered,ready-to-return,assigned-receiver-agent,returned,enumerated)

View File

@ -441,7 +441,7 @@
],
"type": "string",
"description": "Filter by KindBox type",
"name": "filter_kind_box_type",
"name": "filter_type",
"in": "query"
},
{

View File

@ -1378,7 +1378,7 @@ paths:
- cylindrical
- stand-up
in: query
name: filter_kind_box_type
name: filter_type
type: string
- description: Filter by amount
in: query

View File

@ -26,6 +26,7 @@ func (s *SearchRequest) GetSearch() *QuerySearch {
s.Query = strings.TrimSpace(s.Query)
re := regexp.MustCompile(`[\p{P}\p{S}]+`)
s.Query = re.ReplaceAllString(s.Query, "")
s.Query = strings.ReplaceAll(s.Query, " ", "")
for _, val := range searchItems {
searchParams[val] = s.Query
}

View File

@ -8,15 +8,19 @@ import (
func BuildDeletedAtQuery(query, table string) string {
deletedAtClause := fmt.Sprintf(" %s.deleted_at IS NULL ", table)
if !strings.Contains(query, "WHERE") {
return appendStringBefore(query, "WHERE", deletedAtClause)
}
if strings.Contains(query, "WHERE") {
return appendStringAfter(query, "AND", deletedAtClause)
}
func appendStringBefore(s, operator, appendage string) string {
limitIndex := strings.Index(strings.ToUpper(s), "LIMIT")
if strings.Contains(query, "ORDER") {
return appendStringBefore(query, "WHERE", "ORDER", deletedAtClause)
}
return appendStringBefore(query, "WHERE", "LIMIT", deletedAtClause)
}
func appendStringBefore(s, operator, needle, appendage string) string {
limitIndex := strings.Index(strings.ToUpper(s), needle)
if limitIndex == -1 {
return s + operator + appendage
}

View File

@ -13,10 +13,11 @@ func BuildGetSearchQuery(baseQuery string, filter map[string]interface{}, exist
for key, value := range filter {
if key == "id" {
conditions = append(conditions, fmt.Sprintf("%s = ?", key))
args = append(args, value)
} else {
conditions = append(conditions, fmt.Sprintf("%s LIKE ?", key))
args = append(args, "%"+value.(string)+"%")
}
args = append(args, value)
}
query := baseQuery

View File

@ -12,12 +12,15 @@ import (
func (d *DB) GetAllBenefactor(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest, searchParams *params.QuerySearch) ([]entity.Benefactor, uint, error) {
const op = "mysqlbenefactor.GetAllBenefactor"
var sArgs []any
table := "benefactors"
baseQuery := `SELECT * FROM benefactors`
searchQuery, sArgs := builder.BuildGetSearchQuery(baseQuery, *searchParams, false)
query, fArgs := builder.BuildGetAllQuery(searchQuery, table, filter, pagination, sort)
if searchParams != nil {
baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, *searchParams, false)
}
query, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort)
args := append(sArgs, fArgs...)
rows, qErr := d.conn.Conn().QueryContext(ctx, query, args...)

View File

@ -12,12 +12,15 @@ import (
func (d *DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest, searchParams *params.QuerySearch) ([]entity.KindBox, uint, error) {
const op = "mysqlkindbox.GetAllKindBox"
var sArgs []any
table := "kind_boxes"
baseQuery := `SELECT kind_boxes.* FROM kind_boxes`
searchQuery, sArgs := builder.BuildGetSearchQuery(baseQuery, *searchParams, true)
filterQuery, fArgs := builder.BuildGetAllQuery(searchQuery, table, filter, pagination, sort)
if searchParams != nil {
baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, *searchParams, true)
}
filterQuery, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort)
query := builder.BuildDeletedAtQuery(filterQuery, table)
args := append(sArgs, fArgs...)

View File

@ -12,12 +12,15 @@ import (
func (d *DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest, searchParams *param.QuerySearch) ([]entity.KindBoxReq, uint, error) {
const op = "mysqlkindboxreq.GetAllKindBoxReq"
var sArgs []any
table := "kind_box_reqs"
baseQuery := `SELECT kind_box_reqs.* FROM kind_box_reqs`
searchQuery, sArgs := builder.BuildGetSearchQuery(baseQuery, *searchParams, true)
filterQuery, fArgs := builder.BuildGetAllQuery(searchQuery, table, filter, pagination, sort)
if searchParams != nil {
baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, *searchParams, true)
}
filterQuery, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort)
query := builder.BuildDeletedAtQuery(filterQuery, table)
args := append(sArgs, fArgs...)

View File

@ -13,7 +13,7 @@ func (v Validator) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]st
const op = "adminkindboxvalidator.ValidateGetAll"
validFields := []string{
"id", "created_at", "kind_box_req_id", "benefactor_id", "kind_box_type", "amount", "serial_number",
"id", "created_at", "kind_box_req_id", "benefactor_id", "type", "amount", "serial_number",
"status", "deliver_refer_time_id", "deliver_refer_date", "deliver_address_id", "sender_agent_id",
"delivered_at", "return_refer_time_id", "return_refer_date",
"return_address_id", "receiver_agent_id", "returned_at",