-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel.go
67 lines (54 loc) · 1.63 KB
/
model.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
package main
import (
"database/sql"
)
// the product type is the fields that cover each item in our database
// struct fields include encoded JSON key names
type product struct {
ID int `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
const (
getProductQuery = "SELECT name, price FROM products WHERE id=$1"
getProductsQuery = "SELECT id, name, price FROM products LIMIT $1 OFFSET $2"
updateProductQuery = "UPDATE products SET name=$1, price=$2 WHERE id=$3"
deleteProductQuery = "DELETE FROM products WHERE id=$1"
createProductQuery = "INSERT INTO products(name, price) VALUES($1, $2) RETURNING id"
)
// the set of functions for CRUD operations on our model
func (p *product) getProduct(db *sql.DB) error {
return db.QueryRow(getProductQuery, p.ID).Scan(&p.Name, &p.Price)
}
func (p *product) updateProduct(db *sql.DB) error {
_, err := db.Exec(updateProductQuery, p.Name, p.Price, p.ID)
return err
}
func (p *product) deleteProduct(db *sql.DB) error {
_, err := db.Exec(deleteProductQuery, p.ID)
return err
}
func (p *product) createProduct(db *sql.DB) error {
err := db.QueryRow(createProductQuery, p.Name, p.Price).Scan(&p.ID)
if err != nil {
return err
}
return nil
}
// separate function to get multiple products
func getProducts(db *sql.DB, start, count int) ([]product, error) {
rows, err := db.Query(getProductsQuery, count, start)
if err != nil {
return nil, err
}
defer rows.Close()
products := []product{}
for rows.Next() {
var p product
if err := rows.Scan(&p.ID, &p.Name, &p.Price); err != nil {
return nil, err
}
products = append(products, p)
}
return products, nil
}