浏览代码

Save changed user address in database

master
父节点
当前提交
e77294f777
共有 4 个文件被更改,包括 126 次插入25 次删除
  1. +16
    -12
      components/settings.vue
  2. +6
    -6
      migrations/0_29092022_setup_tables.sql
  3. +6
    -3
      migrations/seed.sql
  4. +98
    -4
      skouter.go

+ 16
- 12
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(() => {
})

</script>

<style scoped>
div.address-entry input {
width: 100%;
div.address-entry {
display: flex;
flex-flow: column;
}
</style>

+ 6
- 6
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`)
);



+ 6
- 3
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


+ 98
- 4
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)}



正在加载...
取消
保存