forked from ebhomengo/niki
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
"git.gocasts.ir/ebhomengo/niki/types"
|
|
)
|
|
|
|
type RoleRepo struct {
|
|
conn *mysql.DB
|
|
}
|
|
|
|
func New(conn *mysql.DB) *RoleRepo {
|
|
return &RoleRepo{conn: conn}
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return 0, richerror.New(op).WithErr(err)
|
|
}
|
|
|
|
roleID, err := result.LastInsertId()
|
|
if err != nil {
|
|
return 0, richerror.New(op).WithErr(err)
|
|
}
|
|
|
|
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
|
|
}
|