feat: add list of currencies
This commit is contained in:
@@ -9,8 +9,10 @@ import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/currencyService"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/logger"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/query_params"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/response"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
||||
|
||||
@@ -33,8 +35,9 @@ func NewCurrencyHandler() *CurrencyHandler {
|
||||
func CurrencyHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
handler := NewCurrencyHandler()
|
||||
|
||||
r.Post("/currency-rate", middleware.Require(perms.CurrencyWrite), handler.PostCurrencyRate)
|
||||
r.Get("/currency-rate/:id", handler.GetCurrencyRate)
|
||||
r.Patch("", middleware.Require(perms.CurrencyWrite), handler.PostCurrencyRate)
|
||||
r.Get("/list", handler.List)
|
||||
r.Get("/:id", handler.GetCurrencyRate)
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -50,7 +53,8 @@ func (h *CurrencyHandler) PostCurrencyRate(c fiber.Ctx) error {
|
||||
|
||||
logger.Error("failed to create currency rate",
|
||||
"handler", "CurrencyHandler.PostCurrencyRate",
|
||||
|
||||
"b2b_id_currency", currencyRate.B2bIdCurrency,
|
||||
"conversion_rate", currencyRate.ConversionRate,
|
||||
"error", err.Error(),
|
||||
)
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
@@ -65,16 +69,13 @@ func (h *CurrencyHandler) GetCurrencyRate(c fiber.Ctx) error {
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
|
||||
}
|
||||
|
||||
currency, err := h.CurrencyService.GetCurrency(uint(id))
|
||||
currency, err := h.CurrencyService.Get(uint(id))
|
||||
if err != nil {
|
||||
|
||||
logger.Error("failed to get currency",
|
||||
"handler", "CurrencyHandler.GetCurrencyRate",
|
||||
|
||||
"currency_id", id,
|
||||
"b2b_id_currency", id,
|
||||
"error", err.Error(),
|
||||
)
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
@@ -82,3 +83,37 @@ func (h *CurrencyHandler) GetCurrencyRate(c fiber.Ctx) error {
|
||||
|
||||
return c.JSON(response.Make(currency, 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
func (h *CurrencyHandler) List(c fiber.Ctx) error {
|
||||
langId, ok := localeExtractor.GetLangID(c)
|
||||
if !ok {
|
||||
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
|
||||
}
|
||||
|
||||
p, filt, err := query_params.ParseFilters[model.Currency](c, columnMappingCurrencies)
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
list, err := h.CurrencyService.Find(langId, p, filt)
|
||||
if err != nil {
|
||||
logger.Error("failed to get currency list",
|
||||
"handler", "CurrencyHandler.List",
|
||||
"lang_id", langId,
|
||||
"error", err.Error(),
|
||||
)
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(&list.Items, int(list.Count), i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
var columnMappingCurrencies map[string]string = map[string]string{
|
||||
"id": "c.id",
|
||||
"ps_id_currency": "c.ps_id_currency",
|
||||
"is_default": "c.is_default",
|
||||
"is_active": "c.is_active",
|
||||
"conversion_rate": "r.conversion_rate",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user