Dateien nach "backend/src/routes" hochladen
This commit is contained in:
parent
e33aadaa90
commit
1dc1eade8f
78
backend/src/routes/documents.ts
Normal file
78
backend/src/routes/documents.ts
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
import { Router, Response } from 'express';
|
||||||
|
import { PrismaClient, DocumentCategory } from '@prisma/client';
|
||||||
|
import { AuthedRequest, requireAuth, requireRole } from '../middleware/auth';
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router für den Dokumenten-Safe & Tutorials (Hausordnung, WLAN-Zugang,
|
||||||
|
* Video/Bild-Guides zur Gerätepflege). Für alle eingeloggten Nutzer
|
||||||
|
* sichtbar; Hochladen/Löschen ist Vermieter/Admin vorbehalten.
|
||||||
|
*/
|
||||||
|
export const documentsRouter = Router();
|
||||||
|
|
||||||
|
const CATEGORIES = Object.values(DocumentCategory);
|
||||||
|
|
||||||
|
const DOCUMENT_SELECT = {
|
||||||
|
id: true,
|
||||||
|
title: true,
|
||||||
|
category: true,
|
||||||
|
url: true,
|
||||||
|
description: true,
|
||||||
|
createdAt: true,
|
||||||
|
uploadedBy: { select: { id: true, fullName: true } },
|
||||||
|
};
|
||||||
|
|
||||||
|
documentsRouter.get('/documents', requireAuth, async (_req: AuthedRequest, res: Response) => {
|
||||||
|
const documents = await prisma.document.findMany({
|
||||||
|
orderBy: { createdAt: 'desc' },
|
||||||
|
select: DOCUMENT_SELECT,
|
||||||
|
});
|
||||||
|
res.status(200).json({ documents });
|
||||||
|
});
|
||||||
|
|
||||||
|
documentsRouter.post(
|
||||||
|
'/documents',
|
||||||
|
requireAuth,
|
||||||
|
requireRole('LANDLORD', 'ADMIN'),
|
||||||
|
async (req: AuthedRequest, res: Response) => {
|
||||||
|
const { title, category, url, description } = req.body || {};
|
||||||
|
|
||||||
|
if (!title || typeof title !== 'string' || !title.trim()) {
|
||||||
|
return res.status(400).json({ error: 'title ist erforderlich' });
|
||||||
|
}
|
||||||
|
if (!url || typeof url !== 'string' || !url.trim()) {
|
||||||
|
return res.status(400).json({ error: 'url ist erforderlich' });
|
||||||
|
}
|
||||||
|
if (category && !CATEGORIES.includes(category)) {
|
||||||
|
return res.status(400).json({ error: `Ungültige Kategorie. Erlaubt: ${CATEGORIES.join(', ')}` });
|
||||||
|
}
|
||||||
|
|
||||||
|
const document = await prisma.document.create({
|
||||||
|
data: {
|
||||||
|
title: title.trim().slice(0, 120),
|
||||||
|
category: category || DocumentCategory.SONSTIGES,
|
||||||
|
url: url.trim(),
|
||||||
|
description: typeof description === 'string' && description.trim() ? description.trim().slice(0, 280) : null,
|
||||||
|
uploadedById: req.user!.id,
|
||||||
|
},
|
||||||
|
select: DOCUMENT_SELECT,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(201).json({ document });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
documentsRouter.delete(
|
||||||
|
'/documents/:id',
|
||||||
|
requireAuth,
|
||||||
|
requireRole('LANDLORD', 'ADMIN'),
|
||||||
|
async (req: AuthedRequest, res: Response) => {
|
||||||
|
try {
|
||||||
|
await prisma.document.delete({ where: { id: req.params.id } });
|
||||||
|
res.status(204).send();
|
||||||
|
} catch {
|
||||||
|
res.status(404).json({ error: 'Dokument nicht gefunden' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
Loading…
Reference in New Issue
Block a user