This commit is contained in:
Amir Tavakolian 2026-05-19 12:03:52 +03:30
parent 7a54387722
commit 73b900d24e
5 changed files with 18 additions and 10 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.DB RoleRepo *repository.RoleRepo
AuthorizationService service.Authorization AuthorizationService service.Authorization
RoleHandler http.RoleHandler RoleHandler http.RoleHandler
RoleServer http.RoleServer RoleServer http.RoleServer
@ -40,6 +40,10 @@ 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

@ -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

View File

@ -9,15 +9,15 @@ import (
"git.gocasts.ir/ebhomengo/niki/types" "git.gocasts.ir/ebhomengo/niki/types"
) )
type DB struct { type RoleRepo struct {
conn *mysql.DB conn *mysql.DB
} }
func New(conn *mysql.DB) *DB { func New(conn *mysql.DB) *RoleRepo {
return &DB{conn: conn} return &RoleRepo{conn: conn}
} }
func (m DB) Store(ctx context.Context, req service.StoreRoleRequest) (types.ID, error) { func (m RoleRepo) 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)

View File

@ -19,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.validateRole(req); err != nil { if err := a.validateStoreRole(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)
} }
@ -27,10 +27,14 @@ 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 role, nil return entity.Role{
ID: role,
Title_fa: req.TitleFa,
Title: req.Title,
}, nil
} }
func (s Authorization) validateRole(req StoreRoleRequest) error { func (s Authorization) validateStoreRole(req StoreRoleRequest) error {
return validation.ValidateStruct(&req, return validation.ValidateStruct(&req,
validation.Field(&req.Title, validation.Field(&req.Title,
validation.Required, validation.Required,

View File

@ -1,6 +1,6 @@
package service package service
type StoreRoleRequest struct { type RoleRequest struct {
Title string `json:"title"` Title string `json:"title"`
TitleFa string `json:"title_fa"` TitleFa string `json:"title_fa"`
} }