ch some
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"online-back/internal/identity/userbranch"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
@@ -61,21 +62,10 @@ func (r *repository) GetByID(ctx context.Context, id int64) (userbranch.UserBran
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM branch_user WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
return genericrepo.Delete(ctx, r.client, "branch_user", id)
|
||||
}
|
||||
|
||||
type scannable interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
func scanUserBranch(row scannable) (userbranch.UserBranchDTO, error) {
|
||||
func scanUserBranch(row genericrepo.Scanner) (userbranch.UserBranchDTO, error) {
|
||||
var item userbranch.UserBranchDTO
|
||||
var branchNameJSON []byte
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ package userbranch
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -50,6 +50,10 @@ func (h *handler) create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !rbac.GuardNewBranchAssignment(c, dto.UserId) {
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Create(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
h.logger.Errorf("create branch user failed: %v", err)
|
||||
@@ -73,7 +77,7 @@ func (h *handler) create(c *gin.Context) {
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branch-users/{id} [delete]
|
||||
func (h *handler) delete(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
id, ok := shared.ParseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
@@ -105,11 +109,3 @@ func (h *handler) delete(c *gin.Context) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user