main: second iter

Signed-off-by: nfel <nfilsaraee@gmail.com>
This commit is contained in:
2025-12-27 22:41:36 +03:30
parent 8a7842e263
commit 9a7e627329
33 changed files with 4865 additions and 81 deletions
+26
View File
@@ -0,0 +1,26 @@
import { NextRequest, NextResponse } from 'next/server';
import { albumDb } from '@/lib/db';
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const album = albumDb.getById(params.id);
if (!album) {
return NextResponse.json(
{ error: 'Album not found' },
{ status: 404 }
);
}
return NextResponse.json(album, { status: 200 });
} catch (error) {
console.error('Error fetching album:', error);
return NextResponse.json(
{ error: 'Failed to fetch album' },
{ status: 500 }
);
}
}
+115
View File
@@ -0,0 +1,115 @@
import { NextRequest, NextResponse } from 'next/server';
import { albumDb } from '@/lib/db';
import { Album } from '@/lib/types';
// GET - Get all albums
export async function GET() {
try {
const albums = albumDb.getAll();
return NextResponse.json(albums, { status: 200 });
} catch (error) {
console.error('Error fetching albums:', error);
return NextResponse.json(
{ error: 'Failed to fetch albums' },
{ status: 500 }
);
}
}
// POST - Create new album
export async function POST(request: NextRequest) {
try {
const album: Album = await request.json();
// Validate required fields
if (!album.id || !album.title || !album.year || !album.genre || !album.price) {
return NextResponse.json(
{ error: 'Missing required fields' },
{ status: 400 }
);
}
// Check if album already exists
const existing = albumDb.getById(album.id);
if (existing) {
return NextResponse.json(
{ error: 'Album with this ID already exists' },
{ status: 409 }
);
}
albumDb.create(album);
return NextResponse.json(album, { status: 201 });
} catch (error) {
console.error('Error creating album:', error);
return NextResponse.json(
{ error: 'Failed to create album' },
{ status: 500 }
);
}
}
// PUT - Update album
export async function PUT(request: NextRequest) {
try {
const album: Album = await request.json();
if (!album.id) {
return NextResponse.json(
{ error: 'Album ID is required' },
{ status: 400 }
);
}
// Check if album exists
const existing = albumDb.getById(album.id);
if (!existing) {
return NextResponse.json(
{ error: 'Album not found' },
{ status: 404 }
);
}
albumDb.update(album.id, album);
return NextResponse.json(album, { status: 200 });
} catch (error) {
console.error('Error updating album:', error);
return NextResponse.json(
{ error: 'Failed to update album' },
{ status: 500 }
);
}
}
// DELETE - Delete album
export async function DELETE(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
if (!id) {
return NextResponse.json(
{ error: 'Album ID is required' },
{ status: 400 }
);
}
// Check if album exists
const existing = albumDb.getById(id);
if (!existing) {
return NextResponse.json(
{ error: 'Album not found' },
{ status: 404 }
);
}
albumDb.delete(id);
return NextResponse.json({ message: 'Album deleted successfully' }, { status: 200 });
} catch (error) {
console.error('Error deleting album:', error);
return NextResponse.json(
{ error: 'Failed to delete album' },
{ status: 500 }
);
}
}
+89
View File
@@ -0,0 +1,89 @@
import { NextRequest, NextResponse } from 'next/server';
import { purchaseDb } from '@/lib/db';
import { Purchase } from '@/lib/types';
// GET - Get all purchases
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const albumId = searchParams.get('albumId');
let purchases;
if (albumId) {
purchases = purchaseDb.getByAlbumId(albumId);
} else {
purchases = purchaseDb.getAll();
}
return NextResponse.json(purchases, { status: 200 });
} catch (error) {
console.error('Error fetching purchases:', error);
return NextResponse.json(
{ error: 'Failed to fetch purchases' },
{ status: 500 }
);
}
}
// POST - Create new purchase
export async function POST(request: NextRequest) {
try {
const purchaseData: Omit<Purchase, 'id'> = await request.json();
// Validate required fields
if (!purchaseData.albumId || !purchaseData.transactionId) {
return NextResponse.json(
{ error: 'Missing required fields' },
{ status: 400 }
);
}
// Check if transaction ID already exists
const existing = purchaseDb.getByTransactionId(purchaseData.transactionId);
if (existing) {
return NextResponse.json(
{ error: 'Transaction ID already exists' },
{ status: 409 }
);
}
// Ensure purchaseDate is set
const purchase: Omit<Purchase, 'id'> = {
...purchaseData,
purchaseDate: purchaseData.purchaseDate || new Date(),
};
const created = purchaseDb.create(purchase);
return NextResponse.json(created, { status: 201 });
} catch (error) {
console.error('Error creating purchase:', error);
return NextResponse.json(
{ error: 'Failed to create purchase' },
{ status: 500 }
);
}
}
// DELETE - Delete purchase
export async function DELETE(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
if (!id) {
return NextResponse.json(
{ error: 'Purchase ID is required' },
{ status: 400 }
);
}
purchaseDb.delete(parseInt(id));
return NextResponse.json({ message: 'Purchase deleted successfully' }, { status: 200 });
} catch (error) {
console.error('Error deleting purchase:', error);
return NextResponse.json(
{ error: 'Failed to delete purchase' },
{ status: 500 }
);
}
}
+58
View File
@@ -0,0 +1,58 @@
import { NextRequest, NextResponse } from 'next/server';
import { uploadFileToS3, generateFileKey } from '@/lib/s3';
export async function POST(request: NextRequest) {
try {
const formData = await request.formData();
const file = formData.get('file') as File;
const folder = formData.get('folder') as string || 'audio';
if (!file) {
return NextResponse.json({ error: 'No file provided' }, { status: 400 });
}
// Validate file type (audio only)
const allowedTypes = [
'audio/mpeg',
'audio/mp3',
'audio/wav',
'audio/ogg',
'audio/flac',
'audio/aac',
'audio/m4a',
];
if (!allowedTypes.includes(file.type)) {
return NextResponse.json(
{ error: 'Invalid file type. Only audio files are allowed.' },
{ status: 400 }
);
}
// Validate file size (max 50MB for audio)
const maxSize = 50 * 1024 * 1024; // 50MB
if (file.size > maxSize) {
return NextResponse.json(
{ error: 'File too large. Maximum size is 50MB.' },
{ status: 400 }
);
}
// Generate unique key
const key = generateFileKey(folder, file.name);
// Upload to S3
const url = await uploadFileToS3({
file,
key,
contentType: file.type,
});
return NextResponse.json({ url, key }, { status: 200 });
} catch (error) {
console.error('Audio upload error:', error);
return NextResponse.json(
{ error: 'Failed to upload audio file' },
{ status: 500 }
);
}
}
+50
View File
@@ -0,0 +1,50 @@
import { NextRequest, NextResponse } from 'next/server';
import { uploadFileToS3, generateFileKey } from '@/lib/s3';
export async function POST(request: NextRequest) {
try {
const formData = await request.formData();
const file = formData.get('file') as File;
const folder = formData.get('folder') as string || 'uploads';
if (!file) {
return NextResponse.json({ error: 'No file provided' }, { status: 400 });
}
// Validate file type (images only)
const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp', 'image/gif'];
if (!allowedTypes.includes(file.type)) {
return NextResponse.json(
{ error: 'Invalid file type. Only images are allowed.' },
{ status: 400 }
);
}
// Validate file size (max 5MB)
const maxSize = 5 * 1024 * 1024; // 5MB
if (file.size > maxSize) {
return NextResponse.json(
{ error: 'File too large. Maximum size is 5MB.' },
{ status: 400 }
);
}
// Generate unique key
const key = generateFileKey(folder, file.name);
// Upload to S3
const url = await uploadFileToS3({
file,
key,
contentType: file.type,
});
return NextResponse.json({ url, key }, { status: 200 });
} catch (error) {
console.error('Upload error:', error);
return NextResponse.json(
{ error: 'Failed to upload file' },
{ status: 500 }
);
}
}