|
| 1 | +import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify' |
| 2 | +import { |
| 3 | + createRiver, |
| 4 | + deleteRiver, |
| 5 | + getRiverById, |
| 6 | + getRiverByStationId, |
| 7 | + getRivers, |
| 8 | + updateRiver, |
| 9 | +} from '../models/river' |
| 10 | +import { runRiverLevelCheck } from '../tasks/river-levels' |
| 11 | + |
| 12 | +const nullableNumber = { type: ['number', 'null'] } |
| 13 | + |
| 14 | +const createBodySchema = { |
| 15 | + type: 'object', |
| 16 | + required: ['station_id', 'river_name', 'station_name'], |
| 17 | + additionalProperties: false, |
| 18 | + properties: { |
| 19 | + station_id: { type: 'string', minLength: 1, maxLength: 64 }, |
| 20 | + river_name: { type: 'string', minLength: 1, maxLength: 128 }, |
| 21 | + station_name: { type: 'string', minLength: 1, maxLength: 128 }, |
| 22 | + soglia1: nullableNumber, |
| 23 | + soglia2: nullableNumber, |
| 24 | + soglia3: nullableNumber, |
| 25 | + }, |
| 26 | +} |
| 27 | + |
| 28 | +const patchBodySchema = { |
| 29 | + type: 'object', |
| 30 | + additionalProperties: false, |
| 31 | + minProperties: 1, |
| 32 | + properties: { |
| 33 | + river_name: { type: 'string', minLength: 1, maxLength: 128 }, |
| 34 | + station_name: { type: 'string', minLength: 1, maxLength: 128 }, |
| 35 | + soglia1: nullableNumber, |
| 36 | + soglia2: nullableNumber, |
| 37 | + soglia3: nullableNumber, |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +const idParamSchema = { |
| 42 | + type: 'object', |
| 43 | + required: ['id'], |
| 44 | + additionalProperties: false, |
| 45 | + properties: { |
| 46 | + id: { type: 'integer', minimum: 1 }, |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +interface IdParams { |
| 51 | + id: number |
| 52 | +} |
| 53 | + |
| 54 | +interface CreateRiverBody { |
| 55 | + station_id: string |
| 56 | + river_name: string |
| 57 | + station_name: string |
| 58 | + soglia1?: number | null |
| 59 | + soglia2?: number | null |
| 60 | + soglia3?: number | null |
| 61 | +} |
| 62 | + |
| 63 | +interface PatchRiverBody { |
| 64 | + river_name?: string |
| 65 | + station_name?: string |
| 66 | + soglia1?: number | null |
| 67 | + soglia2?: number | null |
| 68 | + soglia3?: number | null |
| 69 | +} |
| 70 | + |
| 71 | +export const registerRiversRoutes = (fastify: FastifyInstance) => { |
| 72 | + fastify.route({ |
| 73 | + method: 'GET', |
| 74 | + url: '/rivers', |
| 75 | + handler: async (_request: FastifyRequest, reply: FastifyReply) => { |
| 76 | + const rivers = await getRivers() |
| 77 | + reply.status(200).send(rivers) |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + fastify.route<{ Body: CreateRiverBody }>({ |
| 82 | + method: 'POST', |
| 83 | + url: '/rivers', |
| 84 | + schema: { body: createBodySchema }, |
| 85 | + handler: async (request, reply) => { |
| 86 | + const existing = await getRiverByStationId(request.body.station_id) |
| 87 | + |
| 88 | + if (existing) { |
| 89 | + return reply.status(409).send({ error: 'Station already registered' }) |
| 90 | + } |
| 91 | + |
| 92 | + const created = await createRiver({ |
| 93 | + station_id: request.body.station_id, |
| 94 | + river_name: request.body.river_name, |
| 95 | + station_name: request.body.station_name, |
| 96 | + soglia1: request.body.soglia1 ?? null, |
| 97 | + soglia2: request.body.soglia2 ?? null, |
| 98 | + soglia3: request.body.soglia3 ?? null, |
| 99 | + }) |
| 100 | + |
| 101 | + reply.status(201).send(created) |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + fastify.route<{ Params: IdParams; Body: PatchRiverBody }>({ |
| 106 | + method: 'PATCH', |
| 107 | + url: '/rivers/:id', |
| 108 | + schema: { params: idParamSchema, body: patchBodySchema }, |
| 109 | + handler: async (request, reply) => { |
| 110 | + const river = await getRiverById(request.params.id) |
| 111 | + |
| 112 | + if (!river) { |
| 113 | + return reply.status(404).send({ error: 'River not found' }) |
| 114 | + } |
| 115 | + |
| 116 | + const updated = await updateRiver(request.params.id, request.body) |
| 117 | + reply.status(200).send(updated) |
| 118 | + }, |
| 119 | + }) |
| 120 | + |
| 121 | + fastify.route<{ Params: IdParams }>({ |
| 122 | + method: 'DELETE', |
| 123 | + url: '/rivers/:id', |
| 124 | + schema: { params: idParamSchema }, |
| 125 | + handler: async (request, reply) => { |
| 126 | + const river = await getRiverById(request.params.id) |
| 127 | + |
| 128 | + if (!river) { |
| 129 | + return reply.status(404).send({ error: 'River not found' }) |
| 130 | + } |
| 131 | + |
| 132 | + await deleteRiver(request.params.id) |
| 133 | + reply.status(204).send(undefined) |
| 134 | + }, |
| 135 | + }) |
| 136 | + |
| 137 | + fastify.route({ |
| 138 | + method: 'POST', |
| 139 | + url: '/river-levels', |
| 140 | + schema: { |
| 141 | + body: { type: 'object', additionalProperties: false, maxProperties: 0 }, |
| 142 | + }, |
| 143 | + handler: async (_request: FastifyRequest, reply: FastifyReply) => { |
| 144 | + const summary = await runRiverLevelCheck() |
| 145 | + reply.status(200).send(summary) |
| 146 | + }, |
| 147 | + }) |
| 148 | +} |
0 commit comments