pg_repository.go 1.1 KB
package repository

import (
	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
)

func WhereContext(query *sqlbuilder.Query, queryOptions map[string]interface{}) *sqlbuilder.Query {
	if _, ok := queryOptions["context"]; !ok {
		return query
	}
	context := queryOptions["context"].(*domain.Context)
	query.Where("context->'companyId'='?'", context.CompanyId)
	return query
}

func SetOffsetAndLimit(query *sqlbuilder.Query, queryOptions map[string]interface{}, defaultLimit int) {
	pageNumber, pnOk := queryOptions["pageNumber"]
	pageSize, psOk := queryOptions["pageSize"]
	if pnOk && psOk {
		iPageNumber := utils.NumberString(utils.AssertString(pageNumber)).MustInt()
		iPageSize := utils.NumberString(utils.AssertString(pageSize)).MustInt()
		queryOptions["limit"] = iPageSize
		queryOptions["offset"] = (iPageSize * (iPageNumber - 1))
	}
	query.SetOffsetAndLimit(defaultLimit)
}