153 lines
4.0 KiB
Go
153 lines
4.0 KiB
Go
package userbranch
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"online-back/internal/appresult"
|
|
"online-back/internal/handlers"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
const baseURL = "/branch-users"
|
|
|
|
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 := router.Group(baseURL, appresult.JwtTokenCheck())
|
|
group.GET("", h.list)
|
|
group.POST("", h.create)
|
|
group.DELETE("/:id", h.delete)
|
|
}
|
|
|
|
// list godoc
|
|
// @Summary List branch users
|
|
// @Description Returns the authenticated user's branch-user assignments with the related user and branch
|
|
// @Tags branch-users
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param branch_name query string false "Filter by branch name (en, ru, tm)"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 401 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /branch-users [get]
|
|
func (h *handler) list(c *gin.Context) {
|
|
userID, ok := currentUserID(c)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, appresult.ErrUnauthorized)
|
|
return
|
|
}
|
|
|
|
filter := ListFilter{
|
|
UserId: &userID,
|
|
BranchName: strings.TrimSpace(c.Query("branch_name")),
|
|
}
|
|
|
|
result, err := h.repository.List(c.Request.Context(), filter)
|
|
if err != nil {
|
|
h.logger.Errorf("list branch users failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// create godoc
|
|
// @Summary Assign user to branch
|
|
// @Description Creates a branch-user assignment
|
|
// @Tags branch-users
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param body body CreateUserBranchDTO true "Branch-user payload"
|
|
// @Success 201 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /branch-users [post]
|
|
func (h *handler) create(c *gin.Context) {
|
|
var dto CreateUserBranchDTO
|
|
if err := c.ShouldBindJSON(&dto); err != nil {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
item, err := h.repository.Create(c.Request.Context(), dto)
|
|
if err != nil {
|
|
h.logger.Errorf("create branch user failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// delete godoc
|
|
// @Summary Remove user from branch
|
|
// @Description Deletes a branch-user assignment
|
|
// @Tags branch-users
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Branch-user ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /branch-users/{id} [delete]
|
|
func (h *handler) delete(c *gin.Context) {
|
|
id, ok := parseID(c.Param("id"))
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
} else if err != nil {
|
|
h.logger.Errorf("delete branch user failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData("branch user removed"))
|
|
}
|
|
|
|
func parseID(raw string) (int64, bool) {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil || id <= 0 {
|
|
return 0, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
func currentUserID(c *gin.Context) (int64, bool) {
|
|
rawID, exists := c.Get("id")
|
|
if !exists {
|
|
return 0, false
|
|
}
|
|
|
|
switch id := rawID.(type) {
|
|
case int64:
|
|
return id, id > 0
|
|
case int:
|
|
return int64(id), id > 0
|
|
case string:
|
|
return parseID(id)
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|