54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type OperationStatus string
|
|
|
|
const (
|
|
OperationStatusRunning OperationStatus = "RUNNING"
|
|
OperationStatusSucceeded OperationStatus = "SUCCEEDED"
|
|
OperationStatusFailed OperationStatus = "FAILED"
|
|
OperationStatusCanceled OperationStatus = "CANCELED"
|
|
)
|
|
|
|
type Operation struct {
|
|
ID string
|
|
TargetID string
|
|
Type string
|
|
Status OperationStatus
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (o Operation) Validate() error {
|
|
if strings.TrimSpace(o.ID) == "" {
|
|
return fmt.Errorf("%w: operation id is required", ErrInvalidArgument)
|
|
}
|
|
if strings.TrimSpace(o.TargetID) == "" {
|
|
return fmt.Errorf("%w: operation target id is required", ErrInvalidArgument)
|
|
}
|
|
if strings.TrimSpace(o.Type) == "" {
|
|
return fmt.Errorf("%w: operation type is required", ErrInvalidArgument)
|
|
}
|
|
if !o.Status.Valid() {
|
|
return fmt.Errorf("%w: operation status %q", ErrInvalidStatus, o.Status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s OperationStatus) Valid() bool {
|
|
switch s {
|
|
case OperationStatusRunning,
|
|
OperationStatusSucceeded,
|
|
OperationStatusFailed,
|
|
OperationStatusCanceled:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|