Add task routes, bug fixes
This commit is contained in:
84
src/task/task.controller.ts
Normal file
84
src/task/task.controller.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { CreateTaskInput, UpdateTaskInput } from "./task.schema";
|
||||
import {
|
||||
createTask,
|
||||
deleteTask,
|
||||
getTask,
|
||||
listTasks,
|
||||
updateTask,
|
||||
} from "./task.service";
|
||||
import { PageQueryParams } from "../pagination";
|
||||
|
||||
export async function createTaskHandler(
|
||||
req: FastifyRequest,
|
||||
res: FastifyReply
|
||||
) {
|
||||
const input = req.body as CreateTaskInput;
|
||||
|
||||
try {
|
||||
const rts = await createTask(input, req.user);
|
||||
return res.code(201).send(rts);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTaskHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const { taskId } = req.params as { taskId: string };
|
||||
|
||||
try {
|
||||
const task = await getTask(taskId, req.user.tenantId);
|
||||
if (task == null)
|
||||
return res.code(404).send({ error: "resource not found" });
|
||||
|
||||
return res.code(200).send(task);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function listTaskHandler(req: FastifyRequest, res: FastifyReply) {
|
||||
const queryParams = req.query as PageQueryParams;
|
||||
|
||||
try {
|
||||
const taskList = await listTasks(queryParams, req.user.tenantId);
|
||||
return res.code(200).send(taskList);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateTaskHandler(
|
||||
req: FastifyRequest,
|
||||
res: FastifyReply
|
||||
) {
|
||||
const input = req.body as UpdateTaskInput;
|
||||
const { taskId } = req.params as { taskId: string };
|
||||
|
||||
try {
|
||||
const updatedTask = await updateTask(taskId, input, req.user.tenantId);
|
||||
if (!updatedTask)
|
||||
return res.code(404).send({ error: "resource not found" });
|
||||
|
||||
return res.code(200).send(updatedTask);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteTaskHandler(
|
||||
req: FastifyRequest,
|
||||
res: FastifyReply
|
||||
) {
|
||||
const { taskId } = req.params as { taskId: string };
|
||||
|
||||
try {
|
||||
const deleteResult = await deleteTask(taskId, req.user.tenantId);
|
||||
if (deleteResult.deletedCount == 0)
|
||||
return res.code(404).send({ error: "resource not found" });
|
||||
|
||||
return res.code(204).send();
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user