forked from ebhomengo/niki
refactor
This commit is contained in:
parent
7a54387722
commit
73b900d24e
|
|
@ -18,7 +18,7 @@ type Config struct {
|
|||
MySQLDB mySql.Config `koanf:"mariadb"`
|
||||
}
|
||||
type Application struct {
|
||||
RoleRepo *repository.DB
|
||||
RoleRepo *repository.RoleRepo
|
||||
AuthorizationService service.Authorization
|
||||
RoleHandler http.RoleHandler
|
||||
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 {
|
||||
var appConfig Config
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
err := c.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
|
|
|||
|
|
@ -9,15 +9,15 @@ import (
|
|||
"git.gocasts.ir/ebhomengo/niki/types"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
type RoleRepo struct {
|
||||
conn *mysql.DB
|
||||
}
|
||||
|
||||
func New(conn *mysql.DB) *DB {
|
||||
return &DB{conn: conn}
|
||||
func New(conn *mysql.DB) *RoleRepo {
|
||||
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"
|
||||
|
||||
result, err := m.conn.Conn().ExecContext(ctx, "INSERT INTO roles VALUES (`?,?`)", req.Title, req.TitleFa)
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func NewAuthorization(roleRepo RoleRepo) Authorization {
|
|||
func (a Authorization) Store(ctx context.Context, req StoreRoleRequest) (entity.Role, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
@ -27,10 +27,14 @@ func (a Authorization) Store(ctx context.Context, req StoreRoleRequest) (entity.
|
|||
if err != nil {
|
||||
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,
|
||||
validation.Field(&req.Title,
|
||||
validation.Required,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package service
|
||||
|
||||
type StoreRoleRequest struct {
|
||||
type RoleRequest struct {
|
||||
Title string `json:"title"`
|
||||
TitleFa string `json:"title_fa"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue