/
produit.go
167 lines (142 loc) · 4.63 KB
/
produit.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package documents
import (
"context"
"N4n3x/verretech-microservices/produit/produitpb"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
//produitCollection is the name of the collection storing our blog documents within the mongo database
const produitCollection = "produit"
type Localisation struct {
Adresse string `bson:"adresse"`
Ville string `bson:"ville"`
Cp string `bson:"cp"`
}
type PointRetrait struct {
Nom string `bson:"nom"`
Localisation *Localisation `bson:"localisation"`
}
type Stock struct {
PointRetrait *PointRetrait `bson:"pointRetrait"`
Qte int32 `bson:"qte"`
}
type Photo struct {
Url string `bson:"url"`
}
type Produit struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Ref string `bson:"ref"`
Description string `bson:"description"`
Prix float32 `bson:"prix"`
Photos []*Photo `bson:"photos"`
Stocks []*Stock `bson:"stocks"`
Tags []string `bson:"tags"`
}
//InsertOne inserts one post in the database
func (produit *Produit) InsertOne(db mongo.Database) (primitive.ObjectID, error) {
fmt.Printf("befor insert %+v\n",produit)
collection := db.Collection(produitCollection)
result, err := collection.InsertOne(context.Background(), produit)
if err != nil {
return primitive.NilObjectID, err
}
return result.InsertedID.(primitive.ObjectID), nil
}
//FindOne returns the product with the specified ID from the database
func (produit *Produit) FindOne(db mongo.Database) error {
collection := db.Collection(produitCollection)
filter := bson.M{"ref": produit.Ref}
err := collection.FindOne(context.Background(), filter).Decode(produit)
if err != nil {
return err
}
return nil
}
//Find returns a cursor pointin to all the produits in the db
func Find(db mongo.Database) (*mongo.Cursor, error) {
collection := db.Collection(produitCollection)
return collection.Find(context.Background(), bson.D{{}})
}
//Update updates the specified produit within the database
func (produit *Produit) Update(db mongo.Database) error {
collection := db.Collection(produitCollection)
update := bson.M{
"$set": bson.M{
"ref": produit.Ref,
"description": produit.Description,
"prix": produit.Prix,
"photos": produit.Photos,
"stocks": produit.Stocks,
"tags": produit.Tags,
},
}
_, err := collection.UpdateOne(context.Background(), bson.M{"_id": produit.ID}, update)
if err != nil {
return err
}
return nil
}
//FromProduitPB parses a produit defined by the protobuff into a mongo produit document
func FromProduitPB(produitProto *produitpb.Produit) (*Produit, error) {
fmt.Printf("%+v\n",produitProto)
var produit = new(Produit)
var photos []*Photo
var stocks []*Stock
if produitProto.ID != "" {
oid, _ := primitive.ObjectIDFromHex(produitProto.ID)
produit.ID = oid
}
for _, e := range produitProto.Photos {
p := &Photo{Url: e.Url}
photos = append(photos, p)
}
produit.Photos = photos
// fmt.Printf("%+v\n",photos)
for _, e := range produitProto.Stocks {
l := &Localisation{Adresse: e.PointRetrait.Localisation.Adresse, Ville: e.PointRetrait.Localisation.Ville, Cp: e.PointRetrait.Localisation.Cp}
pr := &PointRetrait{Nom: e.PointRetrait.Nom, Localisation: l}
s := &Stock{PointRetrait: pr, Qte: e.Qte}
stocks = append(stocks, s)
}
produit.Stocks = stocks
produit.Ref = produitProto.Ref
produit.Description = produitProto.Description
produit.Prix = produitProto.Prix
produit.Tags = produitProto.Tags
// produit := &Produit{
// Ref: produitProto.Ref,
// Description: produitProto.Description,
// Prix: produitProto.Prix,
// Photos: photos,
// Stocks: stocks,
// Tags: produitProto.Tags,
// }
fmt.Printf("End convert %+v\n",produit)
return produit, nil
}
//ToProduitPB parses a mongo produit document into a produit defined by the protobuff
func (produit *Produit) ToProduitPB() *produitpb.Produit {
var photos []*produitpb.Photo
for _, e := range produit.Photos {
p := &produitpb.Photo{Url: e.Url}
photos = append(photos, p)
}
var stocks []*produitpb.Stock
for _, e := range produit.Stocks {
l := &produitpb.Localisation{Adresse: e.PointRetrait.Localisation.Adresse, Ville: e.PointRetrait.Localisation.Ville, Cp: e.PointRetrait.Localisation.Cp}
pr := &produitpb.PointRetrait{Nom: e.PointRetrait.Nom, Localisation: l}
s := &produitpb.Stock{PointRetrait: pr, Qte: e.Qte}
stocks = append(stocks, s)
}
return &produitpb.Produit{
ID: produit.ID.Hex(),
Ref: produit.Ref,
Description: produit.Description,
Prix: produit.Prix,
Photos: photos,
Stocks: stocks,
Tags: produit.Tags,
}
}