147 lines
3.7 KiB
JavaScript
147 lines
3.7 KiB
JavaScript
const express = require('express');
|
|
const { ShoppingItem } = require('../models');
|
|
const { Op } = require('sequelize');
|
|
|
|
const router = express.Router();
|
|
|
|
// Get all shopping items
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const items = await ShoppingItem.findAll({
|
|
order: [['createdAt', 'DESC']],
|
|
});
|
|
res.json(items);
|
|
} catch (error) {
|
|
console.error('Error fetching shopping items:', error);
|
|
res.status(500).json({ error: 'Failed to fetch shopping items' });
|
|
}
|
|
});
|
|
|
|
// Get a specific shopping item
|
|
router.get('/:id', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const item = await ShoppingItem.findByPk(id);
|
|
|
|
if (!item) {
|
|
return res.status(404).json({ error: 'Shopping item not found' });
|
|
}
|
|
|
|
res.json(item);
|
|
} catch (error) {
|
|
console.error('Error fetching shopping item:', error);
|
|
res.status(500).json({ error: 'Failed to fetch shopping item' });
|
|
}
|
|
});
|
|
|
|
// Create a new shopping item
|
|
router.post('/', async (req, res) => {
|
|
try {
|
|
const { name, amount, date } = req.body;
|
|
|
|
if (!name) {
|
|
return res.status(400).json({ error: 'Name is required' });
|
|
}
|
|
|
|
const item = await ShoppingItem.create({
|
|
name,
|
|
amount: amount || '1',
|
|
date: date || new Date(),
|
|
});
|
|
|
|
res.status(201).json(item);
|
|
} catch (error) {
|
|
console.error('Error creating shopping item:', error);
|
|
res.status(500).json({ error: 'Failed to create shopping item' });
|
|
}
|
|
});
|
|
|
|
// Update a shopping item
|
|
router.put('/:id', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { name, amount, checked, date } = req.body;
|
|
|
|
const item = await ShoppingItem.findByPk(id);
|
|
|
|
if (!item) {
|
|
return res.status(404).json({ error: 'Shopping item not found' });
|
|
}
|
|
|
|
// Update fields if provided
|
|
if (name !== undefined) item.name = name;
|
|
if (amount !== undefined) item.amount = amount;
|
|
if (checked !== undefined) item.checked = checked;
|
|
if (date !== undefined) item.date = date;
|
|
|
|
await item.save();
|
|
|
|
res.json(item);
|
|
} catch (error) {
|
|
console.error('Error updating shopping item:', error);
|
|
res.status(500).json({ error: 'Failed to update shopping item' });
|
|
}
|
|
});
|
|
|
|
// Toggle checked status of a shopping item
|
|
router.patch('/:id/toggle', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
const item = await ShoppingItem.findByPk(id);
|
|
|
|
if (!item) {
|
|
return res.status(404).json({ error: 'Shopping item not found' });
|
|
}
|
|
|
|
item.checked = !item.checked;
|
|
await item.save();
|
|
|
|
res.json(item);
|
|
} catch (error) {
|
|
console.error('Error toggling shopping item:', error);
|
|
res.status(500).json({ error: 'Failed to toggle shopping item' });
|
|
}
|
|
});
|
|
|
|
// Delete a shopping item
|
|
router.delete('/:id', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
const item = await ShoppingItem.findByPk(id);
|
|
|
|
if (!item) {
|
|
return res.status(404).json({ error: 'Shopping item not found' });
|
|
}
|
|
|
|
await item.destroy();
|
|
|
|
res.json({ message: 'Shopping item deleted successfully' });
|
|
} catch (error) {
|
|
console.error('Error deleting shopping item:', error);
|
|
res.status(500).json({ error: 'Failed to delete shopping item' });
|
|
}
|
|
});
|
|
|
|
// Delete all checked items
|
|
router.delete('/checked/all', async (req, res) => {
|
|
try {
|
|
const deletedCount = await ShoppingItem.destroy({
|
|
where: {
|
|
checked: true,
|
|
},
|
|
});
|
|
|
|
res.json({
|
|
message: `Deleted ${deletedCount} checked items`,
|
|
deletedCount
|
|
});
|
|
} catch (error) {
|
|
console.error('Error deleting checked items:', error);
|
|
res.status(500).json({ error: 'Failed to delete checked items' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|