forked from josephspurrier/gowebapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
79 lines (64 loc) · 1.94 KB
/
user.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
package model
import (
"time"
"github.com/atabek/gowebapp/shared/database"
"gopkg.in/mgo.v2/bson"
)
// *****************************************************************************
// User
// *****************************************************************************
// User table contains the information for each user
type User struct {
ObjectId bson.ObjectId `bson:"_id"`
First_name string `db:"first_name" bson:"first_name"`
Last_name string `db:"last_name" bson:"last_name"`
Email string `db:"email" bson:"email"`
Password string `db:"password" bson:"password"`
Status_id uint8 `db:"status_id" bson:"status_id"`
Created_at time.Time `db:"created_at" bson:"created_at"`
Updated_at time.Time `db:"updated_at" bson:"updated_at"`
Deleted uint8 `db:"deleted" bson:"deleted"`
}
// Id returns the user id
func (u *User) ID() string {
return u.ObjectId.Hex()
}
// UserByEmail gets user information from email
func UserByEmail(email string) (User, error) {
var err error
result := User{}
if database.CheckConnection() {
session := database.Mongo.Copy()
defer session.Close()
c := session.DB(database.ReadConfig().MongoDB.Database).C("user")
err = c.Find(bson.M{"email": email}).One(&result)
} else {
err = ErrUnavailable
}
return result, standardizeError(err)
}
// UserCreate creates user
func UserCreate(first_name, last_name, email, password string) error {
var err error
now := time.Now()
if database.CheckConnection() {
session := database.Mongo.Copy()
defer session.Close()
c := session.DB(database.ReadConfig().MongoDB.Database).C("user")
user := &User{
ObjectId: bson.NewObjectId(),
First_name: first_name,
Last_name: last_name,
Email: email,
Password: password,
Status_id: 1,
Created_at: now,
Updated_at: now,
Deleted: 0,
}
err = c.Insert(user)
} else {
err = ErrUnavailable
}
return standardizeError(err)
}