fixed
This commit is contained in:
265
backend/internal/client/users/handler.go
Executable file
265
backend/internal/client/users/handler.go
Executable file
@@ -0,0 +1,265 @@
|
||||
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"
|
||||
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(getStaticsURL, h.GetStatics)
|
||||
router.GET(getOneStaticURL, h.GetOneStatic)
|
||||
router.GET(getCurrencyURL, h.GetCurrency)
|
||||
router.GET(getSettingURL, h.GetSetting)
|
||||
}
|
||||
|
||||
// =================================================================================
|
||||
|
||||
// GetNews godoc
|
||||
// @Description all news
|
||||
// @Tags news
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param limit query int false "limit"
|
||||
// @Param page query int false "page"
|
||||
// @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)
|
||||
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"
|
||||
// @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)
|
||||
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"
|
||||
// @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)
|
||||
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)
|
||||
}
|
||||
|
||||
// GetStatics godoc
|
||||
// @Description all static data
|
||||
// @Tags statics
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param limit query int false "limit"
|
||||
// @Param page query int false "page"
|
||||
// @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)
|
||||
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"
|
||||
// @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)
|
||||
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 per language)
|
||||
// @Tags currency
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} CurrencyDTO
|
||||
// @Router /client/users/currency [get]
|
||||
func (h *handler) GetCurrency(c *gin.Context) {
|
||||
result, err := h.repository.GetCurrency(c.Request.Context())
|
||||
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
|
||||
// @Success 200 {array} CategoryDTO
|
||||
// @Router /client/users/categories [get]
|
||||
func (h *handler) GetCategories(c *gin.Context) {
|
||||
result, err := h.repository.GetCategories(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = result
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
}
|
||||
Reference in New Issue
Block a user