-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbhandler.go
107 lines (81 loc) · 2.77 KB
/
dbhandler.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
package main
import (
"gopkg.in/mgo.v2"
"log"
)
type MongoConnection struct {
dbSession *mgo.Session
}
func OpenConnectionSession() (mongoConnection *MongoConnection) {
mongoConnection = new(MongoConnection)
mongoConnection.createNewDBConnection()
return
}
func (mConnection *MongoConnection) createNewDBConnection() (err error) {
mConnection.dbSession, err = mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
mConnection.dbSession.SetMode(mgo.Monotonic, true)
return
}
func (mConnection *MongoConnection) CreateNewUser(user *User) bool {
return user.CreateNewUser(mConnection)
}
func (mConnection *MongoConnection) LoginWithCredentials(email string, password string) *User {
user := &User{}
return user.LoginWithCredentials(mConnection, email, password)
}
func (mConnection *MongoConnection) CreateNewProduct(product *Product) bool {
return product.CreateNewProduct(mConnection)
}
func (mConnection *MongoConnection) UpdateProduct(product *Product) bool {
return product.UpdateProduct(mConnection)
}
func (mConnection *MongoConnection) DeleteProduct(token string, productID string) bool {
product := &Product{}
return product.DeleteProduct(mConnection, token, productID)
}
func (mConnection *MongoConnection) ListAllProducts(token string) []Product {
product := &Product{}
return product.ListProducts(mConnection, token)
}
func (mConnection *MongoConnection) FindExistingProducts(token string, productsIDs []string) []Product {
product := &Product{}
return product.FindProductsByIDs(mConnection, token, productsIDs)
}
func (mConnection *MongoConnection) CreateNewCustomer(customer *Customer) bool {
return customer.CreateNewCustomer(mConnection)
}
func (mConnection *MongoConnection) UpdateCustomer(customer *Customer) bool {
return customer.UpdateCustomer(mConnection)
}
func (mConnection *MongoConnection) DeleteCustomer(token string, customerID string) bool {
customer := &Customer{}
return customer.DeleteCustomer(mConnection, token, customerID)
}
func (mConnection *MongoConnection) ListAllCustomers(token string) []Customer {
customer := &Customer{}
return customer.ListCustomers(mConnection, token)
}
func (mConnection *MongoConnection) FindExistingCustomers(token string, customerID string) *Customer {
customer := &Customer{}
return customer.FindCustomerByID(mConnection, token, customerID)
}
func (mConnection *MongoConnection) CreateNewInvoice(invoice *Invoice) bool {
return invoice.CreateInvoice(mConnection)
}
func (mConnection *MongoConnection) SaveTestObject(testInvoice *Invoice) bool {
if mConnection.dbSession == nil {
return false
}
session := mConnection.dbSession.Copy()
defer session.Close()
collection := session.DB("goitest").C("invoice")
err := collection.Insert(testInvoice)
if err != nil {
log.Fatal(err)
return false
}
return true
}