This commit is contained in:
h
2026-04-06 15:36:51 +09:00
parent 1081ea1074
commit 2e9e100178
10 changed files with 216 additions and 20 deletions

View File

@@ -64,6 +64,7 @@ if (!carCols.includes('has_etc')) {
if (!carCols.includes('tire_type')) {
db.exec("ALTER TABLE cars ADD COLUMN tire_type TEXT DEFAULT 'ノーマル'");
}
db.prepare("UPDATE cars SET tire_type = 'スタッドレス' WHERE tire_type = 'スタットレス'").run();
// Seed some initial cars if none exist
const carCount = db.prepare('SELECT COUNT(*) as cnt FROM cars').get();
@@ -86,13 +87,27 @@ function broadcast(message) {
});
}
function normalizeTireType(value) {
return value === 'スタットレス' ? 'スタッドレス' : value;
}
function normalizeCar(car) {
if (!car) {
return car;
}
return {
...car,
tire_type: normalizeTireType(car.tire_type),
};
}
wss.on('connection', (ws) => {
ws.on('error', () => {}); // suppress unhandled error events
});
// --- Cars API ---
app.get('/api/cars', (req, res) => {
const cars = db.prepare('SELECT * FROM cars ORDER BY id').all();
const cars = db.prepare('SELECT * FROM cars ORDER BY id').all().map(normalizeCar);
res.json(cars);
});
@@ -104,7 +119,7 @@ app.post('/api/cars', (req, res) => {
const result = db.prepare(
'INSERT INTO cars (name, description, inspection_expiry, has_etc, tire_type) VALUES (?, ?, ?, ?, ?)'
).run(name.trim(), description, inspection_expiry, has_etc ? 1 : 0, tire_type);
const car = db.prepare('SELECT * FROM cars WHERE id = ?').get(result.lastInsertRowid);
const car = normalizeCar(db.prepare('SELECT * FROM cars WHERE id = ?').get(result.lastInsertRowid));
broadcast({ type: 'data_changed', entity: 'cars' });
res.status(201).json(car);
});
@@ -120,7 +135,7 @@ app.put('/api/cars/:id', (req, res) => {
if (result.changes === 0) {
return res.status(404).json({ error: '車が見つかりません' });
}
const car = db.prepare('SELECT * FROM cars WHERE id = ?').get(req.params.id);
const car = normalizeCar(db.prepare('SELECT * FROM cars WHERE id = ?').get(req.params.id));
broadcast({ type: 'data_changed', entity: 'cars' });
res.json(car);
});