feat: Improve admin dashboard and add weighted song selection
- Replace filename column with activation count - Add pagination (10 items per page) and search functionality - Add delete functionality (removes DB entry and file) - Remove manual title/artist input (auto-extract from ID3 tags) - Replace text buttons with emoji icons (Edit, Delete, Save, Cancel) - Implement weighted random selection for daily puzzles - Add custom favicon - Fix docker-compose.yml configuration
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { writeFile } from 'fs/promises';
|
||||
import { writeFile, unlink } from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { parseBuffer } from 'music-metadata';
|
||||
|
||||
@@ -9,23 +9,30 @@ const prisma = new PrismaClient();
|
||||
export async function GET() {
|
||||
const songs = await prisma.song.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
artist: true,
|
||||
filename: true,
|
||||
createdAt: true,
|
||||
}
|
||||
include: {
|
||||
puzzles: true,
|
||||
},
|
||||
});
|
||||
return NextResponse.json(songs);
|
||||
|
||||
// Map to include activation count
|
||||
const songsWithActivations = songs.map(song => ({
|
||||
id: song.id,
|
||||
title: song.title,
|
||||
artist: song.artist,
|
||||
filename: song.filename,
|
||||
createdAt: song.createdAt,
|
||||
activations: song.puzzles.length,
|
||||
}));
|
||||
|
||||
return NextResponse.json(songsWithActivations);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file') as File;
|
||||
let title = formData.get('title') as string;
|
||||
let artist = formData.get('artist') as string;
|
||||
let title = '';
|
||||
let artist = '';
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: 'No file provided' }, { status: 400 });
|
||||
@@ -33,19 +40,17 @@ export async function POST(request: Request) {
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
|
||||
// Try to extract metadata if title or artist are missing
|
||||
if (!title || !artist) {
|
||||
try {
|
||||
const metadata = await parseBuffer(buffer, file.type);
|
||||
if (!title && metadata.common.title) {
|
||||
title = metadata.common.title;
|
||||
}
|
||||
if (!artist && metadata.common.artist) {
|
||||
artist = metadata.common.artist;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to parse metadata:', e);
|
||||
// Extract metadata from file
|
||||
try {
|
||||
const metadata = await parseBuffer(buffer, file.type);
|
||||
if (metadata.common.title) {
|
||||
title = metadata.common.title;
|
||||
}
|
||||
if (metadata.common.artist) {
|
||||
artist = metadata.common.artist;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to parse metadata:', e);
|
||||
}
|
||||
|
||||
// Fallback if still missing
|
||||
@@ -91,3 +96,41 @@ export async function PUT(request: Request) {
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: Request) {
|
||||
try {
|
||||
const { id } = await request.json();
|
||||
|
||||
if (!id) {
|
||||
return NextResponse.json({ error: 'Missing id' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Get song to find filename
|
||||
const song = await prisma.song.findUnique({
|
||||
where: { id: Number(id) },
|
||||
});
|
||||
|
||||
if (!song) {
|
||||
return NextResponse.json({ error: 'Song not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Delete file
|
||||
const filePath = path.join(process.cwd(), 'public/uploads', song.filename);
|
||||
try {
|
||||
await unlink(filePath);
|
||||
} catch (e) {
|
||||
console.error('Failed to delete file:', e);
|
||||
// Continue with DB deletion even if file deletion fails
|
||||
}
|
||||
|
||||
// Delete from database (will cascade delete related puzzles)
|
||||
await prisma.song.delete({
|
||||
where: { id: Number(id) },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting song:', error);
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user