This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
/
project.go
133 lines (102 loc) · 2.75 KB
/
project.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
package store
import (
"context"
"fmt"
"github.com/I1820/I1820/internal/model"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// ProjectCollection is mongodb collection name for project.
const ProjectCollection = "project"
// Project stores and retrieves projects collection.
type Project struct {
DB *mongo.Database
}
// List returns all registered projects.
func (ps Project) List(ctx context.Context) ([]model.Project, error) {
projects := make([]model.Project, 0)
cur, err := ps.DB.Collection(ProjectCollection).Find(ctx, bson.M{})
if err != nil {
return projects, err
}
for cur.Next(ctx) {
var p model.Project
if err := cur.Decode(&p); err != nil {
return projects, err
}
projects = append(projects, p)
}
if err := cur.Close(ctx); err != nil {
return projects, err
}
return projects, err
}
func (ps Project) Create(ctx context.Context, p model.Project) error {
if _, err := ps.DB.Collection(ProjectCollection).InsertOne(ctx, p); err != nil {
return err
}
return nil
}
func (ps Project) Get(ctx context.Context, id string) (model.Project, error) {
var p model.Project
dr := ps.DB.Collection(ProjectCollection).FindOne(ctx, bson.M{
"id": id,
})
if err := dr.Decode(&p); err != nil {
return p, err
}
return p, nil
}
func (ps Project) Delete(ctx context.Context, id string) error {
// remove project entity from database
if _, err := ps.DB.Collection(ProjectCollection).DeleteOne(ctx, bson.M{
"id": id,
}); err != nil {
return err
}
if err := ps.DB.Collection(fmt.Sprintf("%s.logs.%s", ProjectCollection, id)).Drop(ctx); err != nil {
logrus.Errorf("Log collection deletion failed %s", err)
}
return nil
}
func (ps Project) Update(ctx context.Context, id string, fields map[string]interface{}) (model.Project, error) {
var p model.Project
dr := ps.DB.Collection(ProjectCollection).FindOneAndUpdate(ctx, bson.M{
"id": id,
}, bson.M{
"$set": fields,
}, options.FindOneAndUpdate().SetReturnDocument(options.After))
if err := dr.Decode(&p); err != nil {
return p, err
}
return p, nil
}
func (ps Project) Logs(ctx context.Context, name string, limit int) ([]model.ProjectLog, error) {
pls := make([]model.ProjectLog, 0)
cur, err := ps.DB.Collection(fmt.Sprintf("%s.logs.%s", ProjectCollection, name)).Aggregate(ctx, bson.A{
bson.M{
"$sort": bson.M{
"Time": -1,
},
},
bson.M{
"$limit": limit,
},
}, options.Aggregate().SetAllowDiskUse(true))
if err != nil {
return nil, err
}
for cur.Next(ctx) {
var pl model.ProjectLog
if err := cur.Decode(&pl); err != nil {
return nil, err
}
pls = append(pls, pl)
}
if err := cur.Close(ctx); err != nil {
return nil, err
}
return pls, nil
}