mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-12 15:52:39 +10:00
feat(collections): backend
This commit is contained in:
21
server/api/v1/client/game/manifest.get.ts
Normal file
21
server/api/v1/client/game/manifest.get.ts
Normal 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;
|
||||
});
|
||||
30
server/api/v1/client/game/version.get.ts
Normal file
30
server/api/v1/client/game/version.get.ts
Normal 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;
|
||||
});
|
||||
46
server/api/v1/client/game/versions.get.ts
Normal file
46
server/api/v1/client/game/versions.get.ts
Normal 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;
|
||||
});
|
||||
26
server/api/v1/collection/[id]/entry.delete.ts
Normal file
26
server/api/v1/collection/[id]/entry.delete.ts
Normal 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 {};
|
||||
});
|
||||
26
server/api/v1/collection/[id]/entry.post.ts
Normal file
26
server/api/v1/collection/[id]/entry.post.ts
Normal 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 {};
|
||||
});
|
||||
20
server/api/v1/collection/[id]/index.delete.ts
Normal file
20
server/api/v1/collection/[id]/index.delete.ts
Normal 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;
|
||||
});
|
||||
20
server/api/v1/collection/[id]/index.get.ts
Normal file
20
server/api/v1/collection/[id]/index.get.ts
Normal 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;
|
||||
});
|
||||
19
server/api/v1/collection/default/entry.delete.ts
Normal file
19
server/api/v1/collection/default/entry.delete.ts
Normal 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 {};
|
||||
});
|
||||
19
server/api/v1/collection/default/entry.post.ts
Normal file
19
server/api/v1/collection/default/entry.post.ts
Normal 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 {};
|
||||
});
|
||||
13
server/api/v1/collection/index.get.ts
Normal file
13
server/api/v1/collection/index.get.ts
Normal 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;
|
||||
});
|
||||
19
server/api/v1/collection/index.post.ts
Normal file
19
server/api/v1/collection/index.post.ts
Normal 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;
|
||||
});
|
||||
Reference in New Issue
Block a user