feat: implement song rating system with admin view and user persistence

This commit is contained in:
Hördle Bot
2025-11-23 10:34:45 +01:00
parent dc83c8372f
commit 4d3032df36
7 changed files with 191 additions and 11 deletions

View File

@@ -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' };
}
}

View File

@@ -42,6 +42,8 @@ interface Song {
puzzles: DailyPuzzle[];
genres: Genre[];
specials: Special[];
averageRating: number;
ratingCount: number;
}
type SortField = 'id' | 'title' | 'artist' | 'createdAt';
@@ -1141,6 +1143,7 @@ export default function AdminPage() {
Added {sortField === 'createdAt' && (sortDirection === 'asc' ? '' : '')}
</th>
<th style={{ padding: '0.75rem' }}>Activations</th>
<th style={{ padding: '0.75rem' }}>Rating</th>
<th style={{ padding: '0.75rem' }}>Actions</th>
</tr>
</thead>
@@ -1211,6 +1214,15 @@ export default function AdminPage() {
{new Date(song.createdAt).toLocaleDateString('de-DE')}
</td>
<td style={{ padding: '0.75rem', color: '#666' }}>{song.activations}</td>
<td style={{ padding: '0.75rem', color: '#666' }}>
{song.averageRating > 0 ? (
<span title={`${song.ratingCount} ratings`}>
{song.averageRating.toFixed(1)} ★ <span style={{ color: '#999', fontSize: '0.8rem' }}>({song.ratingCount})</span>
</span>
) : (
<span style={{ color: '#ccc' }}>-</span>
)}
</td>
<td style={{ padding: '0.75rem' }}>
<div style={{ display: 'flex', gap: '0.5rem' }}>
<button
@@ -1297,6 +1309,15 @@ export default function AdminPage() {
{new Date(song.createdAt).toLocaleDateString('de-DE')}
</td>
<td style={{ padding: '0.75rem', color: '#666' }}>{song.activations}</td>
<td style={{ padding: '0.75rem', color: '#666' }}>
{song.averageRating > 0 ? (
<span title={`${song.ratingCount} ratings`}>
{song.averageRating.toFixed(1)} ★ <span style={{ color: '#999', fontSize: '0.8rem' }}>({song.ratingCount})</span>
</span>
) : (
<span style={{ color: '#ccc' }}>-</span>
)}
</td>
<td style={{ padding: '0.75rem' }}>
<div style={{ display: 'flex', gap: '0.5rem' }}>
<button

View File

@@ -32,6 +32,8 @@ export async function GET() {
puzzles: song.puzzles,
genres: song.genres,
specials: song.specials.map(ss => ss.special),
averageRating: song.averageRating,
ratingCount: song.ratingCount,
}));
return NextResponse.json(songsWithActivations);