-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
33 lines (29 loc) · 799 Bytes
/
get.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
package rawmaterial
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
func List(ctx context.Context, db *mongo.Database) (*[]*RawMaterial, error) {
collection := db.Collection("RawMat")
cursor, err := collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
records := make([]*RawMaterial, 0)
if err = cursor.All(ctx, &records); err != nil {
return nil, err
}
return &records, nil
}
func Read(ctx context.Context, db *mongo.Database, id primitive.ObjectID) (*RawMaterial, error) {
collection := db.Collection("RawMat")
res := collection.FindOne(ctx, bson.M{"_id": id})
var record RawMaterial
err := res.Decode(&record)
if err != nil {
return nil, err
}
return &record, nil
}