/
egress.go
54 lines (45 loc) · 1.49 KB
/
egress.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
package dao
import (
"context"
"github.com/kiaedev/kiae/api/egress"
"github.com/kiaedev/kiae/pkg/mongoutil"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"google.golang.org/protobuf/types/known/timestamppb"
)
type EgressDao struct {
*Dao
}
func NewEgressDao(db *mongo.Database) *EgressDao {
return &EgressDao{
Dao: NewDao(db.Collection("dependent")),
}
}
func (p *EgressDao) Get(ctx context.Context, id string) (*egress.Egress, error) {
var proj egress.Egress
oid, _ := primitive.ObjectIDFromHex(id)
if err := p.collection.FindOne(ctx, bson.M{"_id": oid}).Decode(&proj); err != nil {
return nil, err
}
return &proj, nil
}
func (p *EgressDao) List(ctx context.Context, m bson.M) ([]*egress.Egress, int64, error) {
var results []*egress.Egress
total, err := mongoutil.ListAndCount(ctx, p.collection, m, &results)
return results, total, err
}
func (p *EgressDao) Create(ctx context.Context, in *egress.Egress) (*egress.Egress, error) {
in.CreatedAt = timestamppb.Now()
in.UpdatedAt = timestamppb.Now()
rt, err := p.collection.InsertOne(ctx, in)
in.Id = rt.InsertedID.(primitive.ObjectID).Hex()
return in, err
}
func (p *EgressDao) Update(ctx context.Context, in *egress.Egress) (*egress.Egress, error) {
oid, _ := primitive.ObjectIDFromHex(in.Id)
in.Id = "" // clean the immutable field
in.UpdatedAt = timestamppb.Now()
_, err := p.collection.UpdateOne(ctx, bson.M{"_id": oid}, bson.M{"$set": in})
return in, err
}