-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1.go
61 lines (51 loc) · 1.91 KB
/
v1.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
package rawmaterial
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type RawMaterial struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
RawMatID string `bson:"rawmatid" json:"rawmatid"`
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Status bool `bson:"status" json:"status"`
Detail []bson.M `bson:"detail" json:"detail"`
ImageID string `bson:"imageid" json:"imageid"`
CreatedTime string `json:"created_time" bson:"created_time"`
UpdatedTime string `json:"updated_time" bson:"updated_time"`
}
func (m *RawMaterial) Create(ctx context.Context, db *mongo.Database) (interface{}, error) {
collection := db.Collection("RawMat")
res, err := collection.InsertOne(ctx, m)
if err != nil {
return nil, err
}
m.ID = res.InsertedID.(primitive.ObjectID)
return m.ID, nil
}
func (doc *RawMaterial) Update(ctx context.Context, db *mongo.Database, id primitive.ObjectID) (interface{}, error) {
collection := db.Collection("RawMat")
filter := bson.M{"_id": id}
update := bson.D{{"$set", bson.D{{"rawmatid", doc.RawMatID}, {"name", doc.Name}, {"type", doc.Type}, {"status", doc.Status}, {"detail", doc.Detail}, {"imageid", doc.ImageID}, {"updated_time", doc.UpdatedTime}}}}
result, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return nil, err
}
return result, nil
}
func Delete(ctx context.Context, db *mongo.Database, id primitive.ObjectID) (interface{}, error) {
collection := db.Collection("RawMat")
filter := bson.M{"_id": id}
result, err := collection.DeleteOne(ctx, filter)
if err != nil {
return nil, err
}
if result.DeletedCount == 0 {
fmt.Println("no matching document found")
}
fmt.Printf("deleted %v document(s)\n", result.DeletedCount)
return result, nil
}