/
product.go
144 lines (109 loc) · 3.3 KB
/
product.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
package main
import (
"gopkg.in/mgo.v2/bson"
"log"
"time"
)
type Product struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
UserID bson.ObjectId `json:"user_id" bson:"user_id"`
Name string `json:"name" bson:"name"`
Description string `json:"description" bson:"description"`
Price string `json:"price" bson:"price"`
Tax string `json:"tax" bson:"tax"`
Valid bool `json:"valid" bson:"valid"`
Date_Created time.Time `json:"date_created" bson:"date_created"`
}
func (product *Product) CreateNewProduct(mConnection *MongoConnection) bool {
if mConnection.dbSession == nil {
return false
}
session := mConnection.dbSession.Copy()
defer session.Close()
collection := session.DB("goinvoice").C("productdata")
err := collection.Insert(product)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) UpdateProduct(mConnection *MongoConnection) bool {
if mConnection.dbSession == nil {
return false
}
session := mConnection.dbSession.Copy()
defer session.Close()
c := session.DB("goinvoice").C("productdata")
err := c.UpdateId(product.Id, product)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) DeleteProduct(mConnection *MongoConnection, token string, productID string) bool {
if mConnection.dbSession == nil {
return false
}
session := mConnection.dbSession.Copy()
defer session.Close()
c := session.DB("goinvoice").C("productdata")
err := c.Remove(bson.M{"_id": bson.ObjectIdHex(productID), "$and": []interface{}{bson.M{"user_id": bson.ObjectIdHex(token)}}})
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) ListProducts(mConnection *MongoConnection, token string) []Product {
if mConnection.dbSession == nil {
return nil
}
session := mConnection.dbSession.Copy()
defer session.Close()
var products Products
c := session.DB("goinvoice").C("productdata")
err := c.Find(bson.M{"user_id": bson.ObjectIdHex(token)}).All(&products)
if err != nil {
log.Fatal(err)
}
return products
}
func (product *Product) FindProduct(mConnection *MongoConnection, token string, productID string) *Product {
if mConnection.dbSession == nil {
return nil
}
session := mConnection.dbSession.Copy()
defer session.Close()
var productData *Product
c := session.DB("goinvoice").C("productdata")
err := c.Find(bson.M{"_id": bson.ObjectIdHex(productID), "$and": []interface{}{bson.M{"user_id": bson.ObjectIdHex(token)}}}).One(&productData)
if err != nil {
log.Fatal(err)
}
return productData
}
func (product *Product) FindProductsByIDs(mConnection *MongoConnection, token string, productsIDs []string) []Product {
if mConnection.dbSession == nil {
return nil
}
session := mConnection.dbSession.Copy()
defer session.Close()
objectsArray := make([]bson.ObjectId, len(productsIDs))
for i := 0; i < len(productsIDs); i++ {
objectsArray[i] = bson.ObjectIdHex(productsIDs[i])
}
var products Products
c := session.DB("goinvoice").C("productdata")
err := c.Find(
bson.M{"user_id": bson.ObjectIdHex(token),
"$and": []interface{}{
bson.M{"_id": []interface{}{
bson.M{"$in": objectsArray}}}}}).All(&products)
if err != nil {
log.Fatal(err)
}
return products
}
type Products []Product