forked from ebhomengo/niki
add update role feature
This commit is contained in:
parent
73b900d24e
commit
8aad672795
|
|
@ -17,8 +17,8 @@ func NewRoleHandler(service service.Authorization) RoleHandler {
|
|||
|
||||
func (r RoleHandler) Store(c *gin.Context) {
|
||||
var request service.StoreRoleRequest
|
||||
|
||||
err := c.ShouldBindJSON(&request)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
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)})
|
||||
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)})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.gocasts.ir/ebhomengo/niki/pkg/httpserver"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type RoleServer struct {
|
||||
|
|
@ -16,11 +14,5 @@ func NewRoleServer(cfg httpserver.Config, handler RoleHandler) RoleServer {
|
|||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,8 @@ package repository
|
|||
import (
|
||||
"context"
|
||||
"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"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
"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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import (
|
|||
"context"
|
||||
"git.gocasts.ir/ebhomengo/niki/domain/authorization/entity"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
"github.com/go-ozzo/ozzo-validation/v4"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Authorization struct {
|
||||
|
|
@ -34,16 +32,21 @@ func (a Authorization) Store(ctx context.Context, req StoreRoleRequest) (entity.
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (s 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]+$`))),
|
||||
func (a Authorization) Update(ctx context.Context, req UpdateRoleRequest) (entity.Role, error) {
|
||||
const op = "authorizationservice.Update"
|
||||
|
||||
validation.Field(&req.TitleFa,
|
||||
validation.Required,
|
||||
validation.Length(4, 0),
|
||||
validation.Match(regexp.MustCompile(`^[\x{0600}-\x{06FF}\s]+$`))),
|
||||
)
|
||||
if err := a.validateUpdateRole(ctx, req); err != nil {
|
||||
return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
package service
|
||||
|
||||
import "git.gocasts.ir/ebhomengo/niki/types"
|
||||
|
||||
type RoleRequest struct {
|
||||
Title string `json:"title"`
|
||||
TitleFa string `json:"title_fa"`
|
||||
}
|
||||
|
||||
type StoreRoleRequest struct {
|
||||
RoleRequest
|
||||
}
|
||||
|
||||
type UpdateRoleRequest struct {
|
||||
ID types.ID `json:"id"`
|
||||
RoleRequest
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@ package service
|
|||
|
||||
import (
|
||||
"context"
|
||||
"git.gocasts.ir/ebhomengo/niki/types"
|
||||
"git.gocasts.ir/ebhomengo/niki/types"
|
||||
)
|
||||
|
||||
type RoleRepo interface {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]+$`))),
|
||||
)
|
||||
}
|
||||
Loading…
Reference in New Issue