-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
127 lines (106 loc) · 3.17 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package account
import (
"context"
"errors"
"time"
"github.com/joinself/restful-client/internal/entity"
"github.com/joinself/restful-client/pkg/log"
"github.com/joinself/self-go-sdk/fact"
)
// Service encapsulates usecase logic for accounts.
type Service interface {
Get(ctx context.Context, username, password string) (Account, error)
Create(ctx context.Context, input CreateAccountRequest) (Account, error)
SetPassword(ctx context.Context, username, password, newPassword string) error
Delete(ctx context.Context, username string) error
Count(ctx context.Context) (int, error)
List(ctx context.Context) []ExtAccount
}
// FactService service to manage sending and receiving fact requests
type FactService interface {
Request(*fact.FactRequest) (*fact.FactResponse, error)
}
// Account represents the data about an account.
type Account struct {
entity.Account
}
type service struct {
repo Repository
logger log.Logger
}
// NewService creates a new account service.
func NewService(repo Repository, logger log.Logger) Service {
return service{repo, logger}
}
// Get returns the account with the specified the account ID.
func (s service) Get(ctx context.Context, username, password string) (Account, error) {
account, err := s.repo.Get(ctx, username, password)
if err != nil {
return Account{}, err
}
return Account{account}, nil
}
// Create creates a new account.
func (s service) Create(ctx context.Context, req CreateAccountRequest) (Account, error) {
_, err := s.repo.GetByUsername(ctx, req.Username)
if err == nil {
return Account{}, errors.New("user already exists")
}
now := time.Now()
account := entity.Account{
UserName: req.Username,
Password: req.Password,
CreatedAt: now,
UpdatedAt: now,
RequiresPasswordChange: 1,
}
if req.RequiresPasswordChange != nil {
if *req.RequiresPasswordChange == false {
account.RequiresPasswordChange = 0
}
}
account.SetResources(req.Resources)
err = s.repo.Create(ctx, account)
if err != nil {
return Account{}, err
}
return s.Get(ctx, req.Username, req.Password)
}
// SetPassword updates the password for the given account id.
func (s service) SetPassword(ctx context.Context, username, password, newPassword string) error {
a, err := s.Get(ctx, username, password)
if err != nil {
return err
}
return s.repo.SetPassword(ctx, a.ID, newPassword)
}
// Delete deletes the account with the specified ID.
func (s service) Delete(ctx context.Context, username string) error {
account, err := s.repo.GetByUsername(ctx, username)
if err != nil {
return err
}
if err = s.repo.Delete(ctx, account.ID); err != nil {
return err
}
return nil
}
// Count returns the number of accounts.
func (s service) Count(ctx context.Context) (int, error) {
return s.repo.Count(ctx)
}
func (s service) List(ctx context.Context) []ExtAccount {
accounts, err := s.repo.List(ctx)
if err != nil {
return []ExtAccount{}
}
output := []ExtAccount{}
for _, a := range accounts {
output = append(output, ExtAccount{
UserName: a.UserName,
Resources: a.Resources,
RequiresPasswordChange: (a.RequiresPasswordChange != 0),
})
}
return output
}