Refaktoriere Controller-Methoden zur Benutzer-, Event- und Menü-Datenverwaltung, indem die Logik in separate Service-Klassen ausgelagert wird. Implementiere eine verbesserte Fehlerbehandlung und sichere Rückgaben. Füge eine neue Route zur Passwortänderung im Benutzer-Router hinzu.
This commit is contained in:
132
services/PageService.js
Normal file
132
services/PageService.js
Normal file
@@ -0,0 +1,132 @@
|
||||
const { Page } = require('../models');
|
||||
|
||||
class PageService {
|
||||
/**
|
||||
* Seiteninhalt anhand Link abrufen
|
||||
*/
|
||||
async getPageContent(link) {
|
||||
try {
|
||||
if (!link) {
|
||||
throw new Error('VALIDATION_ERROR: Link ist erforderlich');
|
||||
}
|
||||
|
||||
const page = await Page.findOne({ where: { link } });
|
||||
|
||||
if (!page) {
|
||||
throw new Error('PAGE_NOT_FOUND');
|
||||
}
|
||||
|
||||
return {
|
||||
content: page.content || '',
|
||||
title: page.title || '',
|
||||
link: page.link
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching page content:', error);
|
||||
throw new Error('PAGE_CONTENT_FETCH_ERROR');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Seiteninhalt speichern
|
||||
*/
|
||||
async savePageContent(pageData) {
|
||||
try {
|
||||
const { link, name, content } = pageData;
|
||||
|
||||
if (!link || !name) {
|
||||
throw new Error('VALIDATION_ERROR: Link und Name sind erforderlich');
|
||||
}
|
||||
|
||||
// Prüfen ob Seite bereits existiert
|
||||
const existingPage = await Page.findOne({ where: { link } });
|
||||
|
||||
if (existingPage) {
|
||||
// Seite aktualisieren
|
||||
await existingPage.update({
|
||||
content: content || '',
|
||||
title: name,
|
||||
updated_at: new Date()
|
||||
});
|
||||
} else {
|
||||
// Neue Seite erstellen
|
||||
await Page.create({
|
||||
link,
|
||||
title: name,
|
||||
content: content || '',
|
||||
created_at: new Date(),
|
||||
updated_at: new Date()
|
||||
});
|
||||
}
|
||||
|
||||
return { message: 'Seiteninhalt erfolgreich gespeichert' };
|
||||
} catch (error) {
|
||||
console.error('Error saving page content:', error);
|
||||
throw new Error('PAGE_CONTENT_SAVE_ERROR');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Seiten abrufen
|
||||
*/
|
||||
async getAllPages() {
|
||||
try {
|
||||
const pages = await Page.findAll({
|
||||
order: [['title', 'ASC']],
|
||||
attributes: ['id', 'link', 'title', 'created_at', 'updated_at']
|
||||
});
|
||||
|
||||
return pages;
|
||||
} catch (error) {
|
||||
console.error('Error fetching all pages:', error);
|
||||
throw new Error('PAGES_FETCH_ERROR');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Seite anhand ID abrufen
|
||||
*/
|
||||
async getPageById(id) {
|
||||
try {
|
||||
if (!id || isNaN(parseInt(id))) {
|
||||
throw new Error('VALIDATION_ERROR: Ungültige ID');
|
||||
}
|
||||
|
||||
const page = await Page.findByPk(id);
|
||||
|
||||
if (!page) {
|
||||
throw new Error('PAGE_NOT_FOUND');
|
||||
}
|
||||
|
||||
return page;
|
||||
} catch (error) {
|
||||
console.error('Error fetching page by ID:', error);
|
||||
throw new Error('PAGE_FETCH_ERROR');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Seite löschen
|
||||
*/
|
||||
async deletePage(id) {
|
||||
try {
|
||||
if (!id || isNaN(parseInt(id))) {
|
||||
throw new Error('VALIDATION_ERROR: Ungültige ID');
|
||||
}
|
||||
|
||||
const page = await Page.findByPk(id);
|
||||
|
||||
if (!page) {
|
||||
throw new Error('PAGE_NOT_FOUND');
|
||||
}
|
||||
|
||||
await page.destroy();
|
||||
return { message: 'Seite erfolgreich gelöscht' };
|
||||
} catch (error) {
|
||||
console.error('Error deleting page:', error);
|
||||
throw new Error('PAGE_DELETE_ERROR');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new PageService();
|
||||
Reference in New Issue
Block a user