trainUs/src/db/repositories/suffixRepository.js
David 352a3d5e88
Some checks are pending
CI / Lint & Format (push) Waiting to run
CI / Build (push) Waiting to run
CI / Tests (${{ matrix.browser }}) (chromium) (push) Waiting to run
CI / Tests (${{ matrix.browser }}) (firefox) (push) Waiting to run
Initial commit
2026-06-18 13:06:57 +02:00

70 lines
2 KiB
JavaScript

import { db } from '../database.js'
export const suffixRepository = {
async create(entry) {
await db.run('INSERT INTO suffix (user_uuid, user_name, suffix) VALUES (?, ?, ?)', [
entry.user_uuid,
entry.user_name,
entry.suffix,
])
},
async getByUuid(userUuid) {
return db.selectOne('SELECT * FROM suffix WHERE user_uuid = ?', [userUuid])
},
async getBySuffix(suffix) {
return db.selectOne('SELECT * FROM suffix WHERE suffix = ?', [suffix])
},
async getAll() {
return db.selectAll('SELECT * FROM suffix ORDER BY user_name')
},
async update(userUuid, data) {
return db.run('UPDATE suffix SET user_name = ?, suffix = ? WHERE user_uuid = ?', [
data.user_name,
data.suffix,
userUuid,
])
},
async delete(userUuid) {
return db.run('DELETE FROM suffix WHERE user_uuid = ?', [userUuid])
},
/**
* Renames a suffix across all entity title/label columns.
* Updates suffix table entry + all titles containing [OLD_SUFFIX] to [NEW_SUFFIX].
*/
async renameSuffix(userUuid, newSuffix) {
const existing = await this.getByUuid(userUuid)
if (!existing) throw new Error('Suffix entry not found')
const oldPattern = `[${existing.suffix}]`
const newPattern = `[${newSuffix}]`
// Update suffix table
await db.run('UPDATE suffix SET suffix = ? WHERE user_uuid = ?', [newSuffix, userUuid])
// Update titles across entity tables
await db.run(
"UPDATE exercise SET title = REPLACE(title, ?, ?) WHERE title LIKE '%' || ? || '%'",
[oldPattern, newPattern, oldPattern],
)
await db.run("UPDATE combo SET title = REPLACE(title, ?, ?) WHERE title LIKE '%' || ? || '%'", [
oldPattern,
newPattern,
oldPattern,
])
await db.run(
"UPDATE session SET title = REPLACE(title, ?, ?) WHERE title LIKE '%' || ? || '%'",
[oldPattern, newPattern, oldPattern],
)
await db.run(
"UPDATE collection SET label = REPLACE(label, ?, ?) WHERE label LIKE '%' || ? || '%'",
[oldPattern, newPattern, oldPattern],
)
},
}