/
audit.go
91 lines (79 loc) · 2.39 KB
/
audit.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
package usecase
import (
"context"
"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/internal/repository"
"github.com/openinfradev/tks-api/pkg/httpErrors"
)
type IAuditUsecase interface {
Get(ctx context.Context, auditId uuid.UUID) (model.Audit, error)
Fetch(ctx context.Context, pg *pagination.Pagination) ([]model.Audit, error)
Create(ctx context.Context, dto model.Audit) (auditId uuid.UUID, err error)
Delete(ctx context.Context, dto model.Audit) error
}
type AuditUsecase struct {
repo repository.IAuditRepository
userRepo repository.IUserRepository
organizationRepo repository.IOrganizationRepository
}
func NewAuditUsecase(r repository.Repository) IAuditUsecase {
return &AuditUsecase{
repo: r.Audit,
userRepo: r.User,
organizationRepo: r.Organization,
}
}
func (u *AuditUsecase) Create(ctx context.Context, dto model.Audit) (auditId uuid.UUID, err error) {
if dto.OrganizationId != "" {
organization, err := u.organizationRepo.Get(ctx, dto.OrganizationId)
if err == nil {
dto.OrganizationName = organization.Name
}
}
if dto.UserId != nil && *dto.UserId != uuid.Nil {
user, err := u.userRepo.GetByUuid(ctx, *dto.UserId)
if err != nil {
return auditId, err
}
userRoles := ""
for i, role := range user.Roles {
if i > 0 {
userRoles = userRoles + ","
}
userRoles = userRoles + role.Name
}
dto.OrganizationId = user.Organization.ID
dto.OrganizationName = user.Organization.Name
dto.UserAccountId = user.AccountId
dto.UserName = user.Name
dto.UserRoles = userRoles
}
auditId, err = u.repo.Create(ctx, dto)
if err != nil {
return uuid.Nil, httpErrors.NewInternalServerError(err, "", "")
}
return auditId, nil
}
func (u *AuditUsecase) Get(ctx context.Context, auditId uuid.UUID) (res model.Audit, err error) {
res, err = u.repo.Get(ctx, auditId)
if err != nil {
return model.Audit{}, err
}
return
}
func (u *AuditUsecase) Fetch(ctx context.Context, pg *pagination.Pagination) (audits []model.Audit, err error) {
audits, err = u.repo.Fetch(ctx, pg)
if err != nil {
return nil, err
}
return
}
func (u *AuditUsecase) Delete(ctx context.Context, dto model.Audit) (err error) {
err = u.repo.Delete(ctx, dto.ID)
if err != nil {
return httpErrors.NewNotFoundError(err, "", "")
}
return nil
}