|
|
@@ -739,6 +739,61 @@ func guard(r *http.Request, required int) bool { |
|
|
|
return true |
|
|
|
} |
|
|
|
|
|
|
|
// Inserts an address and returns it's ID along with any errors. |
|
|
|
func insertAddress(db *sql.DB, address Address) (int, error){ |
|
|
|
var query string |
|
|
|
var row *sql.Row |
|
|
|
var err error |
|
|
|
var id int // Inserted user's id |
|
|
|
|
|
|
|
query = `INSERT INTO address |
|
|
|
( |
|
|
|
full, |
|
|
|
street, |
|
|
|
city, |
|
|
|
region, |
|
|
|
country, |
|
|
|
zip |
|
|
|
) |
|
|
|
VALUES (?, ?, ?, ?, ?, ?) |
|
|
|
RETURNING id |
|
|
|
` |
|
|
|
|
|
|
|
row = db.QueryRow(query, |
|
|
|
address.Full, |
|
|
|
address.Street, |
|
|
|
address.City, |
|
|
|
address.Region, |
|
|
|
address.Country, |
|
|
|
address.Zip, |
|
|
|
) |
|
|
|
|
|
|
|
err = row.Scan(&id) |
|
|
|
|
|
|
|
return id, err |
|
|
|
} |
|
|
|
|
|
|
|
func queryAddress(db *sql.DB, id int) ( Address, error ) { |
|
|
|
var address Address = Address{Id: id} |
|
|
|
var err error |
|
|
|
|
|
|
|
row := db.QueryRow( |
|
|
|
`SELECT id, full_address, street, city, region, country, zip |
|
|
|
FROM address WHERE id = ?`, id) |
|
|
|
|
|
|
|
err = row.Scan( |
|
|
|
&address.Id, |
|
|
|
&address.Full, |
|
|
|
&address.Street, |
|
|
|
&address.City, |
|
|
|
&address.Region, |
|
|
|
&address.Country, |
|
|
|
&address.Zip, |
|
|
|
) |
|
|
|
|
|
|
|
return address, err |
|
|
|
} |
|
|
|
|
|
|
|
func queryUsers(db *sql.DB, id int) ( []User, error ) { |
|
|
|
var users []User |
|
|
|
var query string |
|
|
@@ -755,7 +810,8 @@ func queryUsers(db *sql.DB, id int) ( []User, error ) { |
|
|
|
u.title, |
|
|
|
u.status, |
|
|
|
u.verified, |
|
|
|
u.role |
|
|
|
u.role, |
|
|
|
u.address |
|
|
|
FROM user u WHERE u.id = CASE @e := ? WHEN 0 THEN u.id ELSE @e END |
|
|
|
` |
|
|
|
rows, err = db.Query(query, id) |
|
|
@@ -781,10 +837,17 @@ func queryUsers(db *sql.DB, id int) ( []User, error ) { |
|
|
|
&user.Status, |
|
|
|
&user.Verified, |
|
|
|
&user.Role, |
|
|
|
&user.Address.Id, |
|
|
|
) |
|
|
|
err != nil { |
|
|
|
return users, err |
|
|
|
} |
|
|
|
|
|
|
|
user.Address, err = queryAddress(db, user.Address.Id) |
|
|
|
if err != nil { |
|
|
|
return users, err |
|
|
|
} |
|
|
|
|
|
|
|
users = append(users, user) |
|
|
|
} |
|
|
|
|
|
|
@@ -833,6 +896,9 @@ func insertUser(db *sql.DB, user User) (User, error){ |
|
|
|
var row *sql.Row |
|
|
|
var err error |
|
|
|
var id int // Inserted user's id |
|
|
|
|
|
|
|
user.Address.Id, err = insertAddress(db, user.Address) |
|
|
|
if err != nil { return user, err } |
|
|
|
|
|
|
|
query = `INSERT INTO user |
|
|
|
( |
|
|
@@ -843,9 +909,10 @@ func insertUser(db *sql.DB, user User) (User, error){ |
|
|
|
created, |
|
|
|
role, |
|
|
|
verified, |
|
|
|
address, |
|
|
|
last_login |
|
|
|
) |
|
|
|
VALUES (?, ?, ?, sha2(?, 256), NOW(), ?, ?, NOW()) |
|
|
|
VALUES (?, ?, ?, sha2(?, 256), NOW(), ?, ?, ?, NOW()) |
|
|
|
RETURNING id |
|
|
|
` |
|
|
|
row = db.QueryRow(query, |
|
|
@@ -855,6 +922,7 @@ func insertUser(db *sql.DB, user User) (User, error){ |
|
|
|
user.Password, |
|
|
|
user.Role, |
|
|
|
user.Verified, |
|
|
|
user.Address.Id, |
|
|
|
) |
|
|
|
|
|
|
|
err = row.Scan(&id) |
|
|
@@ -866,6 +934,32 @@ func insertUser(db *sql.DB, user User) (User, error){ |
|
|
|
return users[0], nil |
|
|
|
} |
|
|
|
|
|
|
|
func updateAddress(address Address, db *sql.DB) error { |
|
|
|
query := ` |
|
|
|
UPDATE address |
|
|
|
SET |
|
|
|
full_address = CASE @e := ? WHEN '' THEN full_address ELSE @e END, |
|
|
|
street = CASE @fn := ? WHEN '' THEN street ELSE @fn END, |
|
|
|
city = CASE @ln := ? WHEN '' THEN city ELSE @ln END, |
|
|
|
region = CASE @r := ? WHEN '' THEN region ELSE @r END, |
|
|
|
country = CASE @r := ? WHEN '' THEN country ELSE @r END, |
|
|
|
zip = CASE @r := ? WHEN '' THEN zip ELSE @r END |
|
|
|
WHERE id = ? |
|
|
|
` |
|
|
|
|
|
|
|
_, err := db.Exec(query, |
|
|
|
address.Full, |
|
|
|
address.Street, |
|
|
|
address.City, |
|
|
|
address.Region, |
|
|
|
address.Country, |
|
|
|
address.Zip, |
|
|
|
address.Id, |
|
|
|
) |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
func updateUser(user User, db *sql.DB) error { |
|
|
|
query := ` |
|
|
|
UPDATE user |
|
|
@@ -920,6 +1014,8 @@ func setUser(user User, db *sql.DB) error { |
|
|
|
|
|
|
|
err = updateUser(user, db) |
|
|
|
if err != nil { return err } |
|
|
|
err = updateAddress(user.Address, db) |
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
@@ -1740,8 +1836,6 @@ func showPDF(w http.ResponseWriter, r *http.Request) { |
|
|
|
User: users[0], |
|
|
|
} |
|
|
|
|
|
|
|
// fmt.Println(info) |
|
|
|
|
|
|
|
err = pa.Execute(w, info) |
|
|
|
if err != nil {fmt.Println(err)} |
|
|
|
|
|
|
|