Compare commits

..

No commits in common. "951ec222d645a713f16091f9139167ac292c6680" and "5502f0f98be53dbd53e4df0dab57781ae46a7e72" have entirely different histories.

8 changed files with 26 additions and 143 deletions

View File

@ -18,7 +18,7 @@ type Config struct {
MySQLDB mySql.Config `koanf:"mariadb"` MySQLDB mySql.Config `koanf:"mariadb"`
} }
type Application struct { type Application struct {
RoleRepo *repository.RoleRepo RoleRepo *repository.DB
AuthorizationService service.Authorization AuthorizationService service.Authorization
RoleHandler http.RoleHandler RoleHandler http.RoleHandler
RoleServer http.RoleServer RoleServer http.RoleServer
@ -40,10 +40,6 @@ func (a Application) Setup() Application {
} }
} }
func (a Application) Start() {
// todo implement role service start
}
func (a Application) getYamlConfigPath() Config { func (a Application) getYamlConfigPath() Config {
var appConfig Config var appConfig Config

View File

@ -31,20 +31,5 @@ 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

@ -17,10 +17,7 @@ func NewRoleServer(cfg httpserver.Config, handler RoleHandler) RoleServer {
func (s RoleServer) Start() { func (s RoleServer) Start() {
r := gin.Default() r := gin.Default()
group := r.Group("/role") r.POST("/role/create", s.handler.Store)
group.POST("/create", s.handler.Store)
group.PUT("/update/:id", s.handler.Update)
err := r.Run(fmt.Sprintf("%s:%s", s.cfg.Host, s.cfg.Port)) err := r.Run(fmt.Sprintf("%s:%s", s.cfg.Host, s.cfg.Port))
if err != nil { if err != nil {

View File

@ -3,20 +3,21 @@ package repository
import ( import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/domain/authorization/service" "git.gocasts.ir/ebhomengo/niki/domain/authorization/service"
"git.gocasts.ir/ebhomengo/niki/pkg/database/mysql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/pkg/database/mysql"
"git.gocasts.ir/ebhomengo/niki/types" "git.gocasts.ir/ebhomengo/niki/types"
) )
type RoleRepo struct { type DB struct {
conn *mysql.DB conn *mysql.DB
} }
func New(conn *mysql.DB) *RoleRepo { func New(conn *mysql.DB) *DB {
return &RoleRepo{conn: conn} return &DB{conn: conn}
} }
func (m RoleRepo) Store(ctx context.Context, req service.StoreRoleRequest) (types.ID, error) { func (m DB) Store(ctx context.Context, req service.StoreRoleRequest) (types.ID, error) {
const op = "domain.authorization.repository.role.store" const op = "domain.authorization.repository.role.store"
result, err := m.conn.Conn().ExecContext(ctx, "INSERT INTO roles VALUES (`?,?`)", req.Title, req.TitleFa) result, err := m.conn.Conn().ExecContext(ctx, "INSERT INTO roles VALUES (`?,?`)", req.Title, req.TitleFa)
@ -31,34 +32,3 @@ 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,6 +4,8 @@ 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 {
@ -17,7 +19,7 @@ func NewAuthorization(roleRepo RoleRepo) Authorization {
func (a Authorization) Store(ctx context.Context, req StoreRoleRequest) (entity.Role, error) { func (a Authorization) Store(ctx context.Context, req StoreRoleRequest) (entity.Role, error) {
const op = "authorizationservice.Store" const op = "authorizationservice.Store"
if err := a.validateStoreRole(req); err != nil { if err := a.validateRole(req); err != nil {
return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
@ -25,28 +27,19 @@ func (a Authorization) Store(ctx context.Context, req StoreRoleRequest) (entity.
if err != nil { if err != nil {
return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
return entity.Role{ return role, nil
ID: role,
Title_fa: req.TitleFa,
Title: req.Title,
}, nil
} }
func (a Authorization) Update(ctx context.Context, req UpdateRoleRequest) (entity.Role, error) { func (s Authorization) validateRole(req StoreRoleRequest) error {
const op = "authorizationservice.Update" return validation.ValidateStruct(&req,
validation.Field(&req.Title,
validation.Required,
validation.Length(4, 0),
validation.Match(regexp.MustCompile(`^[a-zA-Z\s]+$`))),
if err := a.validateUpdateRole(ctx, req); err != nil { validation.Field(&req.TitleFa,
return entity.Role{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) validation.Required,
} 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,17 +1,6 @@
package service package service
import "git.gocasts.ir/ebhomengo/niki/types" type StoreRoleRequest 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,11 +2,9 @@ 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

@ -1,45 +0,0 @@
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]+$`))),
)
}