mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-20 19:51:09 +10:00
partial: user routes
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
import aclManager from "~/server/internal/acls";
|
||||
import prisma from "~/server/internal/db/database";
|
||||
|
||||
/**
|
||||
* Fetch all featured games. Used for store carousel.
|
||||
*/
|
||||
export default defineEventHandler(async (h3) => {
|
||||
const userId = await aclManager.getUserACL(h3, ["store:read"]);
|
||||
if (!userId) throw createError({ statusCode: 403 });
|
||||
|
||||
@ -21,102 +21,107 @@ const StoreRead = type({
|
||||
sort: "'default' | 'newest' | 'recent' = 'default'",
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (h3) => {
|
||||
const userId = await aclManager.getUserIdACL(h3, ["store:read"]);
|
||||
if (!userId) throw createError({ statusCode: 403 });
|
||||
/**
|
||||
* Store endpoint. Filter games with pagination. Used for all "store views".
|
||||
*/
|
||||
export default defineEventHandler<{ query: typeof StoreRead.infer }>(
|
||||
async (h3) => {
|
||||
const userId = await aclManager.getUserIdACL(h3, ["store:read"]);
|
||||
if (!userId) throw createError({ statusCode: 403 });
|
||||
|
||||
const query = getQuery(h3);
|
||||
const options = StoreRead(query);
|
||||
if (options instanceof ArkErrors)
|
||||
throw createError({ statusCode: 400, statusMessage: options.summary });
|
||||
const query = getQuery(h3);
|
||||
const options = StoreRead(query);
|
||||
if (options instanceof ArkErrors)
|
||||
throw createError({ statusCode: 400, statusMessage: options.summary });
|
||||
|
||||
/**
|
||||
* Generic filters
|
||||
*/
|
||||
const tagFilter = options.tags
|
||||
? {
|
||||
tags: {
|
||||
some: {
|
||||
id: {
|
||||
in: options.tags.split(","),
|
||||
/**
|
||||
* Generic filters
|
||||
*/
|
||||
const tagFilter = options.tags
|
||||
? {
|
||||
tags: {
|
||||
some: {
|
||||
id: {
|
||||
in: options.tags.split(","),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
const platformFilter = options.platform
|
||||
? {
|
||||
versions: {
|
||||
some: {
|
||||
platform: {
|
||||
in: options.platform
|
||||
.split(",")
|
||||
.map(parsePlatform)
|
||||
.filter((e) => e !== undefined),
|
||||
}
|
||||
: undefined;
|
||||
const platformFilter = options.platform
|
||||
? {
|
||||
versions: {
|
||||
some: {
|
||||
platform: {
|
||||
in: options.platform
|
||||
.split(",")
|
||||
.map(parsePlatform)
|
||||
.filter((e) => e !== undefined),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
}
|
||||
: undefined;
|
||||
|
||||
/**
|
||||
* Company filtering
|
||||
*/
|
||||
const companyActions = options.companyActions.split(",");
|
||||
const developedFilter = companyActions.includes("developed")
|
||||
? {
|
||||
developers: {
|
||||
some: {
|
||||
id: options.company!,
|
||||
/**
|
||||
* Company filtering
|
||||
*/
|
||||
const companyActions = options.companyActions.split(",");
|
||||
const developedFilter = companyActions.includes("developed")
|
||||
? {
|
||||
developers: {
|
||||
some: {
|
||||
id: options.company!,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
const publishedFilter = companyActions.includes("published")
|
||||
? {
|
||||
publishers: {
|
||||
some: {
|
||||
id: options.company!,
|
||||
}
|
||||
: undefined;
|
||||
const publishedFilter = companyActions.includes("published")
|
||||
? {
|
||||
publishers: {
|
||||
some: {
|
||||
id: options.company!,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
const companyFilter = options.company
|
||||
? ({
|
||||
OR: [developedFilter, publishedFilter].filter((e) => e !== undefined),
|
||||
} satisfies Prisma.GameWhereInput)
|
||||
: undefined;
|
||||
}
|
||||
: undefined;
|
||||
const companyFilter = options.company
|
||||
? ({
|
||||
OR: [developedFilter, publishedFilter].filter((e) => e !== undefined),
|
||||
} satisfies Prisma.GameWhereInput)
|
||||
: undefined;
|
||||
|
||||
/**
|
||||
* Query
|
||||
*/
|
||||
/**
|
||||
* Query
|
||||
*/
|
||||
|
||||
const finalFilter: Prisma.GameWhereInput = {
|
||||
...tagFilter,
|
||||
...platformFilter,
|
||||
...companyFilter,
|
||||
};
|
||||
const finalFilter: Prisma.GameWhereInput = {
|
||||
...tagFilter,
|
||||
...platformFilter,
|
||||
...companyFilter,
|
||||
};
|
||||
|
||||
const sort: Prisma.GameOrderByWithRelationInput = {};
|
||||
switch (options.sort) {
|
||||
case "default":
|
||||
case "newest":
|
||||
sort.mReleased = "desc";
|
||||
break;
|
||||
case "recent":
|
||||
sort.created = "desc";
|
||||
break;
|
||||
}
|
||||
const sort: Prisma.GameOrderByWithRelationInput = {};
|
||||
switch (options.sort) {
|
||||
case "default":
|
||||
case "newest":
|
||||
sort.mReleased = "desc";
|
||||
break;
|
||||
case "recent":
|
||||
sort.created = "desc";
|
||||
break;
|
||||
}
|
||||
|
||||
const [results, count] = await prisma.$transaction([
|
||||
prisma.game.findMany({
|
||||
skip: options.skip,
|
||||
take: Math.min(options.take, 50),
|
||||
where: finalFilter,
|
||||
orderBy: sort,
|
||||
}),
|
||||
prisma.game.count({ where: finalFilter }),
|
||||
]);
|
||||
const [results, count] = await prisma.$transaction([
|
||||
prisma.game.findMany({
|
||||
skip: options.skip,
|
||||
take: Math.min(options.take, 50),
|
||||
where: finalFilter,
|
||||
orderBy: sort,
|
||||
}),
|
||||
prisma.game.count({ where: finalFilter }),
|
||||
]);
|
||||
|
||||
return { results, count };
|
||||
});
|
||||
return { results, count };
|
||||
},
|
||||
);
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
import aclManager from "~/server/internal/acls";
|
||||
import prisma from "~/server/internal/db/database";
|
||||
|
||||
/**
|
||||
* Fetch all game tags.
|
||||
*/
|
||||
export default defineEventHandler(async (h3) => {
|
||||
const userId = await aclManager.getUserIdACL(h3, ["store:read"]);
|
||||
if (!userId) throw createError({ statusCode: 403 });
|
||||
|
||||
Reference in New Issue
Block a user