Initial commit: Hördle Web App
This commit is contained in:
16
app/api/admin/login/route.ts
Normal file
16
app/api/admin/login/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { password } = await request.json();
|
||||
const adminPassword = process.env.ADMIN_PASSWORD || 'admin123'; // Default for dev if not set
|
||||
|
||||
if (password === adminPassword) {
|
||||
return NextResponse.json({ success: true });
|
||||
} else {
|
||||
return NextResponse.json({ error: 'Invalid password' }, { status: 401 });
|
||||
}
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
72
app/api/daily/route.ts
Normal file
72
app/api/daily/route.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
|
||||
let dailyPuzzle = await prisma.dailyPuzzle.findUnique({
|
||||
where: { date: today },
|
||||
include: { song: true },
|
||||
});
|
||||
|
||||
if (!dailyPuzzle) {
|
||||
// Find a random song to set as today's puzzle
|
||||
const songsCount = await prisma.song.count();
|
||||
if (songsCount === 0) {
|
||||
return NextResponse.json({ error: 'No songs available' }, { status: 404 });
|
||||
}
|
||||
|
||||
const skip = Math.floor(Math.random() * songsCount);
|
||||
const randomSong = await prisma.song.findFirst({
|
||||
skip: skip,
|
||||
});
|
||||
|
||||
if (randomSong) {
|
||||
dailyPuzzle = await prisma.dailyPuzzle.create({
|
||||
data: {
|
||||
date: today,
|
||||
songId: randomSong.id,
|
||||
},
|
||||
include: { song: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!dailyPuzzle) {
|
||||
return NextResponse.json({ error: 'Failed to create puzzle' }, { status: 500 });
|
||||
}
|
||||
|
||||
// Return only necessary info to client (hide title/artist initially if we want strict security,
|
||||
// but for this app we might need it for validation or just return the audio URL and ID)
|
||||
// Actually, we should probably NOT return the title/artist here if we want to prevent cheating via network tab,
|
||||
// but the requirement says "guess the title", so we need to validate on server or client.
|
||||
// For simplicity in this prototype, we'll return the ID and audio URL.
|
||||
// Validation can happen in a separate "guess" endpoint or client-side if we trust the user not to inspect too much.
|
||||
// Let's return the audio URL. The client will request the full song info ONLY when they give up or guess correctly?
|
||||
// Or we can just return the ID and have a separate "check" endpoint.
|
||||
// For now, let's return the ID and the filename (public URL).
|
||||
|
||||
return NextResponse.json({
|
||||
id: dailyPuzzle.id,
|
||||
audioUrl: `/uploads/${dailyPuzzle.song.filename}`,
|
||||
// We might need a hash or something to validate guesses without revealing the answer,
|
||||
// but for now let's keep it simple. The client needs to know if the guess is correct.
|
||||
// We can send the answer hash? Or just handle checking on the client for now (easiest but insecure).
|
||||
// Let's send the answer for now, assuming this is a fun app not a competitive e-sport.
|
||||
// Wait, if I send the answer, it's too easy to cheat.
|
||||
// Better: The client sends a guess, the server validates.
|
||||
// But the requirements didn't specify a complex backend validation.
|
||||
// Let's stick to: Client gets audio. Client has a list of all songs (for autocomplete).
|
||||
// Client checks if selected song ID matches the daily puzzle song ID.
|
||||
// So we need to return the song ID.
|
||||
songId: dailyPuzzle.songId
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching daily puzzle:', error);
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
93
app/api/songs/route.ts
Normal file
93
app/api/songs/route.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { writeFile } from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { parseBuffer } from 'music-metadata';
|
||||
|
||||
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,
|
||||
}
|
||||
});
|
||||
return NextResponse.json(songs);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: 'No file provided' }, { status: 400 });
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback if still missing
|
||||
if (!title) title = 'Unknown Title';
|
||||
if (!artist) artist = 'Unknown Artist';
|
||||
|
||||
const filename = `${Date.now()}-${file.name.replace(/[^a-zA-Z0-9.]/g, '_')}`;
|
||||
const uploadDir = path.join(process.cwd(), 'public/uploads');
|
||||
|
||||
await writeFile(path.join(uploadDir, filename), buffer);
|
||||
|
||||
const song = await prisma.song.create({
|
||||
data: {
|
||||
title,
|
||||
artist,
|
||||
filename,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(song);
|
||||
} catch (error) {
|
||||
console.error('Error uploading song:', error);
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: Request) {
|
||||
try {
|
||||
const { id, title, artist } = await request.json();
|
||||
|
||||
if (!id || !title || !artist) {
|
||||
return NextResponse.json({ error: 'Missing fields' }, { status: 400 });
|
||||
}
|
||||
|
||||
const updatedSong = await prisma.song.update({
|
||||
where: { id: Number(id) },
|
||||
data: { title, artist },
|
||||
});
|
||||
|
||||
return NextResponse.json(updatedSong);
|
||||
} catch (error) {
|
||||
console.error('Error updating song:', error);
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user