Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
sort slice before return
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Jan 5, 2024
1 parent 645155a commit 2240617
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 3 deletions.
3 changes: 3 additions & 0 deletions backend/app/api/handlers/v1/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ func (ctrl *V1Controller) HandleBase(ready ReadyFunc, build Build) errchain.Hand
// @Router /v1/currency [GET]
func (ctrl *V1Controller) HandleCurrency() errchain.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
// Set Cache for 10 Minutes
w.Header().Set("Cache-Control", "max-age=600")

return server.JSON(w, http.StatusOK, ctrl.svc.Currencies.Slice())
}
}
Expand Down
13 changes: 11 additions & 2 deletions backend/internal/core/currencies/currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
_ "embed"
"encoding/json"
"io"
"slices"
"sync"
)

Expand Down Expand Up @@ -71,9 +72,17 @@ func NewCurrencyService(currencies []Currency) *CurrencyRegistry {
func (cs *CurrencyRegistry) Slice() []Currency {
cs.mu.RLock()
defer cs.mu.RUnlock()
out := make([]Currency, 0, len(cs.registry))

keys := make([]string, 0, len(cs.registry))
for key := range cs.registry {
out = append(out, cs.registry[key])
keys = append(keys, key)
}

slices.Sort(keys)

out := make([]Currency, 0, len(cs.registry))
for i := range keys {
out = append(out, cs.registry[keys[i]])
}

return out
Expand Down
189 changes: 188 additions & 1 deletion backend/internal/core/currencies/currencies.json
Original file line number Diff line number Diff line change
@@ -1 +1,188 @@
[]
[
{
"code": "AED",
"local": "United Arab Emirates",
"symbol": "د.إ",
"name": "United Arab Emirates Dirham"
},
{
"code": "AUD",
"local": "Australia",
"symbol": "A$",
"name": "Australian Dollar"
},
{
"code": "BGN",
"local": "bg-BG",
"symbol": "lv",
"name": "Bulgarian lev"
},
{
"code": "BRL",
"local": "Brazil",
"symbol": "R$",
"name": "Brazilian Real"
},
{
"code": "CAD",
"local": "Canada",
"symbol": "C$",
"name": "Canadian Dollar"
},
{
"code": "CHF",
"local": "Switzerland",
"symbol": "CHF",
"name": "Swiss Franc"
},
{
"code": "CZK",
"local": "cs-CZ",
"symbol": "",
"name": "Czech Koruna"
},
{
"code": "DKK",
"local": "da-DK",
"symbol": "kr",
"name": "Danish Krone"
},
{
"code": "EUR",
"local": "Eurozone",
"symbol": "",
"name": "Euro"
},
{
"code": "GBP",
"local": "United Kingdom",
"symbol": "£",
"name": "British Pound Sterling"
},
{
"code": "HKD",
"local": "Hong Kong",
"symbol": "HK$",
"name": "Hong Kong Dollar"
},
{
"code": "IDR",
"local": "Indonesia",
"symbol": "Rp",
"name": "Indonesian Rupiah"
},
{
"code": "INR",
"local": "India",
"symbol": "",
"name": "Indian Rupee"
},
{
"code": "JPY",
"local": "Japan",
"symbol": "¥",
"name": "Japanese Yen"
},
{
"code": "KRW",
"local": "South Korea",
"symbol": "",
"name": "South Korean Won"
},
{
"code": "MXN",
"local": "Mexico",
"symbol": "Mex$",
"name": "Mexican Peso"
},
{
"code": "NOK",
"local": "Norway",
"symbol": "kr",
"name": "Norwegian Krone"
},
{
"code": "NZD",
"local": "New Zealand",
"symbol": "NZ$",
"name": "New Zealand Dollar"
},
{
"code": "PLN",
"local": "Poland",
"symbol": "",
"name": "Polish Zloty"
},
{
"code": "RMB",
"local": "zh-CN",
"symbol": "¥",
"name": "Chinese Yuan"
},
{
"code": "RON",
"local": "ro-RO",
"symbol": "lei",
"name": "Romanian Leu"
},
{
"code": "RUB",
"local": "Russia",
"symbol": "",
"name": "Russian Ruble"
},
{
"code": "SAR",
"local": "Saudi Arabia",
"symbol": "",
"name": "Saudi Riyal"
},
{
"code": "SEK",
"local": "Sweden",
"symbol": "kr",
"name": "Swedish Krona"
},
{
"code": "SGD",
"local": "Singapore",
"symbol": "S$",
"name": "Singapore Dollar"
},
{
"code": "THB",
"local": "Thailand",
"symbol": "฿",
"name": "Thai Baht"
},
{
"code": "TRY",
"local": "Turkey",
"symbol": "",
"name": "Turkish Lira"
},
{
"code": "USD",
"local": "United States",
"symbol": "$",
"name": "United States Dollar"
},
{
"code": "XAG",
"local": "Global",
"symbol": "XAG",
"name": "Silver Troy Ounce"
},
{
"code": "XAU",
"local": "Global",
"symbol": "XAU",
"name": "Gold Troy Ounce"
},
{
"code": "ZAR",
"local": "South Africa",
"symbol": "R",
"name": "South African Rand"
}
]

0 comments on commit 2240617

Please sign in to comment.