-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.go
68 lines (55 loc) · 1.67 KB
/
contacts.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
package service
import "database/sql"
// Contact describes a contact in our database.
type Contact struct {
Id int `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
// ===== ADD CONTACT ===================================================================================================
// AddContact inserts a new contact into the database.
func (db *Database) AddContact(c Contact) (int, error) {
var contactId int
err := db.Write(func(tx *Transaction) {
contactId = tx.AddContact(c)
})
return contactId, err
}
// AddContact inserts a new contact within the transaction.
func (tx *Transaction) AddContact(c Contact) int {
row := tx.QueryRow(
"INSERT INTO contacts (email, name) VALUES ($1, $2) RETURNING id",
c.Email,
c.Name,
)
var id int
if err := row.Scan(&id); err != nil {
panic(err)
}
return id
}
// ===== GET CONTACT ===================================================================================================
// GetContactByEmail reads a Contact from the Database.
func (db *Database) GetContactByEmail(email string) (*Contact, error) {
var contact *Contact
err := db.Read(func(tx *Transaction) {
contact = tx.GetContactByEmail(email)
})
return contact, err
}
// GetContactByEmail finds a contact given an email address. `nil` is returned if the Contact doesn't exist in the DB.
func (tx *Transaction) GetContactByEmail(email string) *Contact {
row := tx.QueryRow(
"SELECT id, email, name FROM contacts WHERE email = $1",
email,
)
var contact Contact
err := row.Scan(&contact.Id, &contact.Email, &contact.Name)
if err == nil {
return &contact
} else if err == sql.ErrNoRows {
return nil
} else {
panic(err)
}
}