ms-tester/pkg/models/role.go

54 lines
933 B
Go
Raw Normal View History

2024-08-16 11:05:29 +00:00
package models
2024-08-22 23:02:14 +00:00
import (
"errors"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
)
2024-08-16 11:05:29 +00:00
type Role int32
const (
RoleSpectator Role = 0
RoleParticipant Role = 1
RoleModerator Role = 2
RoleAdmin Role = 3
)
func (role Role) IsAdmin() bool {
return role == RoleAdmin
}
func (role Role) IsModerator() bool {
return role == RoleModerator
}
func (role Role) IsParticipant() bool {
return role == RoleParticipant
}
func (role Role) IsSpectator() bool {
return role == RoleSpectator
}
func (role Role) AtLeast(other Role) bool {
return role >= other
}
func (role Role) AtMost(other Role) bool {
return role <= other
}
2024-08-22 23:02:14 +00:00
var ErrBadRole = errors.New("bad role")
2024-08-16 11:05:29 +00:00
func (role Role) Valid() error {
switch role {
case RoleSpectator, RoleParticipant, RoleModerator, RoleAdmin:
return nil
}
2024-08-22 23:02:14 +00:00
return lib.ServiceError(ErrBadRole, lib.ErrValidationFailed, "bad role")
2024-08-16 11:05:29 +00:00
}
2024-08-20 11:18:23 +00:00
func (role Role) AsPointer() *Role {
return &role
}