add update role feature

This commit is contained in:
Amir Tavakolian 2026-05-19 13:59:32 +03:30
parent 73b900d24e
commit 8aad672795
7 changed files with 124 additions and 26 deletions

View File

@ -17,8 +17,8 @@ func NewRoleHandler(service service.Authorization) RoleHandler {
func (r RoleHandler) Store(c *gin.Context) { func (r RoleHandler) Store(c *gin.Context) {
var request service.StoreRoleRequest var request service.StoreRoleRequest
err := c.ShouldBindJSON(&request) err := c.ShouldBindJSON(&request)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
@ -31,5 +31,20 @@ func (r RoleHandler) Store(c *gin.Context) {
} }
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("role %s created successfully", role.Title_fa)}) c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("role %s created successfully", role.Title_fa)})
return }
func (r RoleHandler) Update(c *gin.Context) {
var request service.UpdateRoleRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
role, err := r.service.Update(c, request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("role %s updated successfully", role.Title_fa)})
} }

View File

@ -1,9 +1,7 @@
package http package http
import ( import (
"fmt"
"git.gocasts.ir/ebhomengo/niki/pkg/httpserver" "git.gocasts.ir/ebhomengo/niki/pkg/httpserver"
"github.com/gin-gonic/gin"
) )
type RoleServer struct { type RoleServer struct {
@ -16,11 +14,5 @@ func NewRoleServer(cfg httpserver.Config, handler RoleHandler) RoleServer {
} }
func (s RoleServer) Start() { func (s RoleServer) Start() {
r := gin.Default()
r.POST("/role/create", s.handler.Store)
err := r.Run(fmt.Sprintf("%s:%s", s.cfg.Host, s.cfg.Port))
if err != nil {
panic(err.Error())
}
} }

View File

@ -3,9 +3,8 @@ package repository
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/domain/authorization/service" "git.gocasts.ir/ebhomengo/niki/domain/authorization/service"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/pkg/database/mysql" "git.gocasts.ir/ebhomengo/niki/pkg/database/mysql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/types" "git.gocasts.ir/ebhomengo/niki/types"
) )
@ -32,3 +31,34 @@ func (m RoleRepo) Store(ctx context.Context, req service.StoreRoleRequest) (type
return types.ID(roleID), nil return types.ID(roleID), nil
} }
func (m RoleRepo) Update(ctx context.Context, req service.UpdateRoleRequest) (types.ID, error) {
const op = "domain.authorization.repository.role.update"
result, err := m.conn.Conn().ExecContext(ctx,
"UPDATE roles SET title = ?, title_fa = ? WHERE id = ?",
req.Title, req.TitleFa, req.ID)
if err != nil {
return 0, richerror.New(op).WithErr(err)
}
_, err = result.RowsAffected()
if err != nil {
return 0, richerror.New(op).WithErr(err)
}
return types.ID(req.ID), nil
}
func (m RoleRepo) IsRoleExistsByID(ctx context.Context, id types.ID) error {
const op = "domain.authorization.repository.role.is_exists_by_id"
var exists bool
err := m.conn.Conn().QueryRowContext(ctx, "SELECT EXISTS(SELECT 1 FROM roles WHERE id = ?)", id).Scan(&exists)
if err != nil {
return richerror.New(op).WithErr(err)
}
return nil
}

View File

@ -4,8 +4,6 @@ import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/domain/authorization/entity" "git.gocasts.ir/ebhomengo/niki/domain/authorization/entity"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/go-ozzo/ozzo-validation/v4"
"regexp"
) )
type Authorization struct { type Authorization struct {
@ -34,16 +32,21 @@ func (a Authorization) Store(ctx context.Context, req StoreRoleRequest) (entity.
}, nil }, nil
} }
func (s Authorization) validateStoreRole(req StoreRoleRequest) error { func (a Authorization) Update(ctx context.Context, req UpdateRoleRequest) (entity.Role, error) {
return validation.ValidateStruct(&req, const op = "authorizationservice.Update"
validation.Field(&req.Title,
validation.Required,
validation.Length(4, 0),
validation.Match(regexp.MustCompile(`^[a-zA-Z\s]+$`))),
validation.Field(&req.TitleFa, if err := a.validateUpdateRole(ctx, req); err != nil {
validation.Required, return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
validation.Length(4, 0), }
validation.Match(regexp.MustCompile(`^[\x{0600}-\x{06FF}\s]+$`))),
) role, err := a.roleRepo.Update(ctx, req)
if err != nil {
return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return entity.Role{
ID: role,
Title_fa: req.TitleFa,
Title: req.Title,
}, nil
} }

View File

@ -1,6 +1,17 @@
package service package service
import "git.gocasts.ir/ebhomengo/niki/types"
type RoleRequest struct { type RoleRequest struct {
Title string `json:"title"` Title string `json:"title"`
TitleFa string `json:"title_fa"` TitleFa string `json:"title_fa"`
} }
type StoreRoleRequest struct {
RoleRequest
}
type UpdateRoleRequest struct {
ID types.ID `json:"id"`
RoleRequest
}

View File

@ -2,9 +2,11 @@ package service
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/types" "git.gocasts.ir/ebhomengo/niki/types"
) )
type RoleRepo interface { type RoleRepo interface {
Store(ctx context.Context, req StoreRoleRequest) (types.ID, error) Store(ctx context.Context, req StoreRoleRequest) (types.ID, error)
Update(ctx context.Context, req UpdateRoleRequest) (types.ID, error)
IsRoleExistsByID(ctx context.Context, id types.ID) error
} }

View File

@ -0,0 +1,45 @@
package service
import (
"context"
"github.com/go-ozzo/ozzo-validation/v4"
"regexp"
)
func (a Authorization) validateStoreRole(req StoreRoleRequest) error {
return validation.ValidateStruct(&req,
validation.Field(&req.Title,
validation.Required,
validation.Length(4, 0),
validation.Match(regexp.MustCompile(`^[a-zA-Z\s]+$`))),
validation.Field(&req.TitleFa,
validation.Required,
validation.Length(4, 0),
validation.Match(regexp.MustCompile(`^[\x{0600}-\x{06FF}\s]+$`))),
)
}
func (a Authorization) validateUpdateRole(ctx context.Context, req UpdateRoleRequest) error {
return validation.ValidateStruct(&req,
validation.Field(&req.ID,
validation.Required,
validation.By(func(value interface{}) error {
err := a.roleRepo.IsRoleExistsByID(ctx, req.ID)
if err != nil {
return err
}
return nil
}),
),
validation.Field(&req.Title,
validation.Required,
validation.Length(4, 0),
validation.Match(regexp.MustCompile(`^[a-zA-Z\s]+$`))),
validation.Field(&req.TitleFa,
validation.Required,
validation.Length(4, 0),
validation.Match(regexp.MustCompile(`^[\x{0600}-\x{06FF}\s]+$`))),
)
}