feat: add inspection_expiry/has_etc/tire_type fields, icons in schedule view, and WebSocket real-time sync

Co-authored-by: pdf114514 <57948770+pdf114514@users.noreply.github.com>
Agent-Logs-Url: https://github.com/pdf114514/CarReservation/sessions/6d0f25ae-6db4-4937-ae2b-6674456a5ca1
This commit is contained in:
copilot-swe-agent[bot]
2026-03-22 03:58:07 +00:00
parent 09872737b7
commit 8e9db971d3
13 changed files with 451 additions and 26 deletions

View File

@@ -12,7 +12,8 @@
"better-sqlite3": "^12.8.0",
"cors": "^2.8.6",
"express": "^5.2.1",
"express-rate-limit": "^8.3.1"
"express-rate-limit": "^8.3.1",
"ws": "^8.20.0"
}
},
"node_modules/accepts": {
@@ -1309,6 +1310,27 @@
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"license": "ISC"
},
"node_modules/ws": {
"version": "8.20.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.20.0.tgz",
"integrity": "sha512-sAt8BhgNbzCtgGbt2OxmpuryO63ZoDk/sqaB/znQm94T4fCEsy/yV+7CdC1kJhOU9lboAEU7R3kquuycDoibVA==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}

View File

@@ -16,6 +16,7 @@
"better-sqlite3": "^12.8.0",
"cors": "^2.8.6",
"express": "^5.2.1",
"express-rate-limit": "^8.3.1"
"express-rate-limit": "^8.3.1",
"ws": "^8.20.0"
}
}

View File

@@ -3,9 +3,12 @@ const cors = require('cors');
const rateLimit = require('express-rate-limit');
const Database = require('better-sqlite3');
const path = require('path');
const http = require('http');
const { WebSocketServer } = require('ws');
const app = express();
const PORT = process.env.PORT || 3001;
const server = http.createServer(app);
// Trust the first proxy (nginx) so that express-rate-limit can correctly
// identify clients by their real IP from the X-Forwarded-For header.
@@ -14,6 +17,10 @@ app.set('trust proxy', 1);
app.use(cors());
app.use(express.json());
// Serve the built frontend (production mode)
const distPath = path.join(__dirname, '..', 'frontend', 'dist');
app.use(express.static(distPath));
// Apply rate limiting to all API routes
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
@@ -46,6 +53,18 @@ db.exec(`
);
`);
// Migrate: add new car fields if they don't exist yet
const carCols = db.prepare("PRAGMA table_info(cars)").all().map((c) => c.name);
if (!carCols.includes('inspection_expiry')) {
db.exec("ALTER TABLE cars ADD COLUMN inspection_expiry TEXT DEFAULT ''");
}
if (!carCols.includes('has_etc')) {
db.exec('ALTER TABLE cars ADD COLUMN has_etc INTEGER DEFAULT 0');
}
if (!carCols.includes('tire_type')) {
db.exec("ALTER TABLE cars ADD COLUMN tire_type TEXT DEFAULT 'ノーマル'");
}
// Seed some initial cars if none exist
const carCount = db.prepare('SELECT COUNT(*) as cnt FROM cars').get();
if (carCount.cnt === 0) {
@@ -55,6 +74,22 @@ if (carCount.cnt === 0) {
insertCar.run('代車 C', '');
}
// --- WebSocket Server ---
const wss = new WebSocketServer({ server, path: '/ws' });
function broadcast(message) {
const data = JSON.stringify(message);
wss.clients.forEach((client) => {
if (client.readyState === client.OPEN) {
client.send(data);
}
});
}
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();
@@ -62,25 +97,31 @@ app.get('/api/cars', (req, res) => {
});
app.post('/api/cars', (req, res) => {
const { name, description = '' } = req.body;
const { name, description = '', inspection_expiry = '', has_etc = 0, tire_type = 'ノーマル' } = req.body;
if (!name || !name.trim()) {
return res.status(400).json({ error: '車名は必須です' });
}
const result = db.prepare('INSERT INTO cars (name, description) VALUES (?, ?)').run(name.trim(), description);
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);
broadcast({ type: 'data_changed', entity: 'cars' });
res.status(201).json(car);
});
app.put('/api/cars/:id', (req, res) => {
const { name, description } = req.body;
const { name, description, inspection_expiry, has_etc, tire_type } = req.body;
if (!name || !name.trim()) {
return res.status(400).json({ error: '車名は必須です' });
}
const result = db.prepare('UPDATE cars SET name = ?, description = ? WHERE id = ?').run(name.trim(), description ?? '', req.params.id);
const result = db.prepare(
'UPDATE cars SET name = ?, description = ?, inspection_expiry = ?, has_etc = ?, tire_type = ? WHERE id = ?'
).run(name.trim(), description ?? '', inspection_expiry ?? '', has_etc ? 1 : 0, tire_type ?? 'ノーマル', req.params.id);
if (result.changes === 0) {
return res.status(404).json({ error: '車が見つかりません' });
}
const car = db.prepare('SELECT * FROM cars WHERE id = ?').get(req.params.id);
broadcast({ type: 'data_changed', entity: 'cars' });
res.json(car);
});
@@ -89,6 +130,7 @@ app.delete('/api/cars/:id', (req, res) => {
if (result.changes === 0) {
return res.status(404).json({ error: '車が見つかりません' });
}
broadcast({ type: 'data_changed', entity: 'all' });
res.json({ success: true });
});
@@ -110,6 +152,7 @@ app.post('/api/reservations', (req, res) => {
'INSERT INTO reservations (car_id, start_date, end_date, customer_name, notes) VALUES (?, ?, ?, ?, ?)'
).run(car_id, start_date, end_date, customer_name, notes);
const reservation = db.prepare('SELECT * FROM reservations WHERE id = ?').get(result.lastInsertRowid);
broadcast({ type: 'data_changed', entity: 'reservations' });
res.status(201).json(reservation);
});
@@ -128,6 +171,7 @@ app.put('/api/reservations/:id', (req, res) => {
return res.status(404).json({ error: '予約が見つかりません' });
}
const reservation = db.prepare('SELECT * FROM reservations WHERE id = ?').get(req.params.id);
broadcast({ type: 'data_changed', entity: 'reservations' });
res.json(reservation);
});
@@ -136,9 +180,15 @@ app.delete('/api/reservations/:id', (req, res) => {
if (result.changes === 0) {
return res.status(404).json({ error: '予約が見つかりません' });
}
broadcast({ type: 'data_changed', entity: 'reservations' });
res.json({ success: true });
});
app.listen(PORT, () => {
// SPA fallback: serve index.html for any non-API route
app.get('/*path', apiLimiter, (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});