appresult.Localize applies to all /api/admin routes, so each handler's godoc now declares lang (tm, ru, en, all; default tm) and Swagger UI offers it as a dropdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
package paymentsettings
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"online-back/internal/appresult"
|
|
"online-back/internal/handlers"
|
|
"online-back/internal/identity/rbac"
|
|
"online-back/internal/identity/rbac/perm"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const baseURL = "/payment-settings"
|
|
|
|
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) {
|
|
group := rbac.Group(router, baseURL, rbac.StaffRead(perm.PaymentSettings))
|
|
group.GET("", h.get)
|
|
group.PUT("", h.update)
|
|
}
|
|
|
|
// get godoc
|
|
// @Summary Get payment settings
|
|
// @Description Returns the admin-configurable bank and GBÜS commission fees used on loan payments
|
|
// @Tags payment-settings
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm)
|
|
// @Success 200 {object} PaymentSettingsDTO
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /payment-settings [get]
|
|
func (h *handler) get(c *gin.Context) {
|
|
item, err := h.repository.Get(c.Request.Context())
|
|
if err != nil {
|
|
h.logger.Errorf("get payment settings failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// update godoc
|
|
// @Summary Update payment settings
|
|
// @Description Updates the bank and GBÜS commission fees used on loan payments
|
|
// @Tags payment-settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param body body UpdatePaymentSettingsDTO true "Payment settings payload"
|
|
// @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm)
|
|
// @Success 200 {object} PaymentSettingsDTO
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /payment-settings [put]
|
|
func (h *handler) update(c *gin.Context) {
|
|
var dto UpdatePaymentSettingsDTO
|
|
if err := c.ShouldBindJSON(&dto); err != nil {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
dto.UpdatedBy = currentUserID(c)
|
|
|
|
item, err := h.repository.Update(c.Request.Context(), dto)
|
|
if err != nil {
|
|
h.logger.Errorf("update payment settings failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
func currentUserID(c *gin.Context) *int64 {
|
|
rawID, exists := c.Get("id")
|
|
if !exists {
|
|
return nil
|
|
}
|
|
switch id := rawID.(type) {
|
|
case int64:
|
|
if id > 0 {
|
|
return &id
|
|
}
|
|
case int:
|
|
if id > 0 {
|
|
v := int64(id)
|
|
return &v
|
|
}
|
|
case string:
|
|
parsed, err := strconv.ParseInt(id, 10, 64)
|
|
if err == nil && parsed > 0 {
|
|
return &parsed
|
|
}
|
|
}
|
|
return nil
|
|
}
|