Refactor cape selection logic and remove trigger
Removed the 'unique_active_cape' database trigger and updated the showCape function to manually ensure only one cape is selected per player. This change centralizes the selection logic in application code for better maintainability and error handling.
This commit is contained in:
parent
b6b7cf7fe0
commit
c6afafca2a
@ -258,20 +258,6 @@ async function setupDatabase() {
|
||||
logger.log(`${"unique_active_skin".bold} trigger ready`, ["MariaDB", "yellow"])
|
||||
|
||||
await conn.query(`DROP TRIGGER IF EXISTS unique_active_cape`)
|
||||
await conn.query(`
|
||||
CREATE TRIGGER unique_active_cape
|
||||
AFTER UPDATE ON playersCapes
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.isSelected = 1 THEN
|
||||
UPDATE playersCapes
|
||||
SET isSelected = 0
|
||||
WHERE playerUuid = NEW.playerUuid
|
||||
AND id != NEW.id;
|
||||
END IF;
|
||||
END;
|
||||
`)
|
||||
logger.log(`${"unique_active_cape".bold} trigger ready`, ["MariaDB", "yellow"])
|
||||
|
||||
await conn.query(`DROP TRIGGER IF EXISTS auto_assign_random_default_skin`)
|
||||
await conn.query(`
|
||||
|
||||
@ -416,15 +416,19 @@ async function hideCape(uuid) {
|
||||
|
||||
async function showCape(uuid, hash) {
|
||||
try {
|
||||
const sql = `
|
||||
UPDATE playersCapes
|
||||
SET isSelected = (assetHash = ?)
|
||||
WHERE playerUuid = ?
|
||||
`
|
||||
const result = await database.query(sql, [hash, uuid])
|
||||
return { code: 200, changed: result.affectedRows > 0 }
|
||||
await database.query(
|
||||
"UPDATE playersCapes SET isSelected = 0 WHERE playerUuid = ?",
|
||||
[uuid]
|
||||
)
|
||||
|
||||
const [res] = await database.query(
|
||||
"UPDATE playersCapes SET isSelected = 1 WHERE playerUuid = ? AND assetHash = ?",
|
||||
[uuid, hash]
|
||||
)
|
||||
|
||||
return { code: 200, changed: res.affectedRows > 0 }
|
||||
} catch (error) {
|
||||
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
|
||||
logger.log("Database Error: " + error.toString(), ["MariaDB", "red"])
|
||||
throw new DefaultError(500, "Internal Server Error", "Database Error")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user