Skip to content

Commit

Permalink
sql: return indicative boolean for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Mar 7, 2024
1 parent fdb0520 commit 9687bb6
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 46 deletions.
18 changes: 10 additions & 8 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,32 @@ class Group {
if (!args.id) return false
const id = args.id
delete args.id
// Mysql.debug(1)
const r = await Mysql.execute(
...Mysql.update(
`nt_group`,
`nt_group_id=${id}`,
mapToDbColumn(args, groupDbMap),
),
)
// console.log(r)
return r.changedRows === 1
}

async delete(args) {
await Mysql.execute(`UPDATE nt_group SET deleted=? WHERE nt_group_id=?`, [
args.deleted ?? 1,
args.id,
])
return true
const r = await Mysql.execute(
...Mysql.update(
`nt_group`,
`nt_group_id=${args.id}`,
{ deleted: args.deleted ?? 1 },
),
)
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_group`, { nt_group_id: args.id }),
)
return r.affectedRows === 1
}
}

Expand Down
4 changes: 0 additions & 4 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ class Mysql {
}

if (/^(REPLACE|INSERT) INTO/.test(query)) return rows.insertId
if (/^UPDATE /.test(query)) {
console.log(rows)
console.log(fields)
}

return rows
}
Expand Down
15 changes: 9 additions & 6 deletions lib/nameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,25 @@ class Nameserver {
mapToDbColumn(args, nsDbMap),
),
)
// console.log(r)
return r.changedRows === 1
}

async delete(args) {
await Mysql.execute(
`UPDATE nt_nameserver SET deleted=? WHERE nt_nameserver_id=?`,
[args.deleted ?? 1, args.id],
const r = await Mysql.execute(
...Mysql.update(
`nt_nameserver`,
`nt_nameserver_id=${args.id}`,
{ deleted: args.deleted ?? 1 },
),
)
return true
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_nameserver`, { nt_nameserver_id: args.id }),
)
return r.affectedRows === 1
}
}

Expand Down
19 changes: 11 additions & 8 deletions lib/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class Permission {
${getPermFields()}
, p.deleted
FROM nt_perm p`
// Mysql.debug(1)

const rows = await Mysql.execute(
...Mysql.select(query, mapToDbColumn(args, permDbMap)),
Expand Down Expand Up @@ -76,7 +75,6 @@ class Permission {
if (!args.id) return false
const id = args.id
delete args.id
// Mysql.debug(1)
const r = await Mysql.execute(
...Mysql.update(
`nt_perm`,
Expand All @@ -88,17 +86,22 @@ class Permission {
}

async delete(args) {
await Mysql.execute(`UPDATE nt_perm SET deleted=? WHERE nt_perm_id=?`, [
args.deleted ?? 1,
args.id,
])
return true
if (!args.id) return false
const r = await Mysql.execute(
...Mysql.update(
`nt_perm`,
`nt_perm_id=${args.id}`,
{ deleted: args.deleted ?? 1 },
),
)
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_perm`, mapToDbColumn(args, permDbMap)),
)
return r.affectedRows === 1
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/permission.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ describe('permission', function () {
})

it('destroys a permission', async () => {
const r = await Permission.destroy({ id: permTestCase.id })
assert.equal(r.affectedRows, 1)
assert.ok(await Permission.destroy({ id: permTestCase.id }))
const p = await Permission.get({ id: permTestCase.id })
assert.equal(p, undefined)
})
Expand Down
10 changes: 7 additions & 3 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,20 @@ class User {

async delete(args) {
const r = await Mysql.execute(
`UPDATE nt_user SET deleted=? WHERE nt_user_id=?`,
[args.deleted ?? 1, args.id],
...Mysql.update(
`nt_user`,
`nt_user_id=${args.id}`,
{ deleted: args.deleted ?? 1 },
),
)
return r.changedRows === 1
}

async destroy(args) {
await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_user`, mapToDbColumn({ id: args.id }, userDbMap)),
)
return r.affectedRows === 1
}

generateSalt(length = 16) {
Expand Down
4 changes: 2 additions & 2 deletions lib/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ describe('user', function () {
describe('DELETE', function () {
it('deletes a user', async () => {
assert.ok(await User.delete({ id: testCase.id }))
let u = await User.get({ id: testCase.id, deleted: 1 })
let u = await User.get({ id: testCase.id, deleted: true })
assert.equal(u[0].deleted, true)
await User.delete({ id: testCase.id, deleted: 0 }) // restore
await User.delete({ id: testCase.id, deleted: false }) // restore
u = await User.get({ id: testCase.id })
assert.equal(u[0].deleted, undefined)
})
Expand Down
16 changes: 10 additions & 6 deletions lib/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,21 @@ class Zone {
}

async delete(args) {
await Mysql.execute(`UPDATE nt_zone SET deleted=? WHERE nt_zone_id=?`, [
args.deleted ?? 1,
args.id,
])
return true
const r = await Mysql.execute(
...Mysql.update(
`nt_zone`,
`nt_zone_id=${args.id}`,
{ deleted: args.deleted ?? 1 },
),
)
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_zone`, { nt_zone_id: args.id }),
)
return r.affectedRows === 1
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/zone_record.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,19 @@ class ZoneRecord {
}

async delete(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.update(`nt_zone_record`, `nt_zone_record_id=${args.id}`, {
deleted: args.deleted ?? 1,
}),
)
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_zone_record`, { nt_zone_record_id: args.id }),
)
return r.affectedRows === 1
}
}

Expand Down
3 changes: 1 addition & 2 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ function UserRoutes(server) {
.code(204)
}

const action = request.query.destroy === 'true' ? 'destroy' : 'delete'
await User[action]({ id: users[0].id })
await User.delete({ id: users[0].id })

delete users[0].gid

Expand Down
6 changes: 3 additions & 3 deletions routes/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ describe('user routes', () => {
},
})
// console.log(res.result)
assert.equal(res.statusCode, 204)
assert.ok([200, 204].includes(res.statusCode))
})

it(`GET /user/${userId2}?deleted=1`, async () => {
it(`GET /user/${userId2}?deleted=true`, async () => {
const res = await server.inject({
method: 'GET',
url: `/user/${userId2}?deleted=true`,
Expand All @@ -128,7 +128,7 @@ describe('user routes', () => {
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
assert.ok([200, 204].includes(res.statusCode))
})

it('DELETE /session', async () => {
Expand Down

0 comments on commit 9687bb6

Please sign in to comment.