This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
/
org.go
90 lines (78 loc) · 2.8 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
package repository
import (
"context"
"github.com/tsundata/assistant/api/pb"
"github.com/tsundata/assistant/internal/pkg/global"
"github.com/tsundata/assistant/internal/pkg/middleware/mysql"
)
type OrgRepository interface {
GetObjectiveByID(ctx context.Context, id int64) (*pb.Objective, error)
ListObjectives(ctx context.Context, ) ([]*pb.Objective, error)
CreateObjective(ctx context.Context, objective *pb.Objective) (int64, error)
DeleteObjective(ctx context.Context, id int64) error
GetKeyResultByID(ctx context.Context, id int64) (*pb.KeyResult, error)
ListKeyResults(ctx context.Context, ) ([]*pb.KeyResult, error)
CreateKeyResult(ctx context.Context, keyResult *pb.KeyResult) (int64, error)
DeleteKeyResult(ctx context.Context, id int64) error
}
type MysqlOrgRepository struct {
id *global.ID
db *mysql.Conn
}
func NewMysqlOrgRepository(id *global.ID, db *mysql.Conn) OrgRepository {
return &MysqlOrgRepository{id: id, db: db}
}
func (r *MysqlOrgRepository) GetObjectiveByID(ctx context.Context, id int64) (*pb.Objective, error) {
var objective pb.Objective
err := r.db.WithContext(ctx).Where("id = ?", id).First(&objective).Error
if err != nil {
return nil, err
}
return &objective, nil
}
func (r *MysqlOrgRepository) ListObjectives(ctx context.Context) ([]*pb.Objective, error) {
var objectives []*pb.Objective
err := r.db.WithContext(ctx).Order("id DESC").Find(&objectives).Error
if err != nil {
return nil, err
}
return objectives, nil
}
func (r *MysqlOrgRepository) CreateObjective(ctx context.Context, objective *pb.Objective) (int64, error) {
objective.Id = r.id.Generate(ctx)
err := r.db.WithContext(ctx).Create(&objective).Error
if err != nil {
return 0, err
}
return objective.Id, nil
}
func (r *MysqlOrgRepository) DeleteObjective(ctx context.Context, id int64) error {
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&pb.Objective{}).Error
}
func (r *MysqlOrgRepository) GetKeyResultByID(ctx context.Context, id int64) (*pb.KeyResult, error) {
var keyResult pb.KeyResult
err := r.db.WithContext(ctx).Where("id = ?", id).First(&keyResult).Error
if err != nil {
return nil, err
}
return &keyResult, nil
}
func (r *MysqlOrgRepository) ListKeyResults(ctx context.Context) ([]*pb.KeyResult, error) {
var keyResult []*pb.KeyResult
err := r.db.WithContext(ctx).Order("id DESC").Find(&keyResult).Error
if err != nil {
return nil, err
}
return keyResult, nil
}
func (r *MysqlOrgRepository) CreateKeyResult(ctx context.Context, keyResult *pb.KeyResult) (int64, error) {
keyResult.Id = r.id.Generate(ctx)
err := r.db.WithContext(ctx).Create(&keyResult).Error
if err != nil {
return 0, err
}
return keyResult.Id, nil
}
func (r *MysqlOrgRepository) DeleteKeyResult(ctx context.Context, id int64) error {
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&pb.KeyResult{}).Error
}