feat: improve character update logic and handle haki fields more effectively
This commit is contained in:
@@ -85,24 +85,33 @@ export const actions: Actions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const updates: Record<string, any> = {
|
const [originalCharacter] = await db
|
||||||
// Initialize boolean fields to false (they'll be set to true if present in formData)
|
.select({
|
||||||
hakiObservation: false,
|
hakiObservation: character.hakiObservation,
|
||||||
hakiArmament: false,
|
hakiArmament: character.hakiArmament,
|
||||||
hakiConqueror: false
|
hakiConqueror: character.hakiConqueror
|
||||||
};
|
})
|
||||||
|
.from(character)
|
||||||
|
.where(eq(character.id, id))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!originalCharacter) {
|
||||||
|
return fail(404, { error: 'Character not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const updates: Record<string, any> = {};
|
||||||
|
|
||||||
formData.forEach((value, key) => {
|
formData.forEach((value, key) => {
|
||||||
if (key !== 'id') {
|
if (key !== 'id') {
|
||||||
// Handle checkboxes (haki fields)
|
|
||||||
if (key === 'hakiObservation' || key === 'hakiArmament' || key === 'hakiConqueror') {
|
|
||||||
updates[key] = value === 'on';
|
|
||||||
}
|
|
||||||
// Handle integers (age, bounty, height, devilFruitId, arcId)
|
// Handle integers (age, bounty, height, devilFruitId, arcId)
|
||||||
else if (key === 'age' || key === 'bounty' || key === 'height' || key === 'devilFruitId' || key === 'arcId') {
|
if (key === 'age' || key === 'bounty' || key === 'height' || key === 'devilFruitId' || key === 'arcId') {
|
||||||
const strValue = value as string;
|
const strValue = value as string;
|
||||||
updates[key] = strValue && strValue !== '' ? parseInt(strValue) : null;
|
updates[key] = strValue && strValue !== '' ? parseInt(strValue) : null;
|
||||||
}
|
}
|
||||||
|
// Handle checkboxes (haki fields) after parsing all form data
|
||||||
|
else if (key === 'hakiObservation' || key === 'hakiArmament' || key === 'hakiConqueror') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Handle strings (name, gender, status, origin, affiliations, epithets, pictureUrl, url, firstAppearance)
|
// Handle strings (name, gender, status, origin, affiliations, epithets, pictureUrl, url, firstAppearance)
|
||||||
else {
|
else {
|
||||||
updates[key] = value || null;
|
updates[key] = value || null;
|
||||||
@@ -110,6 +119,17 @@ export const actions: Actions = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const submittedHakiObservation = formData.has('hakiObservation');
|
||||||
|
const submittedHakiArmament = formData.has('hakiArmament');
|
||||||
|
const submittedHakiConqueror = formData.has('hakiConqueror');
|
||||||
|
|
||||||
|
updates.hakiObservation =
|
||||||
|
submittedHakiObservation === originalCharacter.hakiObservation ? null : submittedHakiObservation;
|
||||||
|
updates.hakiArmament =
|
||||||
|
submittedHakiArmament === originalCharacter.hakiArmament ? null : submittedHakiArmament;
|
||||||
|
updates.hakiConqueror =
|
||||||
|
submittedHakiConqueror === originalCharacter.hakiConqueror ? null : submittedHakiConqueror;
|
||||||
|
|
||||||
// Update or insert into characterOverride table
|
// Update or insert into characterOverride table
|
||||||
await db
|
await db
|
||||||
.insert(characterOverride)
|
.insert(characterOverride)
|
||||||
|
|||||||
@@ -148,9 +148,9 @@
|
|||||||
pictureUrl: override.pictureUrl ?? '',
|
pictureUrl: override.pictureUrl ?? '',
|
||||||
url: override.url ?? '',
|
url: override.url ?? '',
|
||||||
devilFruitId: override.devilFruitId !== null && override.devilFruitId !== undefined ? override.devilFruitId : '',
|
devilFruitId: override.devilFruitId !== null && override.devilFruitId !== undefined ? override.devilFruitId : '',
|
||||||
hakiObservation: override.hakiObservation ?? false,
|
hakiObservation: override.hakiObservation ?? char.hakiObservation,
|
||||||
hakiArmament: override.hakiArmament ?? false,
|
hakiArmament: override.hakiArmament ?? char.hakiArmament,
|
||||||
hakiConqueror: override.hakiConqueror ?? false,
|
hakiConqueror: override.hakiConqueror ?? char.hakiConqueror,
|
||||||
firstAppearance: override.firstAppearance ?? '',
|
firstAppearance: override.firstAppearance ?? '',
|
||||||
arcId: override.arcId !== null && override.arcId !== undefined ? override.arcId : '',
|
arcId: override.arcId !== null && override.arcId !== undefined ? override.arcId : '',
|
||||||
status: override.status ?? ''
|
status: override.status ?? ''
|
||||||
|
|||||||
Reference in New Issue
Block a user