-
Notifications
You must be signed in to change notification settings - Fork 0
/
currencies.go
101 lines (87 loc) · 2.25 KB
/
currencies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"sort"
"strings"
"github.com/gofiber/fiber"
exchangerate "github.com/rocketlaunchr/fairpricing/exchange"
"github.com/rocketlaunchr/fairpricing/fair"
)
var cnames = map[string]string{
"EUR": "Euro",
"USD": "US dollar",
"JPY": "Japanese yen",
"BGN": "Bulgarian lev",
"CZK": "Czech koruna",
"DKK": "Danish krone",
"GBP": "Pound sterling",
"HUF": "Hungarian forint",
"PLN": "Polish zloty",
"RON": "Romanian leu",
"SEK": "Swedish krona",
"CHF": "Swiss franc",
"ISK": "Icelandic krona",
"NOK": "Norwegian krone",
"HRK": "Croatian kuna",
"RUB": "Russian rouble",
"TRY": "Turkish lira",
"AUD": "Australian dollar",
"BRL": "Brazilian real",
"CAD": "Canadian dollar",
"CNY": "Chinese yuan renminbi",
"HKD": "Hong Kong dollar",
"IDR": "Indonesian rupiah",
"ILS": "Israeli shekel",
"INR": "Indian rupee",
"KRW": "South Korean won",
"MXN": "Mexican peso",
"MYR": "Malaysian ringgit",
"NZD": "New Zealand dollar",
"PHP": "Philippine peso",
"SGD": "Singapore dollar",
"THB": "Thai baht",
"ZAR": "South African rand",
}
type Currency struct {
Code string `json:"code"` // Currency code
Name string `json:"name"`
}
type Country struct {
Code string `json:"code"` // ISO 3166-1 alpha-2 codes
Name string `json:"name"` // Title Case
}
type CurrenciesResponse struct {
Currencies []Currency `json:"currencies"`
Countries []Country `json:"countries"`
}
// CurrenciesHandler godoc
// @Summary Returns all currencies and countries recognized.
// @Produce json
// @Success 200 {object} CurrenciesResponse
// @Router /currencies [get]
func CurrenciesHandler(c *fiber.Ctx) {
ac := exchangerate.AllCurrencies()
cs := []Currency{}
for _, c := range ac {
cs = append(cs, Currency{
Code: c,
Name: cnames[c],
})
}
scs := []Country{} // supported countries
// We want all countries with a key in Ccodes and also exists in PppGDPperCapita
for countryCode, v := range fair.Ccodes {
country := strings.ToUpper(v.Name)
if _, exists := fair.PppGDPperCapita[country]; exists {
scs = append(scs, Country{
Code: countryCode,
Name: strings.Title(v.Name),
})
}
}
sort.Slice(scs, func(i, j int) bool { return scs[i].Name < scs[j].Name })
resp := CurrenciesResponse{
Currencies: cs,
Countries: scs,
}
c.JSON(resp)
}