From 2240617c8e0d5cf884553f7966c0c6d9288a1c80 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Fri, 5 Jan 2024 11:33:20 -0600 Subject: [PATCH] sort slice before return --- backend/app/api/handlers/v1/controller.go | 3 + .../internal/core/currencies/currencies.go | 13 +- .../internal/core/currencies/currencies.json | 189 +++++++++++++++++- 3 files changed, 202 insertions(+), 3 deletions(-) diff --git a/backend/app/api/handlers/v1/controller.go b/backend/app/api/handlers/v1/controller.go index ab2ff96..25f6aab 100644 --- a/backend/app/api/handlers/v1/controller.go +++ b/backend/app/api/handlers/v1/controller.go @@ -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()) } } diff --git a/backend/internal/core/currencies/currencies.go b/backend/internal/core/currencies/currencies.go index 28073a9..cec1a60 100644 --- a/backend/internal/core/currencies/currencies.go +++ b/backend/internal/core/currencies/currencies.go @@ -7,6 +7,7 @@ import ( _ "embed" "encoding/json" "io" + "slices" "sync" ) @@ -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 diff --git a/backend/internal/core/currencies/currencies.json b/backend/internal/core/currencies/currencies.json index fe51488..59d6b9f 100644 --- a/backend/internal/core/currencies/currencies.json +++ b/backend/internal/core/currencies/currencies.json @@ -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": "Kč", + "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": "zł", + "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" + } +]