50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import type { Request, Response } from 'express';
|
|
import {
|
|
createVisit,
|
|
deleteVisit,
|
|
getVisit,
|
|
listVisits,
|
|
updateVisit
|
|
} from './visit.service';
|
|
import { visitInputSchema, visitUpdateSchema } from './visit.types';
|
|
import { toVisitDto } from './visit.mapper';
|
|
|
|
export async function listVisitsHandler(_req: Request, res: Response) {
|
|
const visits = await listVisits();
|
|
res.json(visits.map(toVisitDto));
|
|
}
|
|
|
|
export async function createVisitHandler(req: Request, res: Response) {
|
|
const parsed = visitInputSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ errors: parsed.error.flatten() });
|
|
}
|
|
const visit = await createVisit(parsed.data);
|
|
res.status(201).json(toVisitDto(visit));
|
|
}
|
|
|
|
export async function getVisitHandler(req: Request, res: Response) {
|
|
const visit = await getVisit(req.params.id);
|
|
if (!visit) {
|
|
return res.status(404).json({ message: 'Visit not found' });
|
|
}
|
|
res.json(toVisitDto(visit));
|
|
}
|
|
|
|
export async function updateVisitHandler(req: Request, res: Response) {
|
|
const parsed = visitUpdateSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ errors: parsed.error.flatten() });
|
|
}
|
|
const visit = await updateVisit(req.params.id, parsed.data);
|
|
if (!visit) {
|
|
return res.status(404).json({ message: 'Visit not found' });
|
|
}
|
|
res.json(toVisitDto(visit));
|
|
}
|
|
|
|
export async function deleteVisitHandler(req: Request, res: Response) {
|
|
await deleteVisit(req.params.id);
|
|
res.status(204).send();
|
|
}
|