forked from blue-jay/blueprint
/
user.go
60 lines (53 loc) · 1.53 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
// Package user provides access to the user table in the MySQL database.
package user
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
)
var (
// table is the table name.
table = "user"
)
// Item defines the model.
type Item struct {
ID uint32 `db:"id"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string `db:"email"`
Password string `db:"password"`
StatusID uint8 `db:"status_id"`
CreatedAt mysql.NullTime `db:"created_at"`
UpdatedAt mysql.NullTime `db:"updated_at"`
DeletedAt mysql.NullTime `db:"deleted_at"`
}
// Connection is an interface for making queries.
type Connection interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Get(dest interface{}, query string, args ...interface{}) error
Select(dest interface{}, query string, args ...interface{}) error
}
// ByEmail gets user information from email.
func ByEmail(db Connection, email string) (Item, bool, error) {
result := Item{}
err := db.Get(&result, fmt.Sprintf(`
SELECT id, password, status_id, first_name
FROM %v
WHERE email = ?
AND deleted_at IS NULL
LIMIT 1
`, table),
email)
return result, err == sql.ErrNoRows, err
}
// Create creates user.
func Create(db Connection, firstName, lastName, email, password string) (sql.Result, error) {
result, err := db.Exec(fmt.Sprintf(`
INSERT INTO %v
(first_name, last_name, email, password)
VALUES
(?,?,?,?)
`, table),
firstName, lastName, email, password)
return result, err
}