From e77294f7771028f809f1997fbf1cdd24682df8d9 Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Sun, 27 Aug 2023 20:39:12 -0400 Subject: [PATCH] Save changed user address in database --- components/settings.vue | 28 ++++--- migrations/0_29092022_setup_tables.sql | 12 +-- migrations/seed.sql | 9 ++- skouter.go | 102 ++++++++++++++++++++++++- 4 files changed, 126 insertions(+), 25 deletions(-) diff --git a/components/settings.vue b/components/settings.vue index 4f8cee3..fc48171 100644 --- a/components/settings.vue +++ b/components/settings.vue @@ -94,7 +94,7 @@ const addresses = ref([]) const address = ref({}) const props = defineProps(['user', 'token']) const emit = defineEmits(['updateAvatar', 'updateLetterhead']) -let user = Object.assign({}, props.user) +const user = Object.assign({}, props.user) function save() { } @@ -191,21 +191,24 @@ function changeLetterhead(blob) { } function setAddress(a) { - addresses.value = null + address.value = { + id: address.value.id, full: a.full_address, street: a.address, - city: a.context.place.name, - region: a.context.region.name, - country: a.context.country.name, - zip: a.context.postcode.name, + city: a.context?.place.name ?? '', + region: a.context?.region?.name ?? '', + country: a.context?.country?.name ?? '', + zip: a.context?.postcode?.name ?? '', } + addresses.value = null } function saveProfile() { + user.value.address = address.value fetch(`/api/user`, {method: 'PATCH', - body: JSON.stringify(user), + body: JSON.stringify(user.value), headers: { "Accept": "application/json", "Authorization": `Bearer ${props.token}`, @@ -274,15 +277,16 @@ watch(props.user, (u) => { if (props.user.letterhead) { changeLetterhead(props.user.letterhead) } + address.value = Object.assign({}, props.user.address) + user.value = Object.assign({}, props.user) + user.value.address = address.value }, {immediate: true}) -onMounted(() => { -}) - diff --git a/migrations/0_29092022_setup_tables.sql b/migrations/0_29092022_setup_tables.sql index 613906d..d25de65 100644 --- a/migrations/0_29092022_setup_tables.sql +++ b/migrations/0_29092022_setup_tables.sql @@ -2,12 +2,12 @@ CREATE TABLE address ( id INT AUTO_INCREMENT, - full VARCHAR(200) UNIQUE NOT NULL, - street VARCHAR(40) UNIQUE NOT NULL, - city VARCHAR(40) UNIQUE NOT NULL, - region VARCHAR(40) UNIQUE NOT NULL, - country VARCHAR(40) UNIQUE NOT NULL, - zip VARCHAR(40) UNIQUE NOT NULL, + full_address VARCHAR(200) NOT NULL, + street VARCHAR(40) NOT NULL, + city VARCHAR(40) NOT NULL, + region VARCHAR(40) NOT NULL, + country VARCHAR(40) NOT NULL, + zip VARCHAR(10) NOT NULL, PRIMARY KEY (`id`) ); diff --git a/migrations/seed.sql b/migrations/seed.sql index e2cca16..ccb2b71 100644 --- a/migrations/seed.sql +++ b/migrations/seed.sql @@ -1,7 +1,8 @@ -INSERT IGNORE INTO address - (street, city, region, country, zip) +INSERT INTO address + (full_address, street, city, region, country, zip) VALUES ( + '443 Rideau Street K1N 2B8, Ottawa, Ontario, Canada', '443 Rideau Street', 'Ottawa', 'Ontario', @@ -9,10 +10,12 @@ VALUES 'K1N 2B8' ), ( + '221 Mountainview Parkway K1N 2B8, Mountainview, San Francisco, USA', '221 Mountainview Parkway', 'Mountainview', 'San Francisco', - 'USA', 'K1N 2B8' + 'USA', + 'K1N 2B8' ); INSERT IGNORE INTO branch diff --git a/skouter.go b/skouter.go index faf4143..56d22a2 100644 --- a/skouter.go +++ b/skouter.go @@ -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)}