Skip to content

Commit 62529d1

Browse files
committed
refactor: use constanta for sortby field in backend
1 parent b81c7ec commit 62529d1

File tree

19 files changed

+98
-44
lines changed

19 files changed

+98
-44
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package base_mysql
2+
3+
import "apps/api/domain/base"
4+
5+
func ToSortByColumn(sortBy base.SortBy) string {
6+
switch sortBy {
7+
case base.CreatedAt:
8+
return "created_at"
9+
default:
10+
return "created_at"
11+
}
12+
}
13+
14+
func ToOrderColumn(order base.Order) string {
15+
switch order {
16+
case base.Ascending:
17+
return "asc"
18+
case base.Descending:
19+
return "desc"
20+
default:
21+
return "asc"
22+
}
23+
}

apps/api/data/mysql/expenses/repository.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package expenses_mysql
22

33
import (
4+
base_mysql "apps/api/data/mysql/base"
5+
"apps/api/domain/base"
46
"apps/api/domain/expenses"
57
"apps/api/utils"
68
"context"
@@ -22,13 +24,9 @@ func (repo Repository) BeginTransaction(ctx context.Context, callback func(ctxWi
2224
return utils.BeginDbTransaction(ctx, repo.db, callback)
2325
}
2426

25-
func (repo Repository) GetExpenseList(ctx context.Context, sortBy string, order string, skip int, limit int) ([]expenses.Expense, error) {
27+
func (repo Repository) GetExpenseList(ctx context.Context, sortBy base.SortBy, order base.Order, skip int, limit int) ([]expenses.Expense, error) {
2628
var expenses []expenses.Expense
27-
result := repo.db.Table("expenses").Where("deleted_at is NULL").Preload("ExpenseItems").Preload("Wallet").Preload("Budget")
28-
29-
if sortBy != "" && order != "" {
30-
result = result.Order(fmt.Sprintf("%s %s", sortBy, order))
31-
}
29+
result := repo.db.Table("expenses").Where("deleted_at is NULL").Preload("ExpenseItems").Preload("Wallet").Preload("Budget").Order(fmt.Sprintf("%s %s", base_mysql.ToSortByColumn(sortBy), base_mysql.ToOrderColumn(order)))
3230

3331
if skip > 0 {
3432
result = result.Offset(skip)

apps/api/data/mysql/materials/repository.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package materials_mysql
22

33
import (
4+
base_mysql "apps/api/data/mysql/base"
5+
"apps/api/domain/base"
46
"apps/api/domain/materials"
57
"apps/api/utils"
68
"context"
@@ -22,14 +24,10 @@ func (repo Repository) BeginTransaction(ctx context.Context, callback func(ctxWi
2224
return utils.BeginDbTransaction(ctx, repo.db, callback)
2325
}
2426

25-
func (repo Repository) GetMaterialList(ctx context.Context, query string, sortBy string, order string, skip int, limit int) ([]materials.Material, error) {
27+
func (repo Repository) GetMaterialList(ctx context.Context, query string, sortBy base.SortBy, order base.Order, skip int, limit int) ([]materials.Material, error) {
2628
db := utils.GetDbFromCtx(ctx, repo.db)
2729
var categories []materials.Material
28-
result := db.Table("materials").Where("deleted_at", nil)
29-
30-
if sortBy != "" && order != "" {
31-
result = result.Order(fmt.Sprintf("%s %s", sortBy, order))
32-
}
30+
result := db.Table("materials").Where("deleted_at", nil).Order(fmt.Sprintf("%s %s", base_mysql.ToSortByColumn(sortBy), base_mysql.ToOrderColumn(order)))
3331

3432
if query != "" {
3533
result = result.Where("name LIKE ?", "%"+query+"%")

apps/api/data/mysql/products/repository.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package products_mysql
22

33
import (
4+
base_mysql "apps/api/data/mysql/base"
5+
"apps/api/domain/base"
46
"apps/api/domain/products"
57
"apps/api/utils"
68
"context"
@@ -22,15 +24,11 @@ func (repo Repository) BeginTransaction(ctx context.Context, callback func(ctxWi
2224
return utils.BeginDbTransaction(ctx, repo.db, callback)
2325
}
2426

25-
func (repo Repository) GetProductList(ctx context.Context, query string, sortBy string, order string, skip int, limit int) ([]products.Product, error) {
27+
func (repo Repository) GetProductList(ctx context.Context, query string, sortBy base.SortBy, order base.Order, skip int, limit int) ([]products.Product, error) {
2628
db := utils.GetDbFromCtx(ctx, repo.db)
2729

2830
var products []products.Product
29-
result := db.Table("products").Preload("Category").Preload("Materials").Preload("Materials.Material").Where("deleted_at", nil)
30-
31-
if sortBy != "" && order != "" {
32-
result = result.Order(fmt.Sprintf("%s %s", sortBy, order))
33-
}
31+
result := db.Table("products").Preload("Category").Preload("Materials").Preload("Materials.Material").Where("deleted_at", nil).Order(fmt.Sprintf("%s %s", base_mysql.ToSortByColumn(sortBy), base_mysql.ToOrderColumn(order)))
3432

3533
if query != "" {
3634
result = result.Where("name LIKE ?", "%"+query+"%")

apps/api/data/mysql/transactions/repository.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package transactions_mysql
22

33
import (
4+
base_mysql "apps/api/data/mysql/base"
5+
"apps/api/domain/base"
46
"apps/api/domain/transactions"
57
"apps/api/utils"
68
"context"
@@ -22,15 +24,11 @@ func (repo Repository) BeginTransaction(ctx context.Context, callback func(ctxWi
2224
return utils.BeginDbTransaction(ctx, repo.db, callback)
2325
}
2426

25-
func (repo Repository) GetTransactionList(ctx context.Context, query string, sortBy string, order string, skip int, limit int, paymentStatus transactions.PaymentStatus) ([]transactions.Transaction, error) {
27+
func (repo Repository) GetTransactionList(ctx context.Context, query string, sortBy base.SortBy, order base.Order, skip int, limit int, paymentStatus transactions.PaymentStatus) ([]transactions.Transaction, error) {
2628
db := utils.GetDbFromCtx(ctx, repo.db)
2729

2830
var transactionResults []transactions.Transaction
29-
result := db.Table("transactions").Where("deleted_at is NULL").Preload("TransactionItems").Preload("Wallet")
30-
31-
if sortBy != "" && order != "" {
32-
result = result.Order(fmt.Sprintf("%s %s", sortBy, order))
33-
}
31+
result := db.Table("transactions").Where("deleted_at is NULL").Preload("TransactionItems").Preload("Wallet").Order(fmt.Sprintf("%s %s", base_mysql.ToSortByColumn(sortBy), base_mysql.ToOrderColumn(order)))
3432

3533
if query != "" {
3634
result = result.Where("name LIKE ?", "%"+query+"%")

apps/api/data/mysql/wallets/repository.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package wallets_mysql
22

33
import (
4+
base_mysql "apps/api/data/mysql/base"
5+
"apps/api/domain/base"
46
"apps/api/domain/wallets"
57
"apps/api/utils"
68
"context"
@@ -55,15 +57,11 @@ func (repo Repository) DeleteWalletById(ctx context.Context, id int64) error {
5557
return result.Error
5658
}
5759

58-
func (repo Repository) GetWalletTransferList(ctx context.Context, walletId int64, sortBy string, order string, skip int, limit int) ([]wallets.WalletTransfer, error) {
60+
func (repo Repository) GetWalletTransferList(ctx context.Context, walletId int64, sortBy base.SortBy, order base.Order, skip int, limit int) ([]wallets.WalletTransfer, error) {
5961
db := utils.GetDbFromCtx(ctx, repo.db)
6062
var walletTransfers []wallets.WalletTransfer
6163

62-
result := db.Table("wallet_transfers").Preload("FromWallet").Preload("ToWallet").Where("deleted_at is NULL AND from_wallet_id = ?", walletId)
63-
64-
if sortBy != "" && order != "" {
65-
result = result.Order(fmt.Sprintf("%s %s", sortBy, order))
66-
}
64+
result := db.Table("wallet_transfers").Preload("FromWallet").Preload("ToWallet").Where("deleted_at is NULL AND from_wallet_id = ?", walletId).Order(fmt.Sprintf("%s %s", base_mysql.ToSortByColumn(sortBy), base_mysql.ToOrderColumn(order)))
6765

6866
if skip > 0 {
6967
result = result.Offset(skip)

apps/api/domain/base/entity.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package base
2+
3+
type SortBy int
4+
5+
const (
6+
CreatedAt SortBy = iota
7+
)
8+
9+
type Order int
10+
11+
const (
12+
Ascending Order = iota
13+
Descending
14+
)

apps/api/domain/expenses/repository.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package expenses
22

33
import (
4+
"apps/api/domain/base"
45
"context"
56
)
67

78
type Repository interface {
89
BeginTransaction(ctx context.Context, callback func(ctxWithTx context.Context) error) error
9-
GetExpenseList(ctx context.Context, sortBy string, order string, skip int, limit int) ([]Expense, error)
10+
GetExpenseList(ctx context.Context, sortBy base.SortBy, order base.Order, skip int, limit int) ([]Expense, error)
1011
GetExpenseById(ctx context.Context, id int64) (Expense, error)
1112
CreateExpense(ctx context.Context, expense *Expense) error
1213
UpdateExpenseById(ctx context.Context, expense *Expense, id int64) error

apps/api/domain/expenses/usecase.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package expenses
22

33
import (
4+
"apps/api/domain/base"
45
"apps/api/domain/budgets"
56
"apps/api/domain/wallets"
67
"context"
@@ -22,7 +23,7 @@ func NewUsecase(repository Repository, budgetRepository budgets.Repository, wall
2223
}
2324
}
2425

25-
func (usecase Usecase) GetExpenseList(ctx context.Context, sortBy string, order string, skip int, limit int) ([]Expense, error) {
26+
func (usecase Usecase) GetExpenseList(ctx context.Context, sortBy base.SortBy, order base.Order, skip int, limit int) ([]Expense, error) {
2627
return usecase.repository.GetExpenseList(ctx, sortBy, order, skip, limit)
2728
}
2829

apps/api/domain/materials/repository.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package materials
22

33
import (
4+
"apps/api/domain/base"
45
"context"
56
)
67

78
type Repository interface {
89
BeginTransaction(ctx context.Context, callback func(ctxWithTx context.Context) error) error
9-
GetMaterialList(ctx context.Context, query string, sortBy string, order string, skip int, limit int) ([]Material, error)
10+
GetMaterialList(ctx context.Context, query string, sortBy base.SortBy, order base.Order, skip int, limit int) ([]Material, error)
1011
GetMaterialListTotal(ctx context.Context, query string) (int64, error)
1112
GetMaterialById(ctx context.Context, id int64) (Material, error)
1213
CreateMaterial(ctx context.Context, materialRequest MaterialRequest) error

0 commit comments

Comments
 (0)