feat: implement song rating system with admin view and user persistence
This commit is contained in:
@@ -30,3 +30,44 @@ export async function sendGotifyNotification(attempts: number, status: 'won' | '
|
||||
console.error('Error sending Gotify notification:', error);
|
||||
}
|
||||
}
|
||||
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function submitRating(songId: number, rating: number, genre?: string | null) {
|
||||
try {
|
||||
const song = await prisma.song.findUnique({ where: { id: songId } });
|
||||
if (!song) throw new Error('Song not found');
|
||||
|
||||
const newRatingCount = song.ratingCount + 1;
|
||||
const newAverageRating = ((song.averageRating * song.ratingCount) + rating) / newRatingCount;
|
||||
|
||||
await prisma.song.update({
|
||||
where: { id: songId },
|
||||
data: {
|
||||
averageRating: newAverageRating,
|
||||
ratingCount: newRatingCount,
|
||||
},
|
||||
});
|
||||
|
||||
// Send Gotify notification for the rating
|
||||
const genreText = genre ? `[${genre}] ` : '';
|
||||
const title = `Hördle Rating: ${rating} Stars`;
|
||||
const message = `Song "${song.title}" by ${song.artist} ${genreText}received a ${rating}-star rating. (Avg: ${newAverageRating.toFixed(2)}, Count: ${newRatingCount})`;
|
||||
|
||||
await fetch(`${GOTIFY_URL}/message?token=${GOTIFY_APP_TOKEN}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: title,
|
||||
message: message,
|
||||
priority: 5,
|
||||
}),
|
||||
});
|
||||
|
||||
return { success: true, averageRating: newAverageRating };
|
||||
} catch (error) {
|
||||
console.error('Error submitting rating:', error);
|
||||
return { success: false, error: 'Failed to submit rating' };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user