feat(collections): backend

This commit is contained in:
DecDuck
2025-01-19 16:29:29 +11:00
parent 716eac79bf
commit a309651fe4
19 changed files with 870 additions and 0 deletions

View File

@ -0,0 +1,21 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import manifestGenerator from "~/server/internal/downloads/manifest";
export default defineClientEventHandler(async (h3) => {
const query = getQuery(h3);
const id = query.id?.toString();
const version = query.version?.toString();
if (!id || !version)
throw createError({
statusCode: 400,
statusMessage: "Missing id or version in query",
});
const manifest = await manifestGenerator.generateManifest(id, version);
if (!manifest)
throw createError({
statusCode: 400,
statusMessage: "Invalid game or version, or no versions added.",
});
return manifest;
});

View File

@ -0,0 +1,30 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import prisma from "~/server/internal/db/database";
export default defineClientEventHandler(async (h3) => {
const query = getQuery(h3);
const id = query.id?.toString();
const version = query.version?.toString();
if (!id || !version)
throw createError({
statusCode: 400,
statusMessage: "Missing id or version in query",
});
const gameVersion = await prisma.gameVersion.findUnique({
where: {
gameId_versionName: {
gameId: id,
versionName: version,
},
},
});
if (!gameVersion)
throw createError({
statusCode: 404,
statusMessage: "Game version not found",
});
return gameVersion;
});

View File

@ -0,0 +1,46 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import prisma from "~/server/internal/db/database";
import { DropManifest } from "~/server/internal/downloads/manifest";
export default defineClientEventHandler(async (h3, {}) => {
const query = getQuery(h3);
const id = query.id?.toString();
if (!id)
throw createError({
statusCode: 400,
statusMessage: "No ID in request query",
});
const versions = await prisma.gameVersion.findMany({
where: {
gameId: id,
},
orderBy: {
versionIndex: "desc", // Latest one first
},
});
const mappedVersions = versions
.map((version) => {
if (!version.dropletManifest) return undefined;
const manifest = JSON.parse(
version.dropletManifest.toString()
) as DropManifest;
/*
TODO: size estimates
They are a little complicated because of delta versions
Manifests need to be generated with the manifest generator and then
added up. I'm a little busy right now to implement this, though.
*/
const newVersion = { ...version, dropletManifest: undefined };
delete newVersion.dropletManifest;
return {
...newVersion,
};
})
.filter((e) => e);
return mappedVersions;
});

View File

@ -0,0 +1,26 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const id = getRouterParam(h3, "id");
if (!id)
throw createError({
statusCode: 400,
statusMessage: "ID required in route params",
});
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
await userLibraryManager.collectionRemove(id, gameId);
return {};
});

View File

@ -0,0 +1,26 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const id = getRouterParam(h3, "id");
if (!id)
throw createError({
statusCode: 400,
statusMessage: "ID required in route params",
});
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
await userLibraryManager.collectionAdd(id, gameId);
return {};
});

View File

@ -0,0 +1,20 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const id = getRouterParam(h3, "id");
if (!id)
throw createError({
statusCode: 400,
statusMessage: "ID required in route params",
});
const collection = await userLibraryManager.deleteCollection(id);
return collection;
});

View File

@ -0,0 +1,20 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const id = getRouterParam(h3, "id");
if (!id)
throw createError({
statusCode: 400,
statusMessage: "ID required in route params",
});
const collection = await userLibraryManager.fetchCollection(id);
return collection;
});

View File

@ -0,0 +1,19 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
await userLibraryManager.libraryRemove(gameId, userId);
return {};
});

View File

@ -0,0 +1,19 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
await userLibraryManager.libraryRemove(gameId, userId);
return {};
});

View File

@ -0,0 +1,13 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const collections = await userLibraryManager.fetchCollections(userId);
return collections;
});

View File

@ -0,0 +1,19 @@
import userLibraryManager from "~/server/internal/userlibrary";
export default defineEventHandler(async (h3) => {
const userId = await h3.context.session.getUserId(h3);
if (!userId)
throw createError({
statusCode: 403,
statusMessage: "Requires authentication",
});
const body = await readBody(h3);
const name = body.name;
if (!name)
throw createError({ statusCode: 400, statusMessage: "Requires name" });
const collections = await userLibraryManager.fetchCollections(userId);
return collections;
});