From 7cec7dbac804ff5b6ac20ae7f52252938794fa80 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Wed, 28 Jun 2023 21:41:59 +0800 Subject: [PATCH] sort result in overview response --- go.mod | 4 ++++ go.sum | 9 +++++++++ pkg/api/transactions.go | 16 +++++++++++----- pkg/models/transaction.go | 18 ++++++++++++++++++ third-patry-dependencies.json | 5 +++++ 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 1c3c0841..58d56093 100644 --- a/go.mod +++ b/go.mod @@ -16,13 +16,16 @@ require ( github.com/sirupsen/logrus v1.9.0 github.com/stretchr/testify v1.8.2 github.com/urfave/cli/v2 v2.25.4 + github.com/wk8/go-ordered-map/v2 v2.1.7 golang.org/x/crypto v0.9.0 gopkg.in/ini.v1 v1.67.0 xorm.io/xorm v1.3.2 ) require ( + github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d // indirect + github.com/buger/jsonparser v1.1.1 // indirect github.com/bytedance/sonic v1.8.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect @@ -37,6 +40,7 @@ require ( github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/leodido/go-urn v1.2.4 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/memcachier/mc/v3 v3.0.3 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect diff --git a/go.sum b/go.sum index 7e9b0323..73b8bf64 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6l github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= @@ -31,6 +33,8 @@ github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyX github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d h1:pVrfxiGfwelyab6n21ZBkbkmbevaf+WvMIiR7sr97hw= github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA= github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= @@ -226,6 +230,7 @@ github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -261,6 +266,8 @@ github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -419,6 +426,8 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli/v2 v2.25.4 h1:HyYwPrTO3im9rYhUff/ZNs78eolxt0nJ4LN+9yJKSH4= github.com/urfave/cli/v2 v2.25.4/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= +github.com/wk8/go-ordered-map/v2 v2.1.7 h1:aUZ1xBMdbvY8wnNt77qqo4nyT3y0pX4Usat48Vm+hik= +github.com/wk8/go-ordered-map/v2 v2.1.7/go.mod h1:9Xvgm2mV2kSq2SAm0Y608tBmu8akTzI7c2bz7/G7ZN4= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= diff --git a/pkg/api/transactions.go b/pkg/api/transactions.go index 8a3c7f1d..f78b5b08 100644 --- a/pkg/api/transactions.go +++ b/pkg/api/transactions.go @@ -4,6 +4,8 @@ import ( "sort" "strings" + orderedmap "github.com/wk8/go-ordered-map/v2" + "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/errs" "github.com/mayswind/ezbookkeeping/pkg/log" @@ -295,7 +297,7 @@ func (a *TransactionsApi) TransactionAmountsHandler(c *core.Context) (interface{ return nil, errs.Or(err, errs.ErrOperationFailed) } - amountsResp := make(map[string]*models.TransactionAmountsResponseItem) + amountsResp := orderedmap.New[string, *models.TransactionAmountsResponseItem]() for i := 0; i < len(requestItems); i++ { requestItem := requestItems[i] @@ -353,17 +355,19 @@ func (a *TransactionsApi) TransactionAmountsHandler(c *core.Context) (interface{ amountsMap[account.Currency] = totalAmounts } - allTotalAmounts := make([]*models.TransactionAmountsResponseItemAmountInfo, 0) + allTotalAmounts := make(models.TransactionAmountsResponseItemAmountInfoSlice, 0) for _, totalAmounts := range amountsMap { allTotalAmounts = append(allTotalAmounts, totalAmounts) } - amountsResp[requestItem.Name] = &models.TransactionAmountsResponseItem{ + sort.Sort(allTotalAmounts) + + amountsResp.Set(requestItem.Name, &models.TransactionAmountsResponseItem{ StartTime: requestItem.StartTime, EndTime: requestItem.EndTime, Amounts: allTotalAmounts, - } + }) } return amountsResp, nil @@ -463,12 +467,14 @@ func (a *TransactionsApi) TransactionMonthAmountsHandler(c *core.Context) (inter continue } - amounts := make([]*models.TransactionAmountsResponseItemAmountInfo, 0, len(monthTotalAmounts)) + amounts := make(models.TransactionAmountsResponseItemAmountInfoSlice, 0, len(monthTotalAmounts)) for _, monthTotalAmount := range monthTotalAmounts { amounts = append(amounts, monthTotalAmount) } + sort.Sort(amounts) + amountsResp = append(amountsResp, &models.TransactionMonthAmountsResponseItem{ Year: year, Month: month, diff --git a/pkg/models/transaction.go b/pkg/models/transaction.go index a1be9ded..24a54eea 100644 --- a/pkg/models/transaction.go +++ b/pkg/models/transaction.go @@ -451,3 +451,21 @@ func (s TransactionMonthAmountsResponseItemSlice) Less(i, j int) bool { return s[i].Month > s[j].Month } + +// TransactionAmountsResponseItemAmountInfoSlice represents the slice data structure of TransactionAmountsResponseItemAmountInfo +type TransactionAmountsResponseItemAmountInfoSlice []*TransactionAmountsResponseItemAmountInfo + +// Len returns the count of items +func (s TransactionAmountsResponseItemAmountInfoSlice) Len() int { + return len(s) +} + +// Swap swaps two items +func (s TransactionAmountsResponseItemAmountInfoSlice) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +// Less reports whether the first item is less than the second one +func (s TransactionAmountsResponseItemAmountInfoSlice) Less(i, j int) bool { + return strings.Compare(s[i].Currency, s[j].Currency) < 0 +} diff --git a/third-patry-dependencies.json b/third-patry-dependencies.json index fb3ffa9e..5f8e0ada 100644 --- a/third-patry-dependencies.json +++ b/third-patry-dependencies.json @@ -87,6 +87,11 @@ "url": "https://github.com/boombuler/barcode", "licenseUrl": "https://github.com/boombuler/barcode/blob/v1.0.1/LICENSE" }, + { + "name": "go-ordered-map", + "url": "https://github.com/wk8/go-ordered-map", + "licenseUrl": "https://github.com/wk8/go-ordered-map/blob/v2.1.7/LICENSE" + }, { "name": "Testify", "copyright": "Copyright (c) 2012-2020 Mat Ryer, Tyler Bunnell and contributors.",