added overall rating
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { redirect, error } from '@sveltejs/kit';
|
||||
import { db } from '$lib/server/db/index.js';
|
||||
import { audioFile, rating, participantProgress, participant } from '$lib/server/db/schema.js';
|
||||
import { audioFile, rating, participantProgress, participant, overallRating } from '$lib/server/db/schema.js';
|
||||
import { eq, and, isNull } from 'drizzle-orm';
|
||||
|
||||
export async function load({ params, url, cookies }) {
|
||||
@@ -252,5 +252,51 @@ export const actions = {
|
||||
console.error('Error updating progress:', error);
|
||||
return { error: 'Failed to update progress' };
|
||||
}
|
||||
},
|
||||
|
||||
saveOverallRating: async ({ request }) => {
|
||||
const data = await request.formData();
|
||||
const participantId = data.get('participantId');
|
||||
const audioFileId = data.get('audioFileId');
|
||||
const overallRatingValue = parseFloat(data.get('overallRating'));
|
||||
|
||||
if (!participantId || !audioFileId || isNaN(overallRatingValue)) {
|
||||
return { error: 'Invalid overall rating data' };
|
||||
}
|
||||
|
||||
try {
|
||||
const overallRatingId = `${participantId}-${audioFileId}-overall-${Date.now()}`;
|
||||
const now = new Date();
|
||||
|
||||
// Use a transaction to ensure atomicity (same pattern as saveRating)
|
||||
await db.transaction(async (tx) => {
|
||||
// Soft delete any existing overall ratings for this participant and audio file (for redo functionality)
|
||||
await tx.update(overallRating)
|
||||
.set({ deletedAt: now })
|
||||
.where(
|
||||
and(
|
||||
eq(overallRating.participantId, participantId),
|
||||
eq(overallRating.audioFileId, audioFileId),
|
||||
isNull(overallRating.deletedAt)
|
||||
)
|
||||
);
|
||||
|
||||
// Always insert new overall rating record
|
||||
await tx.insert(overallRating).values({
|
||||
id: overallRatingId,
|
||||
participantId,
|
||||
audioFileId,
|
||||
value: overallRatingValue,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
deletedAt: null
|
||||
});
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('Error saving overall rating:', error);
|
||||
return { error: 'Failed to save overall rating' };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user