Add car reordering and AM/PM period for reservations
Agent-Logs-Url: https://github.com/pdf114514/CarReservation/sessions/c0a4b7dc-228e-4e7d-a985-61b9a17de159 Co-authored-by: pdf114514 <57948770+pdf114514@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
2e9e100178
commit
675e5f6fe8
@@ -64,15 +64,28 @@ if (!carCols.includes('has_etc')) {
|
||||
if (!carCols.includes('tire_type')) {
|
||||
db.exec("ALTER TABLE cars ADD COLUMN tire_type TEXT DEFAULT 'ノーマル'");
|
||||
}
|
||||
if (!carCols.includes('sort_order')) {
|
||||
db.exec('ALTER TABLE cars ADD COLUMN sort_order INTEGER DEFAULT 0');
|
||||
db.exec('UPDATE cars SET sort_order = id');
|
||||
}
|
||||
db.prepare("UPDATE cars SET tire_type = 'スタッドレス' WHERE tire_type = 'スタットレス'").run();
|
||||
|
||||
// Migrate: add period fields to reservations if they don't exist yet
|
||||
const resCols = db.prepare("PRAGMA table_info(reservations)").all().map((c) => c.name);
|
||||
if (!resCols.includes('start_period')) {
|
||||
db.exec("ALTER TABLE reservations ADD COLUMN start_period TEXT DEFAULT ''");
|
||||
}
|
||||
if (!resCols.includes('end_period')) {
|
||||
db.exec("ALTER TABLE reservations ADD COLUMN end_period TEXT DEFAULT ''");
|
||||
}
|
||||
|
||||
// Seed some initial cars if none exist
|
||||
const carCount = db.prepare('SELECT COUNT(*) as cnt FROM cars').get();
|
||||
if (carCount.cnt === 0) {
|
||||
const insertCar = db.prepare('INSERT INTO cars (name, description) VALUES (?, ?)');
|
||||
insertCar.run('代車 A', '');
|
||||
insertCar.run('代車 B', '');
|
||||
insertCar.run('代車 C', '');
|
||||
const insertCar = db.prepare('INSERT INTO cars (name, description, sort_order) VALUES (?, ?, ?)');
|
||||
insertCar.run('代車 A', '', 1);
|
||||
insertCar.run('代車 B', '', 2);
|
||||
insertCar.run('代車 C', '', 3);
|
||||
}
|
||||
|
||||
// --- WebSocket Server ---
|
||||
@@ -107,7 +120,7 @@ wss.on('connection', (ws) => {
|
||||
|
||||
// --- Cars API ---
|
||||
app.get('/api/cars', (req, res) => {
|
||||
const cars = db.prepare('SELECT * FROM cars ORDER BY id').all().map(normalizeCar);
|
||||
const cars = db.prepare('SELECT * FROM cars ORDER BY sort_order, id').all().map(normalizeCar);
|
||||
res.json(cars);
|
||||
});
|
||||
|
||||
@@ -116,14 +129,31 @@ app.post('/api/cars', (req, res) => {
|
||||
if (!name || !name.trim()) {
|
||||
return res.status(400).json({ error: '車名は必須です' });
|
||||
}
|
||||
const maxOrder = db.prepare('SELECT MAX(sort_order) as m FROM cars').get().m ?? 0;
|
||||
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);
|
||||
'INSERT INTO cars (name, description, inspection_expiry, has_etc, tire_type, sort_order) VALUES (?, ?, ?, ?, ?, ?)'
|
||||
).run(name.trim(), description, inspection_expiry, has_etc ? 1 : 0, tire_type, maxOrder + 1);
|
||||
const car = normalizeCar(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/reorder', (req, res) => {
|
||||
const { ids } = req.body;
|
||||
if (!Array.isArray(ids) || ids.length === 0) {
|
||||
return res.status(400).json({ error: 'ids は配列で指定してください' });
|
||||
}
|
||||
const updateOrder = db.prepare('UPDATE cars SET sort_order = ? WHERE id = ?');
|
||||
const transaction = db.transaction((orderedIds) => {
|
||||
orderedIds.forEach((id, index) => {
|
||||
updateOrder.run(index + 1, id);
|
||||
});
|
||||
});
|
||||
transaction(ids);
|
||||
broadcast({ type: 'data_changed', entity: 'cars' });
|
||||
res.json({ success: true });
|
||||
});
|
||||
|
||||
app.put('/api/cars/:id', (req, res) => {
|
||||
const { name, description, inspection_expiry, has_etc, tire_type } = req.body;
|
||||
if (!name || !name.trim()) {
|
||||
@@ -156,7 +186,7 @@ app.get('/api/reservations', (req, res) => {
|
||||
});
|
||||
|
||||
app.post('/api/reservations', (req, res) => {
|
||||
const { car_id, start_date, end_date, customer_name = '', notes = '' } = req.body;
|
||||
const { car_id, start_date, end_date, customer_name = '', notes = '', start_period = '', end_period = '' } = req.body;
|
||||
if (!car_id || !start_date || !end_date) {
|
||||
return res.status(400).json({ error: 'car_id, start_date, end_date は必須です' });
|
||||
}
|
||||
@@ -164,15 +194,15 @@ app.post('/api/reservations', (req, res) => {
|
||||
return res.status(400).json({ error: '開始日は終了日以前である必要があります' });
|
||||
}
|
||||
const result = db.prepare(
|
||||
'INSERT INTO reservations (car_id, start_date, end_date, customer_name, notes) VALUES (?, ?, ?, ?, ?)'
|
||||
).run(car_id, start_date, end_date, customer_name, notes);
|
||||
'INSERT INTO reservations (car_id, start_date, end_date, customer_name, notes, start_period, end_period) VALUES (?, ?, ?, ?, ?, ?, ?)'
|
||||
).run(car_id, start_date, end_date, customer_name, notes, start_period, end_period);
|
||||
const reservation = db.prepare('SELECT * FROM reservations WHERE id = ?').get(result.lastInsertRowid);
|
||||
broadcast({ type: 'data_changed', entity: 'reservations' });
|
||||
res.status(201).json(reservation);
|
||||
});
|
||||
|
||||
app.put('/api/reservations/:id', (req, res) => {
|
||||
const { car_id, start_date, end_date, customer_name, notes } = req.body;
|
||||
const { car_id, start_date, end_date, customer_name, notes, start_period, end_period } = req.body;
|
||||
if (!car_id || !start_date || !end_date) {
|
||||
return res.status(400).json({ error: 'car_id, start_date, end_date は必須です' });
|
||||
}
|
||||
@@ -180,8 +210,8 @@ app.put('/api/reservations/:id', (req, res) => {
|
||||
return res.status(400).json({ error: '開始日は終了日以前である必要があります' });
|
||||
}
|
||||
const result = db.prepare(
|
||||
'UPDATE reservations SET car_id = ?, start_date = ?, end_date = ?, customer_name = ?, notes = ? WHERE id = ?'
|
||||
).run(car_id, start_date, end_date, customer_name ?? '', notes ?? '', req.params.id);
|
||||
'UPDATE reservations SET car_id = ?, start_date = ?, end_date = ?, customer_name = ?, notes = ?, start_period = ?, end_period = ? WHERE id = ?'
|
||||
).run(car_id, start_date, end_date, customer_name ?? '', notes ?? '', start_period ?? '', end_period ?? '', req.params.id);
|
||||
if (result.changes === 0) {
|
||||
return res.status(404).json({ error: '予約が見つかりません' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user