This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
/
org.go
145 lines (124 loc) · 3.64 KB
/
org.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
144
145
package service
import (
"context"
"github.com/tsundata/assistant/api/pb"
repositoryOrg "github.com/tsundata/assistant/internal/app/bot/org/repository"
)
type Org struct {
middle pb.MiddleSvcClient
repo repositoryOrg.OrgRepository
}
func NewOrg(repo repositoryOrg.OrgRepository, middle pb.MiddleSvcClient) *Org {
return &Org{repo: repo, middle: middle}
}
func (o *Org) CreateObjective(ctx context.Context, payload *pb.ObjectiveRequest) (*pb.StateReply, error) {
reply, err := o.middle.GetOrCreateTag(ctx, &pb.TagRequest{Tag: &pb.Tag{Name: payload.Tag}})
if err != nil {
return nil, err
}
item := pb.Objective{
Name: payload.Objective.GetName(),
TagId: reply.Tag.GetId(),
}
_, err = o.repo.CreateObjective(ctx, &item)
if err != nil {
return nil, err
}
return &pb.StateReply{State: true}, nil
}
func (o *Org) GetObjective(ctx context.Context, payload *pb.ObjectiveRequest) (*pb.ObjectiveReply, error) {
find, err := o.repo.GetObjectiveByID(ctx, payload.Objective.GetId())
if err != nil {
return nil, err
}
return &pb.ObjectiveReply{
Objective: &pb.Objective{
Id: find.Id,
Name: find.Name,
TagId: find.TagId,
CreatedAt: find.CreatedAt,
},
}, nil
}
func (o *Org) GetObjectives(ctx context.Context, _ *pb.ObjectiveRequest) (*pb.ObjectivesReply, error) {
items, err := o.repo.ListObjectives(ctx)
if err != nil {
return nil, err
}
var res []*pb.Objective
for _, item := range items {
res = append(res, &pb.Objective{
Id: item.Id,
Name: item.Name,
TagId: item.TagId,
CreatedAt: item.CreatedAt,
})
}
return &pb.ObjectivesReply{Objective: res}, nil
}
func (o *Org) DeleteObjective(ctx context.Context, payload *pb.ObjectiveRequest) (*pb.StateReply, error) {
err := o.repo.DeleteObjective(ctx, payload.Objective.GetId())
if err != nil {
return nil, err
}
return &pb.StateReply{State: true}, nil
}
func (o *Org) CreateKeyResult(ctx context.Context, payload *pb.KeyResultRequest) (*pb.StateReply, error) {
reply, err := o.middle.GetOrCreateTag(ctx, &pb.TagRequest{Tag: &pb.Tag{Name: payload.Tag}})
if err != nil {
return nil, err
}
item := pb.KeyResult{
ObjectiveId: payload.KeyResult.GetObjectiveId(),
Name: payload.KeyResult.GetName(),
TagId: reply.Tag.GetId(),
}
_, err = o.repo.CreateKeyResult(ctx, &item)
if err != nil {
return nil, err
}
return &pb.StateReply{State: true}, nil
}
func (o *Org) GetKeyResult(ctx context.Context, payload *pb.KeyResultRequest) (*pb.KeyResultReply, error) {
find, err := o.repo.GetKeyResultByID(ctx, payload.KeyResult.GetId())
if err != nil {
return nil, err
}
return &pb.KeyResultReply{
KeyResult: &pb.KeyResult{
Id: find.Id,
Name: find.Name,
ObjectiveId: find.ObjectiveId,
TagId: find.TagId,
Complete: find.Complete,
CreatedAt: find.CreatedAt,
UpdatedAt: find.UpdatedAt,
},
}, nil
}
func (o *Org) GetKeyResults(ctx context.Context, _ *pb.KeyResultRequest) (*pb.KeyResultsReply, error) {
items, err := o.repo.ListKeyResults(ctx)
if err != nil {
return nil, err
}
var res []*pb.KeyResult
for _, item := range items {
res = append(res, &pb.KeyResult{
Id: item.Id,
Name: item.Name,
TagId: item.TagId,
ObjectiveId: item.ObjectiveId,
Complete: item.Complete,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
})
}
return &pb.KeyResultsReply{Result: res}, nil
}
func (o *Org) DeleteKeyResult(ctx context.Context, payload *pb.KeyResultRequest) (*pb.StateReply, error) {
err := o.repo.DeleteKeyResult(ctx, payload.KeyResult.GetId())
if err != nil {
return nil, err
}
return &pb.StateReply{State: true}, nil
}