shop.tonybtw.com

shop.tonybtw.com

https://git.tonybtw.com/shop.tonybtw.com.git git://git.tonybtw.com/shop.tonybtw.com.git
3,921 bytes raw
1
package models
2
3
import (
4
	"database/sql"
5
	"fmt"
6
	"time"
7
)
8
9
type Order struct {
10
	ID                     int
11
	Stripe_Session_ID      string
12
	Stripe_Payment_Intent  string
13
	Email                  string
14
	Total                  int
15
	Status                 string
16
	Shipping_Name          string
17
	Shipping_Address       string
18
	Printful_Order_ID      string
19
	Created_At             time.Time
20
	Updated_At             time.Time
21
}
22
23
type Order_Item struct {
24
	ID         int
25
	Order_ID   int
26
	Product_ID int
27
	Variant_ID int
28
	Quantity   int
29
	Price      int
30
}
31
32
type Order_Item_Detail struct {
33
	Order_Item
34
	Product_Name string
35
	Size         string
36
}
37
38
func Create_Order(
39
	db *sql.DB,
40
	stripe_session_id string,
41
	stripe_payment_intent string,
42
	email string,
43
	total int,
44
	status string,
45
	shipping_name string,
46
	shipping_address string,
47
) (int, error) {
48
	var order_id int
49
	err := db.QueryRow(`
50
		INSERT INTO orders (
51
			stripe_session_id,
52
			stripe_payment_intent,
53
			email,
54
			total,
55
			status,
56
			shipping_name,
57
			shipping_address
58
		)
59
		VALUES ($1, $2, $3, $4, $5, $6, $7)
60
		RETURNING id
61
	`, stripe_session_id, stripe_payment_intent, email, total, status, shipping_name, shipping_address).Scan(&order_id)
62
63
	return order_id, err
64
}
65
66
func Get_Order_By_ID(db *sql.DB, id int) (*Order, error) {
67
	var o Order
68
	err := db.QueryRow(`
69
		SELECT id, stripe_session_id, stripe_payment_intent, email, total, status,
70
		       shipping_name, shipping_address, printful_order_id, created_at, updated_at
71
		FROM orders
72
		WHERE id = $1
73
	`, id).Scan(
74
		&o.ID, &o.Stripe_Session_ID, &o.Stripe_Payment_Intent, &o.Email, &o.Total, &o.Status,
75
		&o.Shipping_Name, &o.Shipping_Address, &o.Printful_Order_ID, &o.Created_At, &o.Updated_At,
76
	)
77
78
	if err == sql.ErrNoRows {
79
		return nil, nil
80
	}
81
	if err != nil {
82
		return nil, err
83
	}
84
	return &o, nil
85
}
86
87
func Get_Order_By_Stripe_Session(db *sql.DB, session_id string) (*Order, error) {
88
	var o Order
89
	err := db.QueryRow(`
90
		SELECT id, stripe_session_id, stripe_payment_intent, email, total, status,
91
		       shipping_name, shipping_address, printful_order_id, created_at, updated_at
92
		FROM orders
93
		WHERE stripe_session_id = $1
94
	`, session_id).Scan(
95
		&o.ID, &o.Stripe_Session_ID, &o.Stripe_Payment_Intent, &o.Email, &o.Total, &o.Status,
96
		&o.Shipping_Name, &o.Shipping_Address, &o.Printful_Order_ID, &o.Created_At, &o.Updated_At,
97
	)
98
99
	if err == sql.ErrNoRows {
100
		return nil, nil
101
	}
102
	if err != nil {
103
		return nil, err
104
	}
105
	return &o, nil
106
}
107
108
func Update_Order(db *sql.DB, id int, fields map[string]interface{}) error {
109
	query := "UPDATE orders SET updated_at = NOW()"
110
	args := []interface{}{}
111
	argCount := 1
112
113
	for key, value := range fields {
114
		query += fmt.Sprintf(", %s = $%d", key, argCount)
115
		args = append(args, value)
116
		argCount++
117
	}
118
119
	query += fmt.Sprintf(" WHERE id = $%d", argCount)
120
	args = append(args, id)
121
122
	_, err := db.Exec(query, args...)
123
	return err
124
}
125
126
func Add_Order_Item(db *sql.DB, order_id, product_id, variant_id, quantity, price int) error {
127
	_, err := db.Exec(`
128
		INSERT INTO order_items (order_id, product_id, variant_id, quantity, price)
129
		VALUES ($1, $2, $3, $4, $5)
130
	`, order_id, product_id, variant_id, quantity, price)
131
	return err
132
}
133
134
func Get_Order_Items(db *sql.DB, order_id int) ([]Order_Item_Detail, error) {
135
	rows, err := db.Query(`
136
		SELECT oi.id, oi.order_id, oi.product_id, oi.variant_id, oi.quantity, oi.price,
137
		       p.name as product_name, v.size
138
		FROM order_items oi
139
		JOIN products p ON p.id = oi.product_id
140
		JOIN variants v ON v.id = oi.variant_id
141
		WHERE oi.order_id = $1
142
	`, order_id)
143
	if err != nil {
144
		return nil, err
145
	}
146
	defer rows.Close()
147
148
	var items []Order_Item_Detail
149
	for rows.Next() {
150
		var item Order_Item_Detail
151
		if err := rows.Scan(
152
			&item.ID, &item.Order_ID, &item.Product_ID, &item.Variant_ID,
153
			&item.Quantity, &item.Price, &item.Product_Name, &item.Size,
154
		); err != nil {
155
			return nil, err
156
		}
157
		items = append(items, item)
158
	}
159
	return items, rows.Err()
160
}