Skip to content

Commit 12b59f8

Browse files
author
Dev Agent
committed
Feat accounting price support sort by resource_id
1 parent 34e72db commit 12b59f8

File tree

5 files changed

+86
-13
lines changed

5 files changed

+86
-13
lines changed

accounting/router/api.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package router
33
import (
44
"fmt"
55

6+
"opencsg.com/csghub-server/builder/instrumentation"
7+
68
"github.com/gin-contrib/pprof"
79
"github.com/gin-gonic/gin"
810
"github.com/gin-gonic/gin/binding"
911
"github.com/go-playground/validator/v10"
1012
"opencsg.com/csghub-server/accounting/handler"
1113
"opencsg.com/csghub-server/api/middleware"
14+
bldmq "opencsg.com/csghub-server/builder/mq"
1215
"opencsg.com/csghub-server/common/config"
1316
"opencsg.com/csghub-server/mq"
1417
)
1518

16-
func NewAccountRouter(config *config.Config, mqHandler mq.MessageQueue) (*gin.Engine, error) {
19+
func NewAccountRouter(config *config.Config, mqHandler mq.MessageQueue, mqFactory bldmq.MessageQueueFactory) (*gin.Engine, error) {
1720
r := gin.New()
18-
r.Use(gin.Recovery())
19-
r.Use(middleware.Log())
21+
middleware.SetInfraMiddleware(r, config, instrumentation.Account)
2022
needAPIKey := middleware.NeedAPIKey(config)
2123
//add router for golang pprof
2224
debugGroup := r.Group("/debug", needAPIKey)
@@ -34,7 +36,7 @@ func NewAccountRouter(config *config.Config, mqHandler mq.MessageQueue) (*gin.En
3436
return nil, fmt.Errorf("error creating multi sync handler, error: %w", err)
3537
}
3638
createMeteringRoutes(apiGroup, meterHandler)
37-
err = createAdvancedRoutes(apiGroup, config, mqHandler)
39+
err = createAdvancedRoutes(apiGroup, config, mqHandler, mqFactory)
3840
if err != nil {
3941
return nil, fmt.Errorf("error creating accounting advanced routes, error:%w", err)
4042
}

api/router/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/gin-contrib/cors"
1212
"github.com/gin-contrib/pprof"
1313
"github.com/gin-gonic/gin"
14+
"github.com/gin-gonic/gin/binding"
15+
"github.com/go-playground/validator/v10"
1416
"github.com/prometheus/client_golang/prometheus/promhttp"
1517

1618
swaggerFiles "github.com/swaggo/files"
@@ -184,6 +186,7 @@ func NewRouter(config *config.Config, enableSwagger bool) (*gin.Engine, error) {
184186
r.Use(middleware.LocalizedErrorMiddleware())
185187
r.Use(middleware.Authenticator(config))
186188
useAdvancedMiddleware(r, config)
189+
createCustomValidator()
187190
apiGroup := r.Group("/api/v1")
188191

189192
versionHandler := handler.NewVersionHandler()
@@ -1327,3 +1330,7 @@ func createWebHookRoutes(apiGroup *gin.RouterGroup, middlewareCollection middlew
13271330
}
13281331
return nil
13291332
}
1333+
1334+
func createCustomValidator() {
1335+
_, _ = binding.Validator.Engine().(*validator.Validate)
1336+
}

builder/store/database/account_price.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,22 @@ func (a *accountPriceStoreImpl) ListBySkuType(ctx context.Context, req types.Acc
134134
if err != nil {
135135
return nil, 0, fmt.Errorf("failed to counting recorder, error: %w", err)
136136
}
137-
_, err = q.Order("sku_type ASC").Order("sku_kind ASC").Order("resource_id ASC").
138-
Order("sku_unit_type ASC").Order("created_at DESC").
137+
sortMap := map[string]string{
138+
"sku_type": "ASC",
139+
"sku_kind": "ASC",
140+
"resource_id": "ASC",
141+
"sku_unit_type": "ASC",
142+
"created_at": "DESC",
143+
}
144+
145+
if len(req.SortBy) > 0 && len(req.SortOrder) > 0 {
146+
sortMap[req.SortBy] = req.SortOrder
147+
}
148+
_, err = q.Order(fmt.Sprintf("%s %s", "sku_type", sortMap["sku_type"])).
149+
Order(fmt.Sprintf("%s %s", "sku_kind", sortMap["sku_kind"])).
150+
Order(fmt.Sprintf("%s %s", "resource_id", sortMap["resource_id"])).
151+
Order(fmt.Sprintf("%s %s", "sku_unit_type", sortMap["sku_unit_type"])).
152+
Order(fmt.Sprintf("%s %s", "created_at", sortMap["created_at"])).
139153
Limit(req.Per).Offset((req.Page-1)*req.Per).Exec(ctx, &result)
140154
if err != nil {
141155
return nil, 0, fmt.Errorf("select prices by type and kind and resource, error: %w", err)

builder/store/database/account_price_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,52 @@ func TestAccountPriceStore_ListBySkuType(t *testing.T) {
230230

231231
}
232232

233+
func TestAccountPriceStore_ListBySkuType_WithSort(t *testing.T) {
234+
db := tests.InitTestDB()
235+
defer db.Close()
236+
ctx := context.TODO()
237+
238+
store := database.NewAccountPriceStoreWithDB(db)
239+
240+
prices := []*database.AccountPrice{
241+
{
242+
SkuType: types.SKUCSGHub, SkuKind: types.SKUPackageAddon, ResourceID: "r1",
243+
SkuUnitType: "u", SkuDesc: "a",
244+
},
245+
{
246+
SkuType: types.SKUCSGHub, SkuKind: types.SKUPackageAddon, ResourceID: "r3",
247+
SkuUnitType: "u", SkuDesc: "b",
248+
},
249+
{
250+
SkuType: types.SKUCSGHub, SkuKind: types.SKUPackageAddon, ResourceID: "r2",
251+
SkuUnitType: "u", SkuDesc: "c",
252+
},
253+
}
254+
255+
for _, p := range prices {
256+
_, err := store.Create(ctx, *p)
257+
require.Nil(t, err)
258+
}
259+
260+
data, count, err := store.ListBySkuType(ctx, types.AcctPriceListDBReq{
261+
SkuType: types.SKUCSGHub,
262+
Page: 1,
263+
Per: 10,
264+
SortBy: "resource_id",
265+
SortOrder: "DESC",
266+
})
267+
require.Nil(t, err)
268+
require.Equal(t, 3, count)
269+
require.Equal(t, 3, len(data))
270+
require.Equal(t, "b/c/a", func(prices []database.AccountPrice) string {
271+
names := []string{}
272+
for _, p := range prices {
273+
names = append(names, p.SkuDesc)
274+
}
275+
return strings.Join(names, "/")
276+
}(data))
277+
}
278+
233279
func TestAccountPriceStore_ListByIds(t *testing.T) {
234280
db := tests.InitTestDB()
235281
defer db.Close()

common/types/accounting.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ type AcctPriceListDBReq struct {
359359
SkuType SKUType `json:"sku_type"`
360360
SkuKind string `json:"sku_kind"`
361361
ResourceID []string `json:"resource_id"`
362+
SortBy string `json:"sort_by"`
363+
SortOrder string `json:"sort_order"`
362364
Per int `json:"per"`
363365
Page int `json:"page"`
364366
}
@@ -367,16 +369,18 @@ type AcctPriceListDBReq struct {
367369
//
368370
// in accounting service and starhub server
369371
type AcctPriceListReq struct {
370-
SkuType SKUType `json:"sku_type"`
371-
SkuKind string `json:"sku_kind"`
372-
ResourceID []string `json:"resource_id"`
373-
Filter AcctPriceListFilter `json:"filter"`
374-
Per int `json:"per"`
375-
Page int `json:"page"`
372+
SkuType SKUType `json:"sku_type" form:"sku_type"`
373+
SkuKind string `json:"sku_kind" form:"sku_kind"`
374+
ResourceID []string `json:"resource_id" form:"resource_id"`
375+
Filter AcctPriceListFilter `json:"-"`
376+
SortBy string `json:"sort_by" form:"sort_by" binding:"omitempty,oneof=resource_id"`
377+
SortOrder string `json:"sort_order" form:"sort_order" binding:"omitempty,oneof=ASC DESC asc desc"`
378+
Per int `json:"per" form:"per,default=50" binding:"min=1,max=100"`
379+
Page int `json:"page" form:"page,default=1" binding:"min=1"`
376380
}
377381

378382
type AcctPriceListFilter struct {
379-
HardwareType string `json:"hardware_type"`
383+
HardwareType string `json:"hardware_type" form:"hardware_type"`
380384
}
381385

382386
type AcctRechargeReq struct {

0 commit comments

Comments
 (0)