feat: scaffold frontend app structure

This commit is contained in:
Anton
2026-04-06 15:15:53 +03:00
parent 698ae37553
commit d7fb5b71ef
29 changed files with 2753 additions and 56 deletions

View File

@@ -4,26 +4,77 @@ import { rowToDto, bodyToColumns, RaceRow } from "../mappers/race";
const router = Router();
type ValidationErrorBody = {
error: "validation_error";
details: string[];
};
function dbError(res: Response) {
res.status(503).json({ error: "database_unavailable" });
}
function validationError(res: Response, details: string[]) {
const body: ValidationErrorBody = { error: "validation_error", details };
res.status(400).json(body);
}
function parseOptionalIntegerQuery(
value: unknown,
fieldName: string,
min?: number,
max?: number,
): { value?: number; error?: string } {
if (value == null) {
return {};
}
if (typeof value !== "string" || value.trim() === "") {
return { error: `${fieldName} must be an integer` };
}
const normalized = value.trim();
if (!/^-?\d+$/.test(normalized)) {
return { error: `${fieldName} must be an integer` };
}
const parsed = Number(normalized);
if (!Number.isInteger(parsed)) {
return { error: `${fieldName} must be an integer` };
}
if (min != null && parsed < min) {
return { error: `${fieldName} must be between ${min} and ${max}` };
}
if (max != null && parsed > max) {
return { error: `${fieldName} must be between ${min} and ${max}` };
}
return { value: parsed };
}
/* ─── GET /races ──────────────────────────────────────────── */
router.get("/races", async (req: Request, res: Response) => {
const yearResult = parseOptionalIntegerQuery(req.query.year, "year");
const monthResult = parseOptionalIntegerQuery(req.query.month, "month", 1, 12);
const details = [yearResult.error, monthResult.error].filter(Boolean) as string[];
if (details.length > 0) {
validationError(res, details);
return;
}
try {
const { year, month } = req.query;
const conditions: string[] = [];
const params: unknown[] = [];
let idx = 1;
if (year) {
if (yearResult.value != null) {
conditions.push(`EXTRACT(YEAR FROM race_date) = $${idx++}`);
params.push(Number(year));
params.push(yearResult.value);
}
if (month) {
if (monthResult.value != null) {
conditions.push(`EXTRACT(MONTH FROM race_date) = $${idx++}`);
params.push(Number(month));
params.push(monthResult.value);
}
const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
@@ -46,7 +97,7 @@ router.get("/races/:id", async (req: Request, res: Response) => {
[req.params.id],
);
if (rows.length === 0) {
res.status(404).json({ error: "not_found" });
res.status(404).json({ error: "not_found", details: ["Race not found"] });
return;
}
res.json(rowToDto(rows[0]));
@@ -62,10 +113,7 @@ router.post("/races", async (req: Request, res: Response) => {
const body = req.body;
if (!body.id || !body.date || !body.title || body.distanceKm == null) {
res.status(400).json({
error: "validation_error",
details: ["Fields id, date, title, distanceKm are required"],
});
validationError(res, ["Fields id, date, title, distanceKm are required"]);
return;
}
@@ -81,7 +129,10 @@ router.post("/races", async (req: Request, res: Response) => {
res.status(201).json(rowToDto(rows[0]));
} catch (err: any) {
if (err.code === "23505") {
res.status(409).json({ error: "conflict", details: ["Race with this id already exists"] });
res.status(409).json({
error: "conflict",
details: ["Race with this id already exists"],
});
return;
}
console.error("[POST /races]", err);
@@ -95,10 +146,7 @@ router.patch("/races/:id", async (req: Request, res: Response) => {
const { columns, values } = bodyToColumns(req.body);
if (columns.length === 0) {
res.status(400).json({
error: "validation_error",
details: ["No updatable fields provided"],
});
validationError(res, ["No updatable fields provided"]);
return;
}
@@ -110,7 +158,7 @@ router.patch("/races/:id", async (req: Request, res: Response) => {
try {
const { rows } = await pool.query<RaceRow>(sql, values);
if (rows.length === 0) {
res.status(404).json({ error: "not_found" });
res.status(404).json({ error: "not_found", details: ["Race not found"] });
return;
}
res.json(rowToDto(rows[0]));
@@ -129,7 +177,7 @@ router.delete("/races/:id", async (req: Request, res: Response) => {
[req.params.id],
);
if (rowCount === 0) {
res.status(404).json({ error: "not_found" });
res.status(404).json({ error: "not_found", details: ["Race not found"] });
return;
}
res.status(204).end();