118 lines
2.6 KiB
TypeScript
118 lines
2.6 KiB
TypeScript
import { FastifyReply, FastifyRequest } from "fastify";
|
|
import {
|
|
CreateViewInput,
|
|
SetDefaultView,
|
|
UpdateViewInput,
|
|
} from "./view.schema";
|
|
import {
|
|
createView,
|
|
deleteView,
|
|
getDefaultViews,
|
|
getView,
|
|
listViews,
|
|
updateDefaultView,
|
|
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);
|
|
return res.code(200).send({ views: 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;
|
|
}
|
|
}
|
|
|
|
export async function getDefaultViewsHanlder(
|
|
req: FastifyRequest,
|
|
res: FastifyReply
|
|
) {
|
|
const { collection } = req.params as { collection: string };
|
|
try {
|
|
const recInDb = await getDefaultViews(collection, req.user);
|
|
return res.code(200).send(recInDb);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|
|
|
|
export async function updateDefaultViewsHanlder(
|
|
req: FastifyRequest,
|
|
res: FastifyReply
|
|
) {
|
|
const input = req.body as SetDefaultView;
|
|
|
|
try {
|
|
const recInDb = await updateDefaultView(input, req.user);
|
|
return res.code(200).send(recInDb?.defaultViews);
|
|
} catch (err) {
|
|
return err;
|
|
}
|
|
}
|