feat(admin ui): customisable image carousel and new layout

This commit is contained in:
DecDuck
2024-12-27 14:43:40 +11:00
parent 43e32b44a2
commit 937954fa02
15 changed files with 853 additions and 194 deletions
@@ -0,0 +1,62 @@
import prisma from "~/server/internal/db/database";
export default defineEventHandler(async (h3) => {
const user = await h3.context.session.getAdminUser(h3);
if (!user) throw createError({ statusCode: 403 });
const body = await readBody(h3);
const gameId = body.gameId;
const imageId = body.imageId;
if (!gameId || !imageId)
throw createError({
statusCode: 400,
statusMessage: "Missing gameId or imageId in body",
});
const game = await prisma.game.findUnique({
where: {
id: gameId,
},
select: {
mBannerId: true,
mImageLibrary: true,
mCoverId: true,
},
});
if (!game)
throw createError({ statusCode: 400, statusMessage: "Invalid game ID" });
const imageIndex = game.mImageLibrary.findIndex((e) => e == imageId);
if (imageIndex == -1)
throw createError({ statusCode: 400, statusMessage: "Image not found" });
game.mImageLibrary.splice(imageIndex, 1);
await h3.context.objects.delete(imageId);
if (game.mBannerId === imageId) {
game.mBannerId = game.mImageLibrary[0];
}
if (game.mCoverId === imageId) {
game.mCoverId = game.mImageLibrary[0];
}
const result = await prisma.game.update({
where: {
id: gameId,
},
data: {
mBannerId: game.mBannerId,
mImageLibrary: game.mImageLibrary,
mCoverId: game.mCoverId,
},
select: {
mBannerId: true,
mImageLibrary: true,
mCoverId: true,
},
});
return result;
});
@@ -0,0 +1,57 @@
import prisma from "~/server/internal/db/database";
import { handleFileUpload } from "~/server/internal/utils/handlefileupload";
export default defineEventHandler(async (h3) => {
const user = await h3.context.session.getAdminUser(h3);
if (!user) throw createError({ statusCode: 403 });
const form = await readMultipartFormData(h3);
if (!form)
throw createError({
statusCode: 400,
statusMessage: "This endpoint requires multipart form data.",
});
const uploadResult = await handleFileUpload(h3, {}, ["internal:read"]);
if (!uploadResult)
throw createError({
statusCode: 400,
statusMessage: "Failed to upload file",
});
const [id, options, pull, dump] = uploadResult;
if (!id) {
dump();
throw createError({
statusCode: 400,
statusMessage: "Did not upload a file",
});
}
const gameId = options.id;
if (!gameId)
throw createError({
statusCode: 400,
statusMessage: "No game ID attached",
});
const hasGame = (await prisma.game.count({ where: { id: gameId } })) != 0;
if (!hasGame) {
dump();
throw createError({ statusCode: 400, statusMessage: "Invalid game ID" });
}
const result = await prisma.game.update({
where: {
id: gameId,
},
data: {
mImageLibrary: {
push: id,
},
},
});
await pull();
return result;
});