Skip to content

Commit

Permalink
feat: core modules SortBy filter for repos
Browse files Browse the repository at this point in the history
  • Loading branch information
thelissimus-work committed Feb 20, 2025
1 parent e86e9b5 commit 84f53ec
Show file tree
Hide file tree
Showing 23 changed files with 498 additions and 185 deletions.
30 changes: 21 additions & 9 deletions modules/core/domain/aggregates/employee/employee_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ package employee

import "context"

type DateRange struct {
From string
To string
type Field int

const (
Id Field = iota
FirstName
LastName
MiddleName
Salary
HourlyRate
Coefficient
CreatedAt
)

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
Limit int
Offset int
SortBy []string
Query string
Field string
CreatedAt DateRange
Limit int
Offset int
Query string
Field string
SortBy SortBy
}

type Repository interface {
Expand Down
16 changes: 14 additions & 2 deletions modules/core/domain/aggregates/role/role_repository.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package role

import (
"context"
import "context"

type Field int

const (
Name Field = iota
Description
CreatedAt
)

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
Name string
AttachPermissions bool
Limit int
Offset int
SortBy SortBy
}

type Repository interface {
Expand Down
20 changes: 17 additions & 3 deletions modules/core/domain/aggregates/user/user_repository.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package user

import (
"context"
import "context"

type Field = int

const (
FirstName Field = iota
LastName
MiddleName
Email
LastLogin
CreatedAt
)

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
Limit int
Offset int
SortBy []string
SortBy SortBy
}

type Repository interface {
Expand Down
18 changes: 15 additions & 3 deletions modules/core/domain/entities/currency/currency_repository.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package currency

import (
"context"
import "context"

type Field int

const (
FieldCode Field = iota
FieldName
FieldSymbol
FieldCreatedAt
)

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
Code string
Limit int
Offset int
SortBy []string
SortBy SortBy
}

type Repository interface {
Expand Down
17 changes: 15 additions & 2 deletions modules/core/domain/entities/permission/permission_repository.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package permission

import (
"context"
import "context"

type Field int

const (
FieldName Field = iota
FieldResource
FieldAction
FieldModifier
)

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
Limit int
Offset int
RoleID uint
SortBy SortBy
}

type Repository interface {
Expand Down
17 changes: 14 additions & 3 deletions modules/core/domain/entities/position/position_repository.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package position

import (
"context"
import "context"

type Field int

const (
Name Field = iota
Descripton
CreatedAt
)

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
ID int64
Limit int
Offset int
SortBy []string
SortBy SortBy
}

type Repository interface {
Expand Down
16 changes: 13 additions & 3 deletions modules/core/domain/entities/session/session_repository.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package session

import (
"context"
import "context"

type Field int

const (
ExpiresAt Field = iota
CreatedAt
)

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
Limit int
Offset int
SortBy []string
Token string
SortBy SortBy
}

type Repository interface {
Expand Down
15 changes: 11 additions & 4 deletions modules/core/domain/entities/upload/upload_repository.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package upload

import (
"context"
)
import "context"

type Field int

const Size Field = iota

type SortBy struct {
Fields []Field
Ascending bool
}

type FindParams struct {
ID uint
Hash string
Limit int
Offset int
SortBy []string
Search string
Type string
SortBy SortBy
}

type Repository interface {
Expand Down
71 changes: 56 additions & 15 deletions modules/core/infrastructure/persistence/currency_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,47 @@ import (
"context"
"errors"
"fmt"
"github.com/iota-uz/iota-sdk/pkg/repo"
"strings"

"github.com/iota-uz/iota-sdk/modules/core/domain/entities/currency"
"github.com/iota-uz/iota-sdk/modules/core/infrastructure/persistence/models"

"github.com/iota-uz/iota-sdk/pkg/composables"
"github.com/iota-uz/iota-sdk/pkg/repo"
)

var (
ErrCurrencyNotFound = errors.New("currency not found")
)

const (
selectCurrenciesQuery = `
SELECT
c.code,
c.name,
c.symbol,
c.created_at,
c.updated_at,
FROM c
`
)

type GormCurrencyRepository struct{}

func NewCurrencyRepository() currency.Repository {
return &GormCurrencyRepository{}
}

func (g *GormCurrencyRepository) GetPaginated(
ctx context.Context, params *currency.FindParams,
func (g *GormCurrencyRepository) queryChats(
ctx context.Context,
query string,
args ...interface{},
) ([]*currency.Currency, error) {
pool, err := composables.UseTx(ctx)
if err != nil {
return nil, err
}
where, args := []string{"1 = 1"}, []interface{}{}
if params.Code != "" {
where, args = append(where, fmt.Sprintf("code = $%d", len(args)+1)), append(args, params.Code)
}

rows, err := pool.Query(ctx, `
SELECT code, name, symbol, created_at, updated_at FROM currencies
WHERE `+strings.Join(where, " AND ")+`
`+repo.FormatLimitOffset(params.Limit, params.Offset)+`
`, args...)

rows, err := pool.Query(ctx, query, args...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -73,6 +77,43 @@ func (g *GormCurrencyRepository) GetPaginated(
return currencies, nil
}

func (g *GormCurrencyRepository) GetPaginated(
ctx context.Context, params *currency.FindParams,
) ([]*currency.Currency, error) {
sortFields := []string{}
for _, f := range params.SortBy.Fields {
switch f {
case currency.FieldCode:
sortFields = append(sortFields, "c.name")
case currency.FieldName:
sortFields = append(sortFields, "c.code")
case currency.FieldSymbol:
sortFields = append(sortFields, "c.symbol")
case currency.FieldCreatedAt:
sortFields = append(sortFields, "c.created_at")
default:
return nil, fmt.Errorf("unknown sort field: %v", f)
}
}

where, args := []string{"1 = 1"}, []interface{}{}
if params.Code != "" {
where = append(where, "c.code ILIKE $1")
args = append(args, "%"+params.Code+"%")
}

return g.queryChats(
ctx,
repo.Join(
selectCurrenciesQuery,
repo.JoinWhere(where...),
repo.OrderBy(sortFields, params.SortBy.Ascending),
repo.FormatLimitOffset(params.Limit, params.Offset),
),
args...,
)
}

func (g *GormCurrencyRepository) Count(ctx context.Context) (uint, error) {
pool, err := composables.UseTx(ctx)
if err != nil {
Expand Down Expand Up @@ -127,7 +168,7 @@ func (g *GormCurrencyRepository) Update(ctx context.Context, entity *currency.Cu
}
row := ToDBCurrency(entity)
if _, err := tx.Exec(ctx, `
UPDATE currencies
UPDATE currencies
SET name = $1, symbol = $2
WHERE code = $3
`, row.Name, row.Symbol, row.Code); err != nil {
Expand Down
Loading

0 comments on commit 84f53ec

Please sign in to comment.