-
Notifications
You must be signed in to change notification settings - Fork 0
/
priceHandler.go
93 lines (75 loc) · 2.08 KB
/
priceHandler.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
package main
import (
"strconv"
"strings"
"github.com/gofiber/fiber"
exchangerate "github.com/rocketlaunchr/fairpricing/exchange"
"github.com/rocketlaunchr/fairpricing/models"
)
type convPrice struct {
OldPrice *models.Price `json:"old_price"`
NewPrice *models.Price `json:"new_price"`
}
// PriceConvert godoc
// @Summary Show price From one currency value to another specified currency value
// @Accept json
// @Produce json
// @Param price path string true "Current Price (e.g. 50USD)"
// @Param toCurrency path string true "New Currency to convert price value to"
// @Param date path string false "Currency conversion timestamp history"
// @Success 200 {object} JsonResponse
// @Failure 400 {object} JsonErrorResponse
// @Failure 404 {object} JsonErrorResponse
// @Failure 500 {object} JsonErrorResponse
// @Router /convert/{price}/{toCurrency}/{date} [get]
func PriceConvert(c *fiber.Ctx) {
// price e.g. 10AUD
price := strings.ToUpper(c.Params("price"))
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
}
toCurrency := strings.ToUpper(c.Params("toCurrency"))
err = validateCurrency(toCurrency)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 404, Title: err.Error(),
},
}
c.Status(404).JSON(errorMsg)
return
}
date := c.Params("date") // not implemented for now
_ = date
p := models.Price{Value: amount, Currency: currency}
newPrice, err := exchangerate.ConvertExchangeRate(p, toCurrency)
if err != nil {
errorMsg := JsonErrorResponse{
Error: &ApiError{
Status: 500, Title: err.Error(),
},
}
c.Status(500).JSON(errorMsg)
return
}
response := JsonResponse{Data: &convPrice{OldPrice: &p, NewPrice: &newPrice}}
c.JSON(response)
}