mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-13 00:02:37 +10:00
feat: add news client routes
This commit is contained in:
20
server/api/v1/client/news/[id]/index.get.ts
Normal file
20
server/api/v1/client/news/[id]/index.get.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
|
||||||
|
import newsManager from "~/server/internal/news";
|
||||||
|
|
||||||
|
export default defineClientEventHandler(async (h3) => {
|
||||||
|
const id = h3.context.params?.id;
|
||||||
|
if (!id)
|
||||||
|
throw createError({
|
||||||
|
statusCode: 400,
|
||||||
|
message: "Missing news ID",
|
||||||
|
});
|
||||||
|
|
||||||
|
const news = await newsManager.fetchById(id);
|
||||||
|
if (!news)
|
||||||
|
throw createError({
|
||||||
|
statusCode: 404,
|
||||||
|
message: "News article not found",
|
||||||
|
});
|
||||||
|
|
||||||
|
return news;
|
||||||
|
});
|
||||||
29
server/api/v1/client/news/index.get.ts
Normal file
29
server/api/v1/client/news/index.get.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
|
||||||
|
import newsManager from "~/server/internal/news";
|
||||||
|
|
||||||
|
export default defineClientEventHandler(async (h3) => {
|
||||||
|
const query = getQuery(h3);
|
||||||
|
|
||||||
|
const orderBy = query.order as "asc" | "desc";
|
||||||
|
if (orderBy) {
|
||||||
|
if (typeof orderBy !== "string" || !["asc", "desc"].includes(orderBy))
|
||||||
|
throw createError({ statusCode: 400, statusMessage: "Invalid order" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const tags = query.tags as string[] | undefined;
|
||||||
|
if (tags) {
|
||||||
|
if (typeof tags !== "object" || !Array.isArray(tags))
|
||||||
|
throw createError({ statusCode: 400, statusMessage: "Invalid tags" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
take: parseInt(query.limit as string),
|
||||||
|
skip: parseInt(query.skip as string),
|
||||||
|
orderBy: orderBy,
|
||||||
|
tags: tags?.map((e) => e.toString()),
|
||||||
|
search: query.search as string,
|
||||||
|
};
|
||||||
|
|
||||||
|
const news = await newsManager.fetch(options);
|
||||||
|
return news;
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user