shop.tonybtw.com

shop.tonybtw.com

https://git.tonybtw.com/shop.tonybtw.com.git git://git.tonybtw.com/shop.tonybtw.com.git
3,704 bytes raw
1
package models
2
3
import (
4
	"database/sql"
5
)
6
7
type Product struct {
8
	ID          int
9
	Slug        string
10
	Name        string
11
	Description string
12
	Price       int
13
	Image_URL   string
14
	Active      bool
15
}
16
17
type Variant struct {
18
	ID                  int
19
	Product_ID          int
20
	Size                string
21
	Printful_Variant_ID sql.NullString
22
	Stock               int
23
	Sort_Order          int
24
}
25
26
type Product_With_Variant struct {
27
	Product_ID          int
28
	Variant_ID          int
29
	Product_Name        string
30
	Size                string
31
	Price               int
32
	Image_URL           string
33
	Printful_Variant_ID sql.NullString
34
}
35
36
type Product_Image struct {
37
	ID         int
38
	Product_ID int
39
	Image_URL  string
40
	Sort_Order int
41
}
42
43
func Get_All_Products(db *sql.DB) ([]Product, error) {
44
	rows, err := db.Query(`
45
		SELECT id, slug, name, description, price, image_url
46
		FROM products
47
		WHERE active = true
48
		ORDER BY created_at DESC
49
	`)
50
	if err != nil {
51
		return nil, err
52
	}
53
	defer rows.Close()
54
55
	var products []Product
56
	for rows.Next() {
57
		var p Product
58
		if err := rows.Scan(&p.ID, &p.Slug, &p.Name, &p.Description, &p.Price, &p.Image_URL); err != nil {
59
			return nil, err
60
		}
61
		products = append(products, p)
62
	}
63
	return products, rows.Err()
64
}
65
66
func Get_Product_By_Slug(db *sql.DB, slug string) (*Product, error) {
67
	var p Product
68
	err := db.QueryRow(`
69
		SELECT id, slug, name, description, price, image_url, active
70
		FROM products
71
		WHERE slug = $1 AND active = true
72
	`, slug).Scan(&p.ID, &p.Slug, &p.Name, &p.Description, &p.Price, &p.Image_URL, &p.Active)
73
74
	if err == sql.ErrNoRows {
75
		return nil, nil
76
	}
77
	if err != nil {
78
		return nil, err
79
	}
80
	return &p, nil
81
}
82
83
func Get_Product_By_ID(db *sql.DB, id int) (*Product, error) {
84
	var p Product
85
	err := db.QueryRow(`
86
		SELECT id, slug, name, description, price, image_url, active
87
		FROM products
88
		WHERE id = $1
89
	`, id).Scan(&p.ID, &p.Slug, &p.Name, &p.Description, &p.Price, &p.Image_URL, &p.Active)
90
91
	if err == sql.ErrNoRows {
92
		return nil, nil
93
	}
94
	if err != nil {
95
		return nil, err
96
	}
97
	return &p, nil
98
}
99
100
func Get_Product_Variants(db *sql.DB, product_id int) ([]Variant, error) {
101
	rows, err := db.Query(`
102
		SELECT id, size, printful_variant_id, stock
103
		FROM variants
104
		WHERE product_id = $1
105
		ORDER BY sort_order
106
	`, product_id)
107
	if err != nil {
108
		return nil, err
109
	}
110
	defer rows.Close()
111
112
	var variants []Variant
113
	for rows.Next() {
114
		var v Variant
115
		v.Product_ID = product_id
116
		if err := rows.Scan(&v.ID, &v.Size, &v.Printful_Variant_ID, &v.Stock); err != nil {
117
			return nil, err
118
		}
119
		variants = append(variants, v)
120
	}
121
	return variants, rows.Err()
122
}
123
124
func Get_Variant_By_ID(db *sql.DB, variant_id int) (*Product_With_Variant, error) {
125
	var pwv Product_With_Variant
126
	err := db.QueryRow(`
127
		SELECT v.id, v.product_id, v.size, v.printful_variant_id, p.name, p.price, p.image_url
128
		FROM variants v
129
		JOIN products p ON p.id = v.product_id
130
		WHERE v.id = $1
131
	`, variant_id).Scan(
132
		&pwv.Variant_ID,
133
		&pwv.Product_ID,
134
		&pwv.Size,
135
		&pwv.Printful_Variant_ID,
136
		&pwv.Product_Name,
137
		&pwv.Price,
138
		&pwv.Image_URL,
139
	)
140
141
	if err == sql.ErrNoRows {
142
		return nil, nil
143
	}
144
	if err != nil {
145
		return nil, err
146
	}
147
	return &pwv, nil
148
}
149
150
func Get_Product_Images(db *sql.DB, product_id int) ([]Product_Image, error) {
151
	rows, err := db.Query(`
152
		SELECT id, product_id, image_url, sort_order
153
		FROM product_images
154
		WHERE product_id = $1
155
		ORDER BY sort_order
156
	`, product_id)
157
	if err != nil {
158
		return nil, err
159
	}
160
	defer rows.Close()
161
162
	var images []Product_Image
163
	for rows.Next() {
164
		var img Product_Image
165
		if err := rows.Scan(&img.ID, &img.Product_ID, &img.Image_URL, &img.Sort_Order); err != nil {
166
			return nil, err
167
		}
168
		images = append(images, img)
169
	}
170
	return images, rows.Err()
171
}