Files
yourpart3/backend/controllers/taxiMapController.js
Torsten Schulz (local) 1ab9407e79
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
2026-07-21 09:08:15 +02:00

126 lines
4.1 KiB
JavaScript
Executable File

import TaxiMapService from '../services/taxiMapService.js';
class TaxiMapController {
constructor() {
this.taxiMapService = new TaxiMapService();
// Bind all methods to the class instance
this.getMapTypes = this.getMapTypes.bind(this);
this.getMaps = this.getMaps.bind(this);
this.getMapById = this.getMapById.bind(this);
this.getDefaultMap = this.getDefaultMap.bind(this);
this.createMap = this.createMap.bind(this);
this.updateMap = this.updateMap.bind(this);
this.deleteMap = this.deleteMap.bind(this);
this.setDefaultMap = this.setDefaultMap.bind(this);
}
async getMapTypes(req, res) {
try {
const mapTypes = await this.taxiMapService.getMapTypes();
res.json({ success: true, data: mapTypes });
} catch (error) {
console.error('Error getting map types:', error);
res.status(500).json({ success: false, message: 'Fehler beim Laden der Map-Typen' });
}
}
async getMaps(req, res) {
try {
const maps = await this.taxiMapService.getMaps();
res.json({ success: true, data: maps });
} catch (error) {
console.error('Error getting maps:', error);
res.status(500).json({ success: false, message: 'Fehler beim Laden der Maps' });
}
}
async getMapById(req, res) {
try {
const { mapId } = req.params;
const map = await this.taxiMapService.getMapById(mapId);
if (!map) {
return res.status(404).json({ success: false, message: 'Map nicht gefunden' });
}
res.json({ success: true, data: map });
} catch (error) {
console.error('Error getting map by ID:', error);
res.status(500).json({ success: false, message: 'Fehler beim Laden der Map' });
}
}
async getDefaultMap(req, res) {
try {
const map = await this.taxiMapService.getDefaultMap();
if (!map) {
return res.status(404).json({ success: false, message: 'Keine Standard-Map gefunden' });
}
res.json({ success: true, data: map });
} catch (error) {
console.error('Error getting default map:', error);
res.status(500).json({ success: false, message: 'Fehler beim Laden der Standard-Map' });
}
}
async createMap(req, res) {
try {
const mapData = req.body;
const map = await this.taxiMapService.createMap(mapData);
res.status(201).json({ success: true, data: map });
} catch (error) {
console.error('Error creating map:', error);
res.status(500).json({ success: false, message: 'Fehler beim Erstellen der Map' });
}
}
async updateMap(req, res) {
try {
const { mapId } = req.params;
const updateData = req.body;
const map = await this.taxiMapService.updateMap(mapId, updateData);
res.json({ success: true, data: map });
} catch (error) {
console.error('Error updating map:', error);
if (error.message === 'Map not found') {
return res.status(404).json({ success: false, message: 'Map nicht gefunden' });
}
res.status(500).json({ success: false, message: 'Fehler beim Aktualisieren der Map' });
}
}
async deleteMap(req, res) {
try {
const { mapId } = req.params;
await this.taxiMapService.deleteMap(mapId);
res.json({ success: true, message: 'Map erfolgreich gelöscht' });
} catch (error) {
console.error('Error deleting map:', error);
if (error.message === 'Map not found') {
return res.status(404).json({ success: false, message: 'Map nicht gefunden' });
}
res.status(500).json({ success: false, message: 'Fehler beim Löschen der Map' });
}
}
async setDefaultMap(req, res) {
try {
const { mapId } = req.params;
await this.taxiMapService.setDefaultMap(mapId);
res.json({ success: true, message: 'Standard-Map erfolgreich gesetzt' });
} catch (error) {
console.error('Error setting default map:', error);
if (error.message === 'Map not found') {
return res.status(404).json({ success: false, message: 'Map nicht gefunden' });
}
res.status(500).json({ success: false, message: 'Fehler beim Setzen der Standard-Map' });
}
}
}
export default TaxiMapController;