endpoints returning languages, countries and currencies

This commit is contained in:
Daniel Goc
2026-03-18 14:12:33 +01:00
parent 6cebcacb5d
commit 01c8f4333f
7 changed files with 160 additions and 5 deletions

View File

@@ -0,0 +1,57 @@
package restricted
import (
"git.ma-al.com/goc_daniel/b2b/app/service/jwtService"
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
"git.ma-al.com/goc_daniel/b2b/app/utils/response"
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
"github.com/gofiber/fiber/v3"
)
// JWTCookiesHandler for updating JWT cookies.
type JWTCookiesHandler struct {
jwtService *jwtService.JWTService
}
// NewJWTCookiesHandler creates a new JWTCookiesHandler instance
func NewJWTCookiesHandler() *JWTCookiesHandler {
jwtService := jwtService.New()
return &JWTCookiesHandler{
jwtService: jwtService,
}
}
func JWTCookiesHandlerRoutes(r fiber.Router) fiber.Router {
handler := NewJWTCookiesHandler()
r.Get("/get-languages", handler.GetLanguages)
r.Get("/get-countries", handler.GetCountries)
r.Get("/update-choice", handler.UpdateChoice)
return r
}
func (h *JWTCookiesHandler) GetLanguages(c fiber.Ctx) error {
languages, err := h.jwtService.GetLanguages()
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&languages, 0, i18n.T_(c, response.Message_OK)))
}
func (h *JWTCookiesHandler) GetCountries(c fiber.Ctx) error {
countries, err := h.jwtService.GetCountriesAndCurrencies()
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&countries, 0, i18n.T_(c, response.Message_OK)))
}
func (h *JWTCookiesHandler) UpdateChoice(c fiber.Ctx) error {
return nil
}

View File

@@ -97,6 +97,11 @@ func (s *Server) Setup() error {
listProducts := s.restricted.Group("/list-products")
restricted.ListProductsHandlerRoutes(listProducts)
// changing the JWT cookies routes (restricted)
// in reality it just handles changing user's country and language
jwtUpdates := s.restricted.Group("/jwt-updates")
restricted.JWTCookiesHandlerRoutes(jwtUpdates)
// // Restricted routes example
// restricted := s.api.Group("/restricted")
// restricted.Use(middleware.AuthMiddleware())

11
app/model/countries.go Normal file
View File

@@ -0,0 +1,11 @@
package model
// Represents a country together with its associated currency
type Country struct {
ID uint `gorm:"primaryKey;column:id" json:"id"`
Name string `gorm:"column:name" json:"name"`
Flag string `gorm:"size:16;not null;column:flag" json:"flag"`
CurrencyID uint `gorm:"column:id_currency" json:"currency_id"`
CurrencyISOCode string `gorm:"column:iso_code" json:"currency_iso_code"`
CurrencyName string `gorm:"column:name" json:"currency_name"`
}

View File

@@ -27,8 +27,8 @@ type JWTClaims struct {
Email string `json:"email"`
Username string `json:"username"`
Role model.CustomerRole `json:"customer_role"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
CartsIDs []uint `json:"carts_ids"`
CountryID uint `json:"country_id"`
jwt.RegisteredClaims
}
@@ -476,8 +476,8 @@ func (s *AuthService) generateAccessToken(user *model.Customer) (string, error)
Email: user.Email,
Username: user.Email,
Role: user.Role,
FirstName: user.FirstName,
LastName: user.LastName,
CartsIDs: []uint{},
CountryID: 1,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Duration(s.config.JWTExpiration) * time.Second)),
IssuedAt: jwt.NewNumericDate(time.Now()),

View File

@@ -0,0 +1,28 @@
package jwtService
import (
"git.ma-al.com/goc_daniel/b2b/app/model"
"git.ma-al.com/goc_daniel/b2b/repository/jwtFieldsRepo"
)
// jwtService handles updating JWT cookies
type JWTService struct {
repo jwtFieldsRepo.JWTFieldsRepo
}
// NewJWTService creates a new JWT service
func New() *JWTService {
return &JWTService{}
}
func (s *JWTService) GetLanguages() ([]model.Language, error) {
return s.repo.GetLanguages()
}
func (s *JWTService) GetCountriesAndCurrencies() ([]model.Country, error) {
return s.repo.GetCountriesAndCurrencies()
}
func (s *JWTService) UpdateChoice() error {
return nil
}