Files
RenewCMS/internal/infrastructure/persistence/mappers/roleMapper.go
T

61 lines
1.4 KiB
Go

package mappers
import (
"time"
"RenewCMS/internal/domain/role"
"RenewCMS/internal/infrastructure/persistence/models"
)
func RoleToDomain(m models.Role, grants []models.RolePermission) role.Role {
var editedAt time.Time
if m.EditedAt != nil {
editedAt = m.EditedAt.UTC()
}
return role.Role{
UpdatedAt: editedAt,
UpdatedBy: m.EditedBy,
ID: m.ID,
Name: m.Name,
Description: m.Description,
BuiltIn: m.BuiltIn,
Locked: m.Locked,
Version: m.Version,
Grants: GrantsToDomain(grants),
}
}
func RoleToModel(r role.Role) models.Role {
var editedAt *time.Time
if !r.UpdatedAt.IsZero() {
stamp := r.UpdatedAt
editedAt = &stamp
}
return models.Role{
EditedAt: editedAt,
EditedBy: r.UpdatedBy,
ID: r.ID,
Name: r.Name,
Description: r.Description,
BuiltIn: r.BuiltIn,
Locked: r.Locked,
Version: r.Version,
}
}
func GrantsToDomain(rows []models.RolePermission) []role.Grant {
grants := make([]role.Grant, 0, len(rows))
for _, row := range rows {
grants = append(grants, role.Grant{Key: row.Key, Scope: role.Scope(row.Scope)})
}
return grants
}
func GrantsToModel(roleID uint32, grants []role.Grant) []models.RolePermission {
rows := make([]models.RolePermission, 0, len(grants))
for _, grant := range grants {
rows = append(rows, models.RolePermission{RoleID: roleID, Key: grant.Key, Scope: string(grant.Scope)})
}
return rows
}