This commit is contained in:
StefanoPutelli 2025-06-28 16:01:31 +02:00
commit 04267b3886
100 changed files with 16495 additions and 0 deletions

View file

@ -0,0 +1,29 @@
import { Router } from 'express';
import jwt from 'jsonwebtoken';
import User, { IUser } from '../models/User.js';
const router = Router();
router.post('/register', async (req, res) => {
try {
const { email, password, name } = req.body as IUser;
const user = await User.create({ email, password, name });
const token = jwt.sign({ id: user.id, email }, process.env.JWT_SECRET!);
res.json({ token, user: { id: user.id, email, name } });
} catch (err: any) {
res.status(400).json({ message: 'Registrazione fallita', error: err.message });
}
});
router.post('/login', async (req, res) => {
const { email, password } = req.body as IUser;
const user = await User.findOne({ email });
if (!user || !(await user.comparePassword(password))) {
res.status(401).json({ message: 'Credenziali non valide' });
return; // ← esce senza value → tipo void
}
const token = jwt.sign({ id: user.id, email }, process.env.JWT_SECRET!);
res.json({ token, user: { id: user.id, email, name: user.name } });
});
export default router;

View file

@ -0,0 +1,66 @@
import { Router } from 'express';
import Item from '../models/Item.js';
import Tag from '../models/Tag.js';
import auth from '../middleware/auth.js';
const router = Router();
/* ──────────── GET: lista completa ──────────── */
router.get('/', async (_req, res) => {
const items = await Item.find().populate('tags');
res.json(items);
});
/* ──────────── GET: ricerca ──────────── */
router.get('/search', async (req, res) => {
const { query = '', tagIds = '' } = req.query as {
query?: string;
tagIds?: string;
};
const regex = new RegExp(query, 'i');
const filter: any = {
$and: [
{ $or: [{ name: regex }, { description: regex }] },
tagIds ? { tags: { $in: tagIds.split(',') } } : {},
],
};
const items = await Item.find(filter).populate('tags');
res.json(items);
});
/* ──────────── POST: crea item (protetto) ──────────── */
router.post('/', auth, async (req, res) => {
const { name, description, tagIds } = req.body as {
name: string;
description: string;
tagIds: string[];
};
const tags = await Tag.find({ _id: { $in: tagIds } });
const item = await Item.create({
name,
description,
tags,
addedBy: req.user!.email,
});
res.status(201).json(await item.populate('tags'));
});
/* ──────────── DELETE: elimina item (protetto) ──────────── */
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();
});
export default router;

View file

@ -0,0 +1,40 @@
import { Router } from 'express';
import Tag from '../models/Tag.js';
import Item from '../models/Item.js';
import auth from '../middleware/auth.js';
const router = Router();
/* ──────────── GET: tutti i tag ──────────── */
router.get('/', async (_req, res) => {
res.json(await Tag.find());
});
/* ──────────── POST: crea tag (protetto) ──────────── */
router.post('/', auth, async (req, res) => {
const { name, color } = req.body as { name: string; color: string };
const tag = await Tag.create({ name, color });
res.status(201).json(tag);
});
/* ──────────── DELETE: elimina tag (protetto) ──────────── */
router.delete('/:id', auth, async (req, res) => {
const { id } = req.params;
// opzionale: blocca leliminazione se il tag è ancora assegnato a qualche item
const inUse = await Item.exists({ tags: id });
if (inUse) {
res.status(409).json({ message: 'Tag in uso: rimuovilo dagli item prima di cancellarlo' });
return;
}
const deleted = await Tag.findByIdAndDelete(id);
if (!deleted) {
res.status(404).json({ message: 'Tag non trovato' });
return;
}
res.status(204).end();
});
export default router;