-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
143 lines (121 loc) Β· 3.78 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2021 Saferwall. All rights reserved.
// Use of this source code is governed by Apache v2 license
// license that can be found in the LICENSE file.
package activity
import (
"context"
"encoding/json"
"time"
"github.com/saferwall/saferwall-api/internal/entity"
"github.com/saferwall/saferwall-api/pkg/log"
)
// Service encapsulates usecase logic for users.
type Service interface {
Get(ctx context.Context, id string, fields []string) (Activity, error)
Query(ctx context.Context, offset, limit int) ([]interface{}, error)
Count(ctx context.Context) (int, error)
Create(ctx context.Context, input CreateActivityRequest) (Activity, error)
Update(ctx context.Context, id string, input UpdateActivityRequest) (Activity, error)
Delete(ctx context.Context, id string) (Activity, error)
DeleteWith(ctx context.Context, kind, username, target string) error
}
// Activity represents the data about an activity.
type Activity struct {
entity.Activity
}
type service struct {
repo Repository
logger log.Logger
}
// CreateActivityRequest represents a user creation request.
type CreateActivityRequest struct {
Kind string `json:"kind" validate:"required,alpha"`
Username string `json:"username" validate:"required,alpha"`
Target string `json:"target" validate:"required,alpha"`
Source string `json:"src" validate:"required,alpha"`
}
// UpdateActivityRequest represents a user update request.
type UpdateActivityRequest struct {
Target string `json:"target" validate:"required,alpha"`
}
// NewService creates a new user service.
func NewService(repo Repository, logger log.Logger) Service {
return service{repo, logger}
}
// Get returns the user with the specified user ID.
func (s service) Get(ctx context.Context, id string, fields []string) (Activity, error) {
activity, err := s.repo.Get(ctx, id, fields)
if err != nil {
return Activity{}, err
}
return Activity{activity}, nil
}
// Create creates a new activity.
func (s service) Create(ctx context.Context, req CreateActivityRequest) (
Activity, error) {
id := entity.ID()
now := time.Now()
err := s.repo.Create(ctx, entity.Activity{
ID: id,
Kind: req.Kind,
Type: "activity",
Username: req.Username,
Target: req.Target,
Timestamp: now.Unix(),
Source: req.Source,
})
if err != nil {
return Activity{}, err
}
return s.Get(ctx, id, nil)
}
// Update updates the activity with the specified ID.
func (s service) Update(ctx context.Context, id string, req UpdateActivityRequest) (
Activity, error) {
activity, err := s.Get(ctx, id, nil)
if err != nil {
return activity, err
}
data, err := json.Marshal(req)
if err != nil {
return activity, err
}
err = json.Unmarshal(data, &activity)
if err != nil {
return activity, err
}
// check if activity.Username == id
if err := s.repo.Update(ctx, id, activity.Activity); err != nil {
return activity, err
}
return activity, nil
}
// Delete deletes the activity with the specified ID.
func (s service) Delete(ctx context.Context, id string) (Activity, error) {
activity, err := s.Get(ctx, id, nil)
if err != nil {
return Activity{}, err
}
if err = s.repo.Delete(ctx, id); err != nil {
return Activity{}, err
}
return activity, nil
}
// Count returns the number of activities.
func (s service) Count(ctx context.Context) (int, error) {
return s.repo.Count(ctx)
}
// Query returns the activities with the specified offset and limit.
func (s service) Query(ctx context.Context, offset, limit int) (
[]interface{}, error) {
result, err := s.repo.Query(ctx, offset, limit)
if err != nil {
return nil, err
}
return result, nil
}
// Delete an activity given its kind, username and target.
func (s service) DeleteWith(ctx context.Context, kind, username, target string) error {
err := s.repo.DeleteWith(ctx, kind, username, target)
return err
}