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
import (
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
"net/http"
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"
echo "github.com/labstack/echo/v4"
)

View File

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

View File

@ -6,11 +6,12 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
benefactorkindboxparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
"github.com/stretchr/testify/suite"
"net/http"
"testing"
)
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.DeliverReferTimeID, response.Data.DeliverReferTimeID)
suite.Require().Equal(kindBox.Data.DeliverReferDate, response.Data.DeliverReferDate)
})
suite.Run("Failure_NotFound", func() {

View File

@ -6,6 +6,10 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
"git.gocasts.ir/ebhomengo/niki/entity"
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"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"net/http"
"testing"
"time"
)
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.Description, response.Data.Description)
assert.Equal(suite.T(), kinBoxReq.Data.DeliverReferTimeID, response.Data.DeliverReferTimeID)
})
suite.Run("Failure_NotFound", func() {

View File

@ -7,10 +7,11 @@ import (
"context"
"encoding/json"
"fmt"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
"github.com/labstack/echo/v4"
"log"
"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

View File

@ -25,3 +25,20 @@ func BuildGetAllQuery(baseQuery, table string, filter param.FilterRequest, pagin
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.
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 args []any
for key, value := range filter {
@ -23,8 +23,8 @@ func BuildGetSearchQuery(baseQuery string, filter map[string]interface{}, exist
query := baseQuery
if len(conditions) > 0 {
subQuery := strings.Join(conditions, " OR ")
if exist {
query += fmt.Sprintf(" INNER JOIN benefactors ON benefactor_id = benefactors.id WHERE (%s) ", subQuery)
if joinQuery != "" {
query += fmt.Sprintf(joinQuery, subQuery)
} else {
if strings.Contains(strings.ToUpper(baseQuery), "WHERE") {
query += " AND "

View File

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

View File

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

View File

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

View File

@ -4,21 +4,21 @@ import (
"context"
"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"
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
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"
var sArgs []any
table := "benefactors"
joinQuery := ""
baseQuery := `SELECT * FROM benefactors`
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)
args := append(sArgs, fArgs...)
@ -45,8 +45,7 @@ func (d *DB) GetAllBenefactor(ctx context.Context, filter params.FilterRequest,
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
var total uint
baseQuery = `SELECT COUNT(*) FROM benefactors`
query, args = builder.BuildGetAllQuery(baseQuery, table, filter, params.PaginationRequest{}, params.SortRequest{})
query, args = builder.BuildGetTotalQuery(table, joinQuery, filter, searchParams, false)
qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
if qErr != nil {
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"
)
func (d *DB) UpdateBenefactor(ctx context.Context, benefactor entity.Benefactor) error {
const op = "mysqlbenefactor.UpdateBenefactor"
func (d *DB) UpdateGeneralBenefactor(ctx context.Context, benefactor entity.Benefactor) error {
const op = "mysqlbenefactor.UpdateGeneralBenefactor"
query := `UPDATE benefactors
SET first_name = ?, last_name = ?, birth_date = ?

View File

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

View File

@ -12,7 +12,7 @@ import (
type Repository interface {
IsExistBenefactorByID(ctx context.Context, id uint) (bool, 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
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.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)
}

View File

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