Merge pull request 'fix(admin): fix query total and refactor query search' (#213) from stage/hamed/refactor-search into develop

Reviewed-on: ebhomengo/niki#213
This commit is contained in:
hossein 2024-11-27 05:21:40 +00:00
commit 7aa381c113
17 changed files with 54 additions and 36 deletions

View File

@ -1,10 +1,10 @@
package benefactorkindboxhandler package benefactorkindboxhandler
import ( import (
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
"net/http" "net/http"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
) )

View File

@ -6,13 +6,14 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"testing"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup" "git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"net/http"
"testing"
) )
type BenefactorAddressTestSuit struct { type BenefactorAddressTestSuit struct {

View File

@ -6,11 +6,12 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"testing"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup" "git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
benefactorkindboxparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" benefactorkindboxparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"net/http"
"testing"
) )
type BenefactorKindBoxTestSuite struct { type BenefactorKindBoxTestSuite struct {
@ -86,7 +87,6 @@ func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGet() {
suite.Require().Equal(kindBox.Data.Status, response.Data.Status) suite.Require().Equal(kindBox.Data.Status, response.Data.Status)
suite.Require().Equal(kindBox.Data.DeliverReferTimeID, response.Data.DeliverReferTimeID) suite.Require().Equal(kindBox.Data.DeliverReferTimeID, response.Data.DeliverReferTimeID)
suite.Require().Equal(kindBox.Data.DeliverReferDate, response.Data.DeliverReferDate) suite.Require().Equal(kindBox.Data.DeliverReferDate, response.Data.DeliverReferDate)
}) })
suite.Run("Failure_NotFound", func() { suite.Run("Failure_NotFound", func() {

View File

@ -6,6 +6,10 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"testing"
"time"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup" "git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
@ -13,9 +17,6 @@ import (
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"net/http"
"testing"
"time"
) )
type BenefactorKindBoxReqsTestSuite struct { type BenefactorKindBoxReqsTestSuite struct {
@ -105,7 +106,6 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Succe
assert.Equal(suite.T(), kinBoxReq.Data.CountRequested, response.Data.CountRequested) assert.Equal(suite.T(), kinBoxReq.Data.CountRequested, response.Data.CountRequested)
assert.Equal(suite.T(), kinBoxReq.Data.Description, response.Data.Description) assert.Equal(suite.T(), kinBoxReq.Data.Description, response.Data.Description)
assert.Equal(suite.T(), kinBoxReq.Data.DeliverReferTimeID, response.Data.DeliverReferTimeID) assert.Equal(suite.T(), kinBoxReq.Data.DeliverReferTimeID, response.Data.DeliverReferTimeID)
}) })
suite.Run("Failure_NotFound", func() { suite.Run("Failure_NotFound", func() {

View File

@ -7,10 +7,11 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
"github.com/labstack/echo/v4"
"log" "log"
"net/http/httptest" "net/http/httptest"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
"github.com/labstack/echo/v4"
) )
// CreateRequest is a utility function to create and send HTTP requests // CreateRequest is a utility function to create and send HTTP requests

View File

@ -25,3 +25,20 @@ func BuildGetAllQuery(baseQuery, table string, filter param.FilterRequest, pagin
return query, args return query, args
} }
func BuildGetTotalQuery(table, joinQuery string, filter param.FilterRequest, searchParams *param.QuerySearch, hasDeletedAt bool) (string, []any) {
var sArgs []any
baseQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s ", table)
if searchParams != nil {
baseQuery, sArgs = BuildGetSearchQuery(baseQuery, joinQuery, *searchParams)
}
query, fArgs := BuildGetAllQuery(baseQuery, table, filter, param.PaginationRequest{}, param.SortRequest{})
if hasDeletedAt {
query = BuildDeletedAtQuery(query, table)
}
args := append(sArgs, fArgs...)
return query, args
}

View File

@ -7,7 +7,7 @@ import (
// TODO: implementation more complete search service. // TODO: implementation more complete search service.
func BuildGetSearchQuery(baseQuery string, filter map[string]interface{}, exist bool) (string, []any) { func BuildGetSearchQuery(baseQuery, joinQuery string, filter map[string]interface{}) (string, []any) {
var conditions []string var conditions []string
var args []any var args []any
for key, value := range filter { for key, value := range filter {
@ -23,8 +23,8 @@ func BuildGetSearchQuery(baseQuery string, filter map[string]interface{}, exist
query := baseQuery query := baseQuery
if len(conditions) > 0 { if len(conditions) > 0 {
subQuery := strings.Join(conditions, " OR ") subQuery := strings.Join(conditions, " OR ")
if exist { if joinQuery != "" {
query += fmt.Sprintf(" INNER JOIN benefactors ON benefactor_id = benefactors.id WHERE (%s) ", subQuery) query += fmt.Sprintf(joinQuery, subQuery)
} else { } else {
if strings.Contains(strings.ToUpper(baseQuery), "WHERE") { if strings.Contains(strings.ToUpper(baseQuery), "WHERE") {
query += " AND " query += " AND "

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -2,6 +2,7 @@ package mysqladdress
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -2,6 +2,7 @@ package mysqladdress
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -4,21 +4,21 @@ import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql" builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (d *DB) GetAllBenefactor(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest, searchParams *params.QuerySearch) ([]entity.Benefactor, uint, error) { func (d *DB) GetAllBenefactor(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest, searchParams *param.QuerySearch) ([]entity.Benefactor, uint, error) {
const op = "mysqlbenefactor.GetAllBenefactor" const op = "mysqlbenefactor.GetAllBenefactor"
var sArgs []any var sArgs []any
table := "benefactors" table := "benefactors"
joinQuery := ""
baseQuery := `SELECT * FROM benefactors` baseQuery := `SELECT * FROM benefactors`
if searchParams != nil { if searchParams != nil {
baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, *searchParams, false) baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, joinQuery, *searchParams)
} }
query, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort) query, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort)
args := append(sArgs, fArgs...) args := append(sArgs, fArgs...)
@ -45,8 +45,7 @@ func (d *DB) GetAllBenefactor(ctx context.Context, filter params.FilterRequest,
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
} }
var total uint var total uint
baseQuery = `SELECT COUNT(*) FROM benefactors` query, args = builder.BuildGetTotalQuery(table, joinQuery, filter, searchParams, false)
query, args = builder.BuildGetAllQuery(baseQuery, table, filter, params.PaginationRequest{}, params.SortRequest{})
qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total) qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
if qErr != nil { if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -9,8 +9,8 @@ import (
"git.gocasts.ir/ebhomengo/niki/repository/mysql" "git.gocasts.ir/ebhomengo/niki/repository/mysql"
) )
func (d *DB) UpdateBenefactor(ctx context.Context, benefactor entity.Benefactor) error { func (d *DB) UpdateGeneralBenefactor(ctx context.Context, benefactor entity.Benefactor) error {
const op = "mysqlbenefactor.UpdateBenefactor" const op = "mysqlbenefactor.UpdateGeneralBenefactor"
query := `UPDATE benefactors query := `UPDATE benefactors
SET first_name = ?, last_name = ?, birth_date = ? SET first_name = ?, last_name = ?, birth_date = ?

View File

@ -4,21 +4,21 @@ import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param" "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql" builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
func (d *DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest, searchParams *params.QuerySearch) ([]entity.KindBox, uint, error) { func (d *DB) GetAllKindBox(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest, searchParams *param.QuerySearch) ([]entity.KindBox, uint, error) {
const op = "mysqlkindbox.GetAllKindBox" const op = "mysqlkindbox.GetAllKindBox"
var sArgs []any var sArgs []any
table := "kind_boxes" table := "kind_boxes"
joinQuery := " INNER JOIN benefactors ON benefactor_id = benefactors.id WHERE (%s) "
baseQuery := `SELECT kind_boxes.* FROM kind_boxes` baseQuery := `SELECT kind_boxes.* FROM kind_boxes`
if searchParams != nil { if searchParams != nil {
baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, *searchParams, true) baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, joinQuery, *searchParams)
} }
filterQuery, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort) filterQuery, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort)
query := builder.BuildDeletedAtQuery(filterQuery, table) query := builder.BuildDeletedAtQuery(filterQuery, table)
@ -45,8 +45,7 @@ func (d *DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pag
} }
var total uint var total uint
baseQuery = `SELECT COUNT(*) FROM kind_boxes WHERE deleted_at IS NULL` query, args = builder.BuildGetTotalQuery(table, joinQuery, filter, searchParams, true)
query, args = builder.BuildGetAllQuery(baseQuery, table, filter, params.PaginationRequest{}, params.SortRequest{})
qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total) qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
if qErr != nil { if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -14,11 +14,11 @@ func (d *DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, p
const op = "mysqlkindboxreq.GetAllKindBoxReq" const op = "mysqlkindboxreq.GetAllKindBoxReq"
var sArgs []any var sArgs []any
table := "kind_box_reqs" table := "kind_box_reqs"
joinQuery := " INNER JOIN benefactors ON benefactor_id = benefactors.id WHERE (%s) "
baseQuery := `SELECT kind_box_reqs.* FROM kind_box_reqs` baseQuery := `SELECT kind_box_reqs.* FROM kind_box_reqs`
if searchParams != nil { if searchParams != nil {
baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, *searchParams, true) baseQuery, sArgs = builder.BuildGetSearchQuery(baseQuery, joinQuery, *searchParams)
} }
filterQuery, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort) filterQuery, fArgs := builder.BuildGetAllQuery(baseQuery, table, filter, pagination, sort)
query := builder.BuildDeletedAtQuery(filterQuery, table) query := builder.BuildDeletedAtQuery(filterQuery, table)
@ -45,8 +45,7 @@ func (d *DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, p
} }
var total uint var total uint
baseQuery = `SELECT COUNT(*) FROM kind_box_reqs WHERE deleted_at IS NULL` query, args = builder.BuildGetTotalQuery(table, joinQuery, filter, searchParams, true)
query, args = builder.BuildGetAllQuery(baseQuery, table, filter, param.PaginationRequest{}, param.SortRequest{})
qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total) qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
if qErr != nil { if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)

View File

@ -12,7 +12,7 @@ import (
type Repository interface { type Repository interface {
IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error)
GetByID(ctx context.Context, id uint) (entity.Benefactor, error) GetByID(ctx context.Context, id uint) (entity.Benefactor, error)
UpdateBenefactor(ctx context.Context, benefactor entity.Benefactor) error UpdateGeneralBenefactor(ctx context.Context, benefactor entity.Benefactor) error
UpdateStatusBenefactor(ctx context.Context, benefactor entity.Benefactor, status entity.BenefactorStatus) error UpdateStatusBenefactor(ctx context.Context, benefactor entity.Benefactor, status entity.BenefactorStatus) error
GetAllBenefactor(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest, searchParams *param.QuerySearch) ([]entity.Benefactor, uint, error) GetAllBenefactor(ctx context.Context, filter param.FilterRequest, pagination param.PaginationRequest, sort param.SortRequest, searchParams *param.QuerySearch) ([]entity.Benefactor, uint, error)
} }

View File

@ -23,7 +23,7 @@ func (s Service) Update(ctx context.Context, req param.BenefactorUpdateRequest)
benefactor.LastName = req.LastName benefactor.LastName = req.LastName
benefactor.BirthDate = req.BirthDate.Time benefactor.BirthDate = req.BirthDate.Time
if uErr := s.repo.UpdateBenefactor(ctx, benefactor); uErr != nil { if uErr := s.repo.UpdateGeneralBenefactor(ctx, benefactor); uErr != nil {
return param.BenefactorUpdateResponse{}, richerror.New(op).WithErr(uErr) return param.BenefactorUpdateResponse{}, richerror.New(op).WithErr(uErr)
} }

View File

@ -96,7 +96,6 @@ func (v Validator) AreSearchValid() validation.RuleFunc {
} }
if len(search.Query) > MaxLengthQuerySearch { if len(search.Query) > MaxLengthQuerySearch {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput) return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
} }
return nil return nil