Add branch_user table and branch-user assignment endpoints
Introduces a branch_user join table linking branches to users, plus create/delete API endpoints under the auth module for managing branch assignments. The create response includes the branch's localized name via a join. Regenerated swagger docs to include the new endpoints. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -10,13 +11,15 @@ import (
|
||||
"online-back/pkg/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const (
|
||||
loginURL = "/login"
|
||||
tokenURL = "/token"
|
||||
profileURL = "/profile"
|
||||
loginURL = "/login"
|
||||
tokenURL = "/token"
|
||||
profileURL = "/profile"
|
||||
branchUserURL = "/branch-users"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
@@ -34,6 +37,10 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
|
||||
protected := router.Group("", appresult.JwtTokenCheck())
|
||||
protected.GET(profileURL, h.getProfile)
|
||||
|
||||
branchUserGroup := router.Group(branchUserURL, appresult.JwtTokenCheck())
|
||||
branchUserGroup.POST("", h.createBranchUser)
|
||||
branchUserGroup.DELETE("/:id", h.deleteBranchUser)
|
||||
}
|
||||
|
||||
// login godoc
|
||||
@@ -148,6 +155,74 @@ func (h *handler) getProfile(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(profile))
|
||||
}
|
||||
|
||||
// createBranchUser godoc
|
||||
// @Summary Assign user to branch
|
||||
// @Description Creates a branch-user assignment
|
||||
// @Tags auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param body body CreateBranchUserDTO 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) createBranchUser(c *gin.Context) {
|
||||
var dto CreateBranchUserDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.CreateBranchUser(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))
|
||||
}
|
||||
|
||||
// deleteBranchUser godoc
|
||||
// @Summary Remove user from branch
|
||||
// @Description Deletes a branch-user assignment
|
||||
// @Tags auth
|
||||
// @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) deleteBranchUser(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.DeleteBranchUser(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) (int, bool) {
|
||||
rawID, exists := c.Get("id")
|
||||
if !exists {
|
||||
|
||||
Reference in New Issue
Block a user