finito spero

This commit is contained in:
StefanoPutelli 2025-06-28 16:31:34 +02:00
parent 3ea07942c6
commit 54d4faca9d
12 changed files with 7315 additions and 70 deletions

View file

@ -5,6 +5,7 @@ export interface IItem extends Document {
name: string;
description: string;
tags: Types.Array<ITag['_id']>;
quantity: number; // ← nuovo
dateAdded: Date;
addedBy: string;
}
@ -13,8 +14,9 @@ const itemSchema = new Schema<IItem>({
name: { type: String, required: true },
description: { type: String, required: true },
tags: [{ type: Schema.Types.ObjectId, ref: 'Tag' }],
quantity: { type: Number, default: 0, min: 0 }, // default 0, mai negativo
dateAdded: { type: Date, default: Date.now },
addedBy: { type: String }
addedBy: { type: String },
});
export default model<IItem>('Item', itemSchema);

View file

@ -1,6 +1,6 @@
import { Router } from 'express';
import Item from '../models/Item.js';
import Tag from '../models/Tag.js';
import Tag from '../models/Tag.js';
import auth from '../middleware/auth.js';
const router = Router();
@ -26,16 +26,16 @@ router.get('/search', async (req, res) => {
],
};
const items = await Item.find(filter).populate('tags');
res.json(items);
res.json(await Item.find(filter).populate('tags'));
});
/* ──────────── POST: crea item (protetto) ──────────── */
/* ──────────── POST: crea item ──────────── */
router.post('/', auth, async (req, res) => {
const { name, description, tagIds } = req.body as {
const { name, description, tagIds, quantity = 0 } = req.body as {
name: string;
description: string;
tagIds: string[];
quantity?: number;
};
const tags = await Tag.find({ _id: { $in: tagIds } });
@ -43,23 +43,68 @@ router.post('/', auth, async (req, res) => {
name,
description,
tags,
quantity,
addedBy: req.user!.email,
});
res.status(201).json(await item.populate('tags'));
});
/* ──────────── DELETE: elimina item (protetto) ──────────── */
/* ──────────── PUT: aggiorna intero item ──────────── */
router.put('/:id', auth, async (req, res) => {
const { id } = req.params;
const { name, description, tagIds, quantity } = req.body as {
name?: string;
description?: string;
tagIds?: string[];
quantity?: number;
};
const update: any = {};
if (name !== undefined) update.name = name;
if (description !== undefined) update.description = description;
if (quantity !== undefined) update.quantity = quantity;
if (tagIds) update.tags = await Tag.find({ _id: { $in: tagIds } });
const item = await Item.findByIdAndUpdate(id, update, { new: true }).populate('tags');
if (!item) {
res.status(404).json({ message: 'Item non trovato' });
return;
}
res.json(item);
});
/* ──────────── PATCH: modifica solo la quantità ──────────── */
router.patch('/:id/quantity', auth, async (req, res) => {
const { id } = req.params;
const { quantity } = req.body as { quantity: number };
if (quantity < 0) {
res.status(400).json({ message: 'La quantità non può essere negativa' });
return;
}
const item = await Item.findByIdAndUpdate(
id,
{ quantity },
{ new: true }
).populate('tags');
if (!item) {
res.status(404).json({ message: 'Item non trovato' });
return;
}
res.json(item);
});
/* ──────────── DELETE: elimina item ──────────── */
router.delete('/:id', auth, async (req, res) => {
const { id } = req.params;
const deleted = await Item.findByIdAndDelete(id);
if (!deleted) {
res.status(404).json({ message: 'Item non trovato' });
return;
}
// 204 No Content: operazione riuscita, nessun body
res.status(204).end();
});