Client endpoints now return a single-language string (default tm) so the frontend can pass ?lang= and skip picking from nested tm/ru/en objects. Co-authored-by: Cursor <cursoragent@cursor.com>
306 lines
8.6 KiB
Go
Executable File
306 lines
8.6 KiB
Go
Executable File
package users
|
|
|
|
import (
|
|
"bankline/internal/appresult"
|
|
"bankline/internal/handlers"
|
|
"bankline/pkg/logging"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
getNewsURL = "/news"
|
|
getOneNewsURL = "/news/:id"
|
|
getCategoriesURL = "/categories"
|
|
getOneSubCategoryURL = "/sub-categories/:id"
|
|
getBannersURL = "/banners"
|
|
getOneBannerURL = "/banners/:id"
|
|
getStaticsURL = "/static"
|
|
getOneStaticURL = "/static/:id"
|
|
getCurrencyURL = "/currency"
|
|
getSettingURL = "/setting"
|
|
)
|
|
|
|
type handler struct {
|
|
logger *logging.Logger
|
|
repository Repository
|
|
}
|
|
|
|
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
|
return &handler{
|
|
logger: logger,
|
|
repository: repository,
|
|
}
|
|
}
|
|
|
|
func (h *handler) Register(router *gin.RouterGroup) {
|
|
router.GET(getNewsURL, h.GetNews)
|
|
router.GET(getOneNewsURL, h.GetOneNews)
|
|
router.GET(getCategoriesURL, h.GetCategories)
|
|
router.GET(getOneSubCategoryURL, h.GetSubCategory)
|
|
router.GET(getBannersURL, h.GetBanners)
|
|
router.GET(getOneBannerURL, h.GetOneBanner)
|
|
router.GET(getStaticsURL, h.GetStatics)
|
|
router.GET(getOneStaticURL, h.GetOneStatic)
|
|
router.GET(getCurrencyURL, h.GetCurrency)
|
|
router.GET(getSettingURL, h.GetSetting)
|
|
}
|
|
|
|
func queryLang(c *gin.Context) string {
|
|
return NormalizeLang(c.Query("lang"))
|
|
}
|
|
|
|
// =================================================================================
|
|
|
|
// GetNews godoc
|
|
// @Description all news
|
|
// @Tags news
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param limit query int false "limit"
|
|
// @Param page query int false "page"
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {object} DataNewsDTO
|
|
// @Router /client/users/news [get]
|
|
func (h *handler) GetNews(c *gin.Context) {
|
|
limitS, _ := c.GetQuery("limit")
|
|
pageS, _ := c.GetQuery("page")
|
|
|
|
limit, _ := strconv.Atoi(limitS)
|
|
page, _ := strconv.Atoi(pageS)
|
|
|
|
result, err := h.repository.GetNews(c.Request.Context(), limit, page, queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetOneNews godoc
|
|
// @Description news by id
|
|
// @Tags news
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "news ID"
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {object} NewsDTO
|
|
// @Router /client/users/news/{id} [get]
|
|
func (h *handler) GetOneNews(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotAcceptable)
|
|
return
|
|
}
|
|
|
|
result, err := h.repository.GetNewsById(c.Request.Context(), id, queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetSubCategory godoc
|
|
// @Description sub-category by id
|
|
// @Tags categories
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "sub-category ID"
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {object} SubCategoryDTO
|
|
// @Router /client/users/sub-categories/{id} [get]
|
|
func (h *handler) GetSubCategory(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotAcceptable)
|
|
return
|
|
}
|
|
|
|
result, err := h.repository.GetSubCategoryById(c.Request.Context(), id, queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetBanners godoc
|
|
// @Description all banners
|
|
// @Tags banners
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param limit query int false "limit"
|
|
// @Param page query int false "page"
|
|
// @Success 200 {object} DataBannerDTO
|
|
// @Router /client/users/banners [get]
|
|
func (h *handler) GetBanners(c *gin.Context) {
|
|
limitS, _ := c.GetQuery("limit")
|
|
pageS, _ := c.GetQuery("page")
|
|
|
|
limit, _ := strconv.Atoi(limitS)
|
|
page, _ := strconv.Atoi(pageS)
|
|
|
|
result, err := h.repository.GetBanners(c.Request.Context(), limit, page)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetOneBanner godoc
|
|
// @Description banner by id
|
|
// @Tags banners
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "banner ID"
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {object} BannerDTO
|
|
// @Router /client/users/banners/{id} [get]
|
|
func (h *handler) GetOneBanner(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotAcceptable)
|
|
return
|
|
}
|
|
|
|
result, err := h.repository.GetBannerById(c.Request.Context(), id, queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetStatics godoc
|
|
// @Description all static data
|
|
// @Tags statics
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param limit query int false "limit"
|
|
// @Param page query int false "page"
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {object} DataStaticDTO
|
|
// @Router /client/users/static [get]
|
|
func (h *handler) GetStatics(c *gin.Context) {
|
|
limitS, _ := c.GetQuery("limit")
|
|
pageS, _ := c.GetQuery("page")
|
|
|
|
limit, _ := strconv.Atoi(limitS)
|
|
page, _ := strconv.Atoi(pageS)
|
|
|
|
result, err := h.repository.GetStatics(c.Request.Context(), limit, page, queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetOneStatic godoc
|
|
// @Description static data by id
|
|
// @Tags statics
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "static ID"
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {object} StaticDTO
|
|
// @Router /client/users/static/{id} [get]
|
|
func (h *handler) GetOneStatic(c *gin.Context) {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotAcceptable)
|
|
return
|
|
}
|
|
|
|
result, err := h.repository.GetStaticById(c.Request.Context(), id, queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetCurrency godoc
|
|
// @Description latest currency content (title + HTML table for requested language)
|
|
// @Tags currency
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {object} CurrencyDTO
|
|
// @Router /client/users/currency [get]
|
|
func (h *handler) GetCurrency(c *gin.Context) {
|
|
result, err := h.repository.GetCurrency(c.Request.Context(), queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetSetting godoc
|
|
// @Description site setting (logo and contact numbers)
|
|
// @Tags settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} SettingDTO
|
|
// @Router /client/users/setting [get]
|
|
func (h *handler) GetSetting(c *gin.Context) {
|
|
result, err := h.repository.GetSetting(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|
|
|
|
// GetCategories godoc
|
|
// @Description all active categories with their active subcategories
|
|
// @Tags categories
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param lang query string false "tm|ru|en (default tm)"
|
|
// @Success 200 {array} CategoryDTO
|
|
// @Router /client/users/categories [get]
|
|
func (h *handler) GetCategories(c *gin.Context) {
|
|
result, err := h.repository.GetCategories(c.Request.Context(), queryLang(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
}
|