Add health records API routes
This commit is contained in:
90
server/routes/health.js
Normal file
90
server/routes/health.js
Normal file
@@ -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;
|
||||
Reference in New Issue
Block a user