Manage categories and locations
This commit is contained in:
@@ -7,6 +7,8 @@ import itemsRouter from './routes/items.js';
|
||||
import uploadsRouter, { createFileUploadRouter, createFileDeleteRouter } from './routes/uploads.js';
|
||||
import statsRouter from './routes/stats.js';
|
||||
import suggestionsRouter from './routes/suggestions.js';
|
||||
import locationsRouter from './routes/locations.js';
|
||||
import categoriesRouter from './routes/categories.js';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const app = express();
|
||||
@@ -26,6 +28,8 @@ app.use('/api/files', createFileDeleteRouter());
|
||||
app.use('/api/uploads', uploadsRouter);
|
||||
app.use('/api/stats', statsRouter);
|
||||
app.use('/api/suggestions', suggestionsRouter);
|
||||
app.use('/api/locations', locationsRouter);
|
||||
app.use('/api/categories', categoriesRouter);
|
||||
|
||||
// Serve static client in production
|
||||
const clientDist = join(__dirname, '..', '..', 'client', 'dist');
|
||||
|
||||
41
server/src/routes/categories.ts
Normal file
41
server/src/routes/categories.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Router } from 'express';
|
||||
import db from '../db.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// List all categories with item counts
|
||||
router.get('/', (_req, res) => {
|
||||
const rows = db.prepare(
|
||||
`SELECT category, COUNT(*) as item_count
|
||||
FROM items WHERE deleted_at IS NULL AND category != ''
|
||||
GROUP BY category ORDER BY category`
|
||||
).all() as { category: string; item_count: number }[];
|
||||
res.json(rows);
|
||||
});
|
||||
|
||||
// Rename a category (bulk update all items)
|
||||
router.put('/:name', (req, res) => {
|
||||
const oldName = req.params.name;
|
||||
const { name } = req.body;
|
||||
if (!name || typeof name !== 'string' || !name.trim()) {
|
||||
res.status(400).json({ error: 'New name is required' });
|
||||
return;
|
||||
}
|
||||
const result = db.prepare(
|
||||
`UPDATE items SET category = ?, updated_at = datetime('now')
|
||||
WHERE category = ? AND deleted_at IS NULL`
|
||||
).run(name.trim(), oldName);
|
||||
res.json({ updated: result.changes });
|
||||
});
|
||||
|
||||
// Delete a category (clear from all items)
|
||||
router.delete('/:name', (req, res) => {
|
||||
const name = req.params.name;
|
||||
const result = db.prepare(
|
||||
`UPDATE items SET category = '', updated_at = datetime('now')
|
||||
WHERE category = ? AND deleted_at IS NULL`
|
||||
).run(name);
|
||||
res.json({ updated: result.changes });
|
||||
});
|
||||
|
||||
export default router;
|
||||
41
server/src/routes/locations.ts
Normal file
41
server/src/routes/locations.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Router } from 'express';
|
||||
import db from '../db.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// List all locations with item counts
|
||||
router.get('/', (_req, res) => {
|
||||
const rows = db.prepare(
|
||||
`SELECT location, COUNT(*) as item_count
|
||||
FROM items WHERE deleted_at IS NULL AND location != ''
|
||||
GROUP BY location ORDER BY location`
|
||||
).all() as { location: string; item_count: number }[];
|
||||
res.json(rows);
|
||||
});
|
||||
|
||||
// Rename a location (bulk update all items)
|
||||
router.put('/:name', (req, res) => {
|
||||
const oldName = req.params.name;
|
||||
const { name } = req.body;
|
||||
if (!name || typeof name !== 'string' || !name.trim()) {
|
||||
res.status(400).json({ error: 'New name is required' });
|
||||
return;
|
||||
}
|
||||
const result = db.prepare(
|
||||
`UPDATE items SET location = ?, updated_at = datetime('now')
|
||||
WHERE location = ? AND deleted_at IS NULL`
|
||||
).run(name.trim(), oldName);
|
||||
res.json({ updated: result.changes });
|
||||
});
|
||||
|
||||
// Delete a location (clear from all items)
|
||||
router.delete('/:name', (req, res) => {
|
||||
const name = req.params.name;
|
||||
const result = db.prepare(
|
||||
`UPDATE items SET location = '', updated_at = datetime('now')
|
||||
WHERE location = ? AND deleted_at IS NULL`
|
||||
).run(name);
|
||||
res.json({ updated: result.changes });
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user