add view routes

This commit is contained in:
2025-03-04 11:35:52 +05:30
parent da5e67e0cd
commit a8d336ca4e
6 changed files with 239 additions and 6 deletions

View File

@@ -0,0 +1,84 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { CreateViewInput, UpdateViewInput } from "./view.schema";
import {
createView,
deleteView,
getView,
listViews,
updateView,
} from "./view.service";
import { PageQueryParams } from "../pagination";
export async function createViewHandler(
req: FastifyRequest,
res: FastifyReply
) {
const input = req.body as CreateViewInput;
try {
const view = await createView(input, req.user);
return res.code(201).send(view);
} catch (err) {
return err;
}
}
export async function getViewHandler(req: FastifyRequest, res: FastifyReply) {
const { viewId } = req.params as { viewId: string };
try {
const view = await getView(viewId, req.user.tenantId);
if (view === null)
return res.code(404).send({ error: "resource not found" });
return res.code(200).send(view);
} catch (err) {
return err;
}
}
export async function listViewHandler(req: FastifyRequest, res: FastifyReply) {
const params = req.query as PageQueryParams;
try {
const views = await listViews(params, req.user.tenantId);
return res.code(200).send(views);
} catch (err) {
return err;
}
}
export async function updateViewHandler(
req: FastifyRequest,
res: FastifyReply
) {
const input = req.body as UpdateViewInput;
const { viewId } = req.params as { viewId: string };
try {
const updatedView = await updateView(viewId, input, req.user.tenantId);
if (!updatedView)
return res.code(404).send({ error: "resource not found" });
return res.code(200).send(updateView);
} catch (err) {
return err;
}
}
export async function deleteViewHandler(
req: FastifyRequest,
res: FastifyReply
) {
const { viewId } = req.params as { viewId: string };
try {
const deleteResult = await deleteView(viewId);
if (deleteResult.deletedCount == 0)
return res.code(404).send({ error: "resource not found" });
return res.code(204).send();
} catch (err) {
return err;
}
}