From a2b39957fa35d00654a68c3b1a2a521e8473ff34 Mon Sep 17 00:00:00 2001 From: jason Date: Sun, 8 Mar 2026 22:45:54 -0500 Subject: [PATCH] Add health records API routes --- server/routes/health.js | 90 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 server/routes/health.js diff --git a/server/routes/health.js b/server/routes/health.js new file mode 100644 index 0000000..3080730 --- /dev/null +++ b/server/routes/health.js @@ -0,0 +1,90 @@ +const express = require('express'); +const router = express.Router(); +const { getDatabase } = require('../db/init'); + +// GET all health records for a dog +router.get('/dog/:dogId', (req, res) => { + try { + const db = getDatabase(); + const records = db.prepare(` + SELECT * FROM health_records + WHERE dog_id = ? + ORDER BY test_date DESC + `).all(req.params.dogId); + + res.json(records); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +// GET single health record +router.get('/:id', (req, res) => { + try { + const db = getDatabase(); + const record = db.prepare('SELECT * FROM health_records WHERE id = ?').get(req.params.id); + + if (!record) { + return res.status(404).json({ error: 'Health record not found' }); + } + + res.json(record); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +// POST create health record +router.post('/', (req, res) => { + try { + const { dog_id, record_type, test_name, test_date, result, document_url, notes } = req.body; + + if (!dog_id || !record_type || !test_date) { + return res.status(400).json({ error: 'Dog ID, record type, and test date are required' }); + } + + const db = getDatabase(); + const dbResult = db.prepare(` + INSERT INTO health_records (dog_id, record_type, test_name, test_date, result, document_url, notes) + VALUES (?, ?, ?, ?, ?, ?, ?) + `).run(dog_id, record_type, test_name, test_date, result, document_url, notes); + + const record = db.prepare('SELECT * FROM health_records WHERE id = ?').get(dbResult.lastInsertRowid); + + res.status(201).json(record); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +// PUT update health record +router.put('/:id', (req, res) => { + try { + const { record_type, test_name, test_date, result, document_url, notes } = req.body; + + const db = getDatabase(); + db.prepare(` + UPDATE health_records + SET record_type = ?, test_name = ?, test_date = ?, result = ?, document_url = ?, notes = ? + WHERE id = ? + `).run(record_type, test_name, test_date, result, document_url, notes, req.params.id); + + const record = db.prepare('SELECT * FROM health_records WHERE id = ?').get(req.params.id); + res.json(record); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +// DELETE health record +router.delete('/:id', (req, res) => { + try { + const db = getDatabase(); + db.prepare('DELETE FROM health_records WHERE id = ?').run(req.params.id); + res.json({ message: 'Health record deleted successfully' }); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +module.exports = router; \ No newline at end of file