brawlset/app/app/api/account/decks/delete/route.ts
2024-12-27 16:41:39 +01:00

55 lines
1.2 KiB
TypeScript

import { NextResponse, NextRequest } from 'next/server'
import { validateToken } from '@/lib/jwt'
import { db } from "@/lib/db"
// TODO: Check if the user is the owner of the deck or an admin.
export async function DELETE(req: NextRequest) {
try {
const token = req?.headers.get("authorization")?.split(" ")[1]
const { id } = await req.json()
if(token == undefined) {
return NextResponse.json({"message": "You did not provide a token."},{
status: 401,
});
}
if(!validateToken(token)) {
return NextResponse.json({"message": "Your token is not valid."},{
status: 401,
});
}
if(id == undefined) {
return NextResponse.json({"message": "You did not provide an ID."},{
status: 401,
});
}
await db.cartes_dans_deck.deleteMany({
where: {
deck_id: id
}
})
await db.deck.delete({
where: {
id
}
})
return NextResponse.json({"message": "Deck successfully deleted."},{
status: 200,
});
} catch (error) {
console.log(error)
return NextResponse.json(
{ error: "Failed, check console" },
{
status: 500,
}
);
}
}