mirror of
https://github.com/joohoi/acme-dns.git
synced 2025-01-19 08:47:37 +07:00
41 lines
760 B
Go
41 lines
760 B
Go
package main
|
|
|
|
import "database/sql"
|
|
|
|
func recordStats(db *sql.DB, userID, productID int64) (err error) {
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
defer func() {
|
|
switch err {
|
|
case nil:
|
|
err = tx.Commit()
|
|
default:
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
if _, err = tx.Exec("UPDATE products SET views = views + 1"); err != nil {
|
|
return
|
|
}
|
|
if _, err = tx.Exec("INSERT INTO product_viewers (user_id, product_id) VALUES (?, ?)", userID, productID); err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func main() {
|
|
// @NOTE: the real connection is not required for tests
|
|
db, err := sql.Open("mysql", "root@/blog")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
if err = recordStats(db, 1 /*some user id*/, 5 /*some product id*/); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|