sessions/internal/domain/api_key.go

67 lines
1.1 KiB
Go

package domain
import (
"fmt"
"time"
)
const (
apiKeyUserIDMin = 1
)
type APIKey struct {
ID int64
Key string
UserID int64
CreatedAt time.Time
}
type CreateAPIKeyParams struct {
key string
userID int64
}
func NewCreateAPIKeyParams(key string, userID int64) (CreateAPIKeyParams, error) {
if key == "" {
return CreateAPIKeyParams{}, ValidationError{
Field: "Key",
Err: ErrRequired,
}
}
if userID < apiKeyUserIDMin {
return CreateAPIKeyParams{}, ValidationError{
Field: "UserID",
Err: MinError{
Min: apiKeyUserIDMin,
},
}
}
return CreateAPIKeyParams{key: key, userID: userID}, nil
}
func (c CreateAPIKeyParams) Key() string {
return c.key
}
func (c CreateAPIKeyParams) UserID() int64 {
return c.userID
}
type APIKeyNotFoundError struct {
Key string
}
func (e APIKeyNotFoundError) Error() string {
return fmt.Sprintf("API key (key=%s) not found", e.Key)
}
func (e APIKeyNotFoundError) UserError() string {
return e.Error()
}
func (e APIKeyNotFoundError) Code() ErrorCode {
return ErrorCodeEntityNotFound
}