Dawid Wysokiński
847ac51b38
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #18
89 lines
1.4 KiB
Go
89 lines
1.4 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
apiKeyMaxLength = 100
|
|
)
|
|
|
|
type APIKey struct {
|
|
ID int64
|
|
Name string
|
|
Key string
|
|
UserID int64
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
type CreateAPIKeyParams struct {
|
|
name string
|
|
key string
|
|
userID int64
|
|
}
|
|
|
|
func NewCreateAPIKeyParams(name, key string, userID int64) (CreateAPIKeyParams, error) {
|
|
if name == "" {
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
Field: "Name",
|
|
Err: ErrRequired,
|
|
}
|
|
}
|
|
|
|
if len(name) > apiKeyMaxLength {
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
Field: "Name",
|
|
Err: MaxLengthError{
|
|
Max: apiKeyMaxLength,
|
|
},
|
|
}
|
|
}
|
|
|
|
if key == "" {
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
Field: "Key",
|
|
Err: ErrRequired,
|
|
}
|
|
}
|
|
|
|
if userID < userIDMin {
|
|
return CreateAPIKeyParams{}, ValidationError{
|
|
Field: "UserID",
|
|
Err: MinError{
|
|
Min: userIDMin,
|
|
},
|
|
}
|
|
}
|
|
|
|
return CreateAPIKeyParams{name: name, key: key, userID: userID}, nil
|
|
}
|
|
|
|
func (c CreateAPIKeyParams) Name() string {
|
|
return c.name
|
|
}
|
|
|
|
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
|
|
}
|