-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfairHandler.go
121 lines (100 loc) · 2.67 KB
/
fairHandler.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"strconv"
"strings"
"github.com/gofiber/fiber"
"github.com/rocketlaunchr/fairpricing/fair"
"github.com/rocketlaunchr/fairpricing/models"
)
type convLocalPrice struct {
OldLocalPrice *models.LocalPrice `json:"old_local_price"`
NewLocalPrice *models.LocalPrice `json:"new_local_price"`
}
// FairExchange godoc
// @Summary Show price in a local price of a given currency
// @Accept json
// @Produce json
// @Param locPrice path string true "Current Local Price"
// @Param countryCode path string true "Country Code to convert to"
// @Param currency path string true "Currency to convert to"
// @Success 200 {object} JsonResponse
// @Failure 400 {object} JsonErrorResponse
// @Failure 404 {object} JsonErrorResponse
// @Failure 500 {object} JsonErrorResponse
// @Router /fair/{locPrice}/{countryCode}/{currency} [get]
func FairExchange(c *fiber.Ctx) {
// locPrice e.g 100AUD@AU
locPrice := strings.Split(strings.ToUpper(c.Params("locPrice")), "@")
price, loc := locPrice[0], locPrice[1]
err := validateCountryCode(loc)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 400, Title: err.Error(),
},
}
c.Status(400).JSON(errorMsg)
return
}
currency := price[len(price)-3:]
err = validateCurrency(currency)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 400, Title: err.Error(),
},
}
c.Status(400).JSON(errorMsg)
return
}
amount, err := strconv.ParseFloat(price[:len(price)-3], 64)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 400, Title: err.Error(),
},
}
c.Status(400).JSON(errorMsg)
return
}
toCountryCode := strings.ToUpper(c.Params("countryCode"))
err = validateCountryCode(toCountryCode)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 400, Title: err.Error(),
},
}
c.Status(400).JSON(errorMsg)
return
}
localPrice := models.LocalPrice{Price: models.Price{Value: amount, Currency: currency}, CountryCode: loc}
toCurrency := strings.ToUpper(c.Params("currency"))
var toCur string
if toCurrency != "" {
err = validateCurrency(toCurrency)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 404, Title: err.Error(),
},
}
c.Status(404).JSON(errorMsg)
}
toCur = toCurrency
} else {
toCur = currency
}
np, err := fair.FairPrice(localPrice, toCountryCode, toCur)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 500, Title: err.Error(),
},
}
c.Status(500).JSON(errorMsg)
return
}
response := JsonResponse{Data: &convLocalPrice{OldLocalPrice: &localPrice, NewLocalPrice: &np}}
c.JSON(response)
}