Replace database with simple JSON file storage
This commit is contained in:
1 parent
e86d4d7d60
commit
491ac6628a
9 files changed
+208
-127
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getDb } from '@/lib/db'
|
||||
import { db } from '@/lib/db'
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
@@ -8,8 +8,7 @@ export async function DELETE(
|
||||
try {
|
||||
const { id } = await params
|
||||
|
||||
const db = getDb()
|
||||
db.prepare('DELETE FROM playlists WHERE id = ?').run(id)
|
||||
db.deletePlaylist(id)
|
||||
|
||||
console.log('[API] Playlist deleted:', id)
|
||||
return NextResponse.json({ success: true })
|
||||
@@ -29,8 +28,7 @@ export async function GET(
|
||||
try {
|
||||
const { id } = await params
|
||||
|
||||
const db = getDb()
|
||||
const playlist = db.prepare('SELECT * FROM playlists WHERE id = ?').get(id) as any
|
||||
const playlist = db.getPlaylistById(id)
|
||||
|
||||
if (!playlist) {
|
||||
return NextResponse.json(
|
||||
@@ -40,16 +38,13 @@ export async function GET(
|
||||
}
|
||||
|
||||
// Get songs for this playlist
|
||||
const songs = db.prepare(`
|
||||
SELECT s.*, ps.addedAt FROM songs s
|
||||
INNER JOIN playlistSongs ps ON s.id = ps.songId
|
||||
WHERE ps.playlistId = ?
|
||||
ORDER BY ps.addedAt ASC
|
||||
`).all(id)
|
||||
const playlistSongs = db.getPlaylistSongs(id)
|
||||
const playlistWithSongs = {
|
||||
...playlist,
|
||||
playlistSongs
|
||||
}
|
||||
|
||||
playlist.playlistSongs = songs.map((song: any) => ({ song }))
|
||||
|
||||
return NextResponse.json(playlist)
|
||||
return NextResponse.json(playlistWithSongs)
|
||||
} catch (error) {
|
||||
console.error('[API] Error fetching playlist:', error)
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getDb } from '@/lib/db'
|
||||
import { db } from '@/lib/db'
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
@@ -8,8 +8,7 @@ export async function DELETE(
|
||||
try {
|
||||
const { id, songId } = await params
|
||||
|
||||
const db = getDb()
|
||||
db.prepare('DELETE FROM playlistSongs WHERE playlistId = ? AND songId = ?').run(id, songId)
|
||||
db.removeSongFromPlaylist(id, songId)
|
||||
|
||||
console.log('[API] Song removed from playlist')
|
||||
return NextResponse.json({ success: true })
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getDb } from '@/lib/db'
|
||||
import { randomBytes } from 'crypto'
|
||||
import { db } from '@/lib/db'
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
@@ -17,30 +16,16 @@ export async function POST(
|
||||
)
|
||||
}
|
||||
|
||||
// Check if already in playlist
|
||||
const db = getDb()
|
||||
const existing = db.prepare('SELECT * FROM playlistSongs WHERE playlistId = ? AND songId = ?').get(id, songId)
|
||||
const playlistSong = db.addSongToPlaylist(id, songId)
|
||||
|
||||
if (existing) {
|
||||
if (!playlistSong) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Song already in playlist' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Get next position
|
||||
const maxPosition = db.prepare('SELECT MAX(position) as max FROM playlistSongs WHERE playlistId = ?').get(id) as any
|
||||
const position = (maxPosition?.max ?? -1) + 1
|
||||
|
||||
const playlistSongId = randomBytes(16).toString('hex')
|
||||
db.prepare(`
|
||||
INSERT INTO playlistSongs (id, playlistId, songId, position)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`).run(playlistSongId, id, songId, position)
|
||||
|
||||
const playlistSong = db.prepare('SELECT * FROM playlistSongs WHERE id = ?').get(playlistSongId)
|
||||
|
||||
console.log('[API] Song added to playlist:', playlistSongId)
|
||||
console.log('[API] Song added to playlist:', playlistSong.id)
|
||||
return NextResponse.json(playlistSong)
|
||||
} catch (error) {
|
||||
console.error('[API] Error adding song to playlist:', error)
|
||||
|
||||
@@ -1,24 +1,16 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getDb } from '@/lib/db'
|
||||
import { randomBytes } from 'crypto'
|
||||
import { db } from '@/lib/db'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const db = getDb()
|
||||
const playlists = db.prepare('SELECT * FROM playlists ORDER BY createdAt DESC').all() as any[]
|
||||
const playlists = db.getPlaylists()
|
||||
|
||||
// Get songs for each playlist
|
||||
const playlistsWithSongs = playlists.map(playlist => {
|
||||
const songs = db.prepare(`
|
||||
SELECT s.* FROM songs s
|
||||
INNER JOIN playlistSongs ps ON s.id = ps.songId
|
||||
WHERE ps.playlistId = ?
|
||||
ORDER BY ps.position
|
||||
`).all(playlist.id)
|
||||
|
||||
const playlistSongs = db.getPlaylistSongs(playlist.id)
|
||||
return {
|
||||
...playlist,
|
||||
playlistSongs: songs.map((song: any) => ({ song }))
|
||||
playlistSongs
|
||||
}
|
||||
})
|
||||
|
||||
@@ -43,13 +35,9 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
const db = getDb()
|
||||
const playlistId = randomBytes(16).toString('hex')
|
||||
db.prepare('INSERT INTO playlists (id, name) VALUES (?, ?)').run(playlistId, name)
|
||||
|
||||
const playlist = db.prepare('SELECT * FROM playlists WHERE id = ?').get(playlistId)
|
||||
const playlist = db.createPlaylist(name)
|
||||
|
||||
console.log('[API] Playlist created:', playlistId)
|
||||
console.log('[API] Playlist created:', playlist.id)
|
||||
return NextResponse.json(playlist)
|
||||
} catch (error) {
|
||||
console.error('[API] Error creating playlist:', error)
|
||||
|
||||
Reference in new issue
Block a user