This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
92 lines (77 loc) · 2.57 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
package audit_logs
import (
"context"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/golang-tire/auth/internal/users"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/golang-tire/auth/internal/entity"
auth "github.com/golang-tire/auth/internal/proto/v1"
)
// Service encapsulates use case logic for audit logs.
type Service interface {
Get(ctx context.Context, uuid string) (*auth.AuditLog, error)
Query(ctx context.Context, query string, offset, limit int64) (*auth.ListAuditLogsResponse, error)
Count(ctx context.Context) (int64, error)
Create(ctx context.Context, input *auth.CreateAuditLogRequest) (*auth.AuditLog, error)
}
type service struct {
repo Repository
userRepo users.Repository
}
// ValidateCreateRequest validates the CreateAuditLogRequest fields.
func ValidateCreateRequest(c *auth.CreateAuditLogRequest) error {
return validation.ValidateStruct(c,
validation.Field(&c.UserUuid, validation.Required, validation.Length(1, 128), is.UUIDv4),
validation.Field(&c.Object, validation.Required, validation.Length(1, 128)),
validation.Field(&c.Action, validation.Required, validation.Length(1, 128)),
validation.Field(&c.OldValue, validation.Length(0, 128)),
validation.Field(&c.NewValue, validation.Length(0, 128)),
)
}
func (s service) Create(ctx context.Context, req *auth.CreateAuditLogRequest) (*auth.AuditLog, error) {
if err := ValidateCreateRequest(req); err != nil {
return nil, err
}
user, err := s.userRepo.Get(ctx, req.UserUuid)
if err != nil {
return nil, err
}
id, err := s.repo.Create(ctx, entity.AuditLog{
User: user,
UserID: user.ID,
Action: req.Action,
Object: req.Object,
OldValue: req.OldValue,
NewValue: req.NewValue,
})
if err != nil {
return nil, err
}
return s.Get(ctx, id)
}
func (s service) Get(ctx context.Context, Uuid string) (*auth.AuditLog, error) {
auditLog, err := s.repo.Get(ctx, Uuid)
if err != nil {
return nil, err
}
return auditLog.ToProto(), nil
}
func (s service) Query(ctx context.Context, query string, offset, limit int64) (*auth.ListAuditLogsResponse, error) {
items, count, err := s.repo.Query(ctx, query, offset, limit)
if err != nil {
return nil, err
}
return &auth.ListAuditLogsResponse{
AuditLogs: entity.AuditLogToProtoList(items),
TotalCount: int64(count),
Offset: offset,
Limit: limit,
}, nil
}
func (s service) Count(ctx context.Context) (int64, error) {
return s.repo.Count(ctx)
}
// NewService creates a new audit logs service.
func NewService(repo Repository, userRepo users.Repository) Service {
return service{repo, userRepo}
}