Changed controllers to classes, added image functionality

This commit is contained in:
Torsten Schulz
2024-09-21 15:26:29 +02:00
parent e494fe41db
commit f1b6dd74f7
20 changed files with 836 additions and 581 deletions

View File

@@ -1,157 +1,161 @@
import settingsService from '../services/settingsService.js';
export const filterSettings = async (req, res) => {
const { userid, type } = req.body;
try {
const responseFields = await settingsService.filterSettings(userid, type);
res.status(200).json(responseFields);
} catch (error) {
console.error('Error filtering settings:', error);
res.status(500).json({ error: 'An error occurred while filtering the settings' });
class SettingsController {
async filterSettings(req, res) {
const { userid, type } = req.body;
try {
const responseFields = await settingsService.filterSettings(userid, type);
res.status(200).json(responseFields);
} catch (error) {
console.error('Error filtering settings:', error);
res.status(500).json({ error: 'An error occurred while filtering the settings' });
}
}
};
export const updateSetting = async (req, res) => {
const { userid, settingId, value } = req.body;
try {
await settingsService.updateSetting(userid, settingId, value);
res.status(200).json({ message: 'Setting updated successfully' });
} catch (error) {
console.error('Error updating user setting:', error);
res.status(500).json({ error: 'Internal server error' });
async updateSetting(req, res) {
const { userid, settingId, value } = req.body;
try {
await settingsService.updateSetting(userid, settingId, value);
res.status(200).json({ message: 'Setting updated successfully' });
} catch (error) {
console.error('Error updating user setting:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
};
export const getTypeParamValueId = async (req, res) => {
const { paramValue } = req.body;
try {
const paramValueId = await settingsService.getTypeParamValueId(paramValue);
res.status(200).json({ paramValueId });
} catch (error) {
console.error('Error retrieving parameter value ID:', error);
res.status(404).json({ error: "notfound" });
async getTypeParamValueId(req, res) {
const { paramValue } = req.body;
try {
const paramValueId = await settingsService.getTypeParamValueId(paramValue);
res.status(200).json({ paramValueId });
} catch (error) {
console.error('Error retrieving parameter value ID:', error);
res.status(404).json({ error: "notfound" });
}
}
};
export const getTypeParamValues = async (req, res) => {
const { type } = req.body;
try {
const paramValues = await settingsService.getTypeParamValues(type);
res.status(200).json(paramValues);
} catch (error) {
console.error('Error retrieving parameter values:', error);
res.status(500).json({ error: 'An error occurred while retrieving the parameter values' });
async getTypeParamValues(req, res) {
const { type } = req.body;
try {
const paramValues = await settingsService.getTypeParamValues(type);
res.status(200).json(paramValues);
} catch (error) {
console.error('Error retrieving parameter values:', error);
res.status(500).json({ error: 'An error occurred while retrieving the parameter values' });
}
}
async getTypeParamValue(req, res) {
const { id } = req.params;
try {
const paramValue = await settingsService.getTypeParamValue(id);
res.status(200).json({ paramValue });
} catch (error) {
console.error('Error retrieving parameter value:', error);
res.status(404).json({ error: "notfound" });
}
}
async getAccountSettings(req, res) {
try {
const { userId } = req.body;
const accountSettings = await settingsService.getAccountSettings(userId);
res.status(200).json(accountSettings);
} catch (error) {
console.error('Error retrieving account settings:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
async setAccountSettings(req, res) {
try {
await settingsService.setAccountSettings(req.body);
res.status(200).json({ message: 'Account settings updated successfully' });
} catch (error) {
console.error('Error updating account settings:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
async getPossibleInterests(req, res) {
try {
const { userid: userId } = req.headers;
const interests = await settingsService.getPossibleInterests(userId);
res.status(200).json(interests);
} catch (error) {
console.error('Error retrieving possible interests:', error);
res.status(500).json({ error: 'An error occurred while retrieving the possible interests' });
}
}
async getInterests(req, res) {
try {
const { userid: userId } = req.headers;
const interests = await settingsService.getInterests(userId);
res.status(200).json(interests);
} catch (error) {
console.error('Error retrieving interests:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
async addInterest(req, res) {
try {
const { userid: userId } = req.headers;
const { name } = req.body;
const interest = await settingsService.addInterest(userId, name);
res.status(200).json({ interest });
} catch (error) {
console.error('Error adding interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
async addUserInterest(req, res) {
try {
const { userid: userId } = req.headers;
const { interestid: interestId } = req.body;
await settingsService.addUserInterest(userId, interestId);
res.status(200).json({ message: 'User interest added successfully' });
} catch (error) {
console.error('Error adding user interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
async removeInterest(req, res) {
try {
const { userid: userId } = req.headers;
const { id: interestId } = req.params;
await settingsService.removeInterest(userId, interestId);
res.status(200).json({ message: 'Interest removed successfully' });
} catch (error) {
console.error('Error removing interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
async getVisibilities(req, res) {
try {
const visibilities = await settingsService.getVisibilities();
res.status(200).json(visibilities);
} catch (error) {
console.error('Error retrieving visibilities:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
async updateVisibility(req, res) {
const { userParamTypeId, visibilityId } = req.body;
const hashedUserId = req.headers.userid;
try {
await settingsService.updateVisibility(hashedUserId, userParamTypeId, visibilityId);
res.status(200).json({ message: 'Visibility updated successfully' });
} catch (error) {
console.error('Error updating visibility:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
}
export const getTypeParamValue = async (req, res) => {
const { id } = req.params;
try {
const paramValue = await settingsService.getTypeParamValue(id);
res.status(200).json({ paramValue });
} catch (error) {
console.error('Error retrieving parameter value:', error);
res.status(404).json({ error: "notfound" });
}
};
export const getAccountSettings = async (req, res) => {
try {
const { userId } = req.body;
const accountSettings = await settingsService.getAccountSettings(userId);
res.status(200).json(accountSettings);
} catch (error) {
console.error('Error retrieving account settings:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
export const setAccountSettings = async (req, res) => {
try {
await settingsService.setAccountSettings(req.body);
res.status(200).json({ message: 'Account settings updated successfully' });
} catch (error) {
console.error('Error updating account settings:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
export const getPossibleInterests = async (req, res) => {
try {
const { userid: userId } = req.headers;
const interests = await settingsService.getPossibleInterests(userId);
res.status(200).json(interests);
} catch (error) {
console.error('Error retrieving possible interests:', error);
res.status(500).json({ error: 'An error occurred while retrieving the possible interests' });
}
}
export const getInterests = async (req, res) => {
try {
const { userid: userId } = req.headers;
const interests = await settingsService.getInterests(userId);
res.status(200).json(interests);
} catch (error) {
console.error('Error retrieving interests:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const addInterest = async (req, res) => {
try {
const { userid: userId } = req.headers;
const { name } = req.body;
const interest = await settingsService.addInterest(userId, name);
res.status(200).json({ interest });
} catch (error) {
console.error('Error adding interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const addUserInterest = async (req, res) => {
try {
const { userid: userId } = req.headers;
const { interestid: interestId } = req.body;
await settingsService.addUserInterest(userId, interestId);
res.status(200).json({ message: 'User interest added successfully' });
} catch (error) {
console.error('Error adding user interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const removeInterest = async (req, res) => {
try {
const { userid: userId } = req.headers;
const { id: interestId } = req.params;
await settingsService.removeInterest(userId, interestId);
res.status(200).json({ message: 'Interest removed successfully' });
} catch (error) {
console.error('Error removing interest:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const getVisibilities = async (req, res) => {
try {
const visibilities = await settingsService.getVisibilities();
res.status(200).json(visibilities);
} catch (error) {
console.error('Error retrieving visibilities:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export const updateVisibility = async (req, res) => {
const { userParamTypeId, visibilityId } = req.body;
const hashedUserId = req.headers.userid;
try {
await settingsService.updateVisibility(hashedUserId, userParamTypeId, visibilityId);
res.status(200).json({ message: 'Visibility updated successfully' });
} catch (error) {
console.error('Error updating visibility:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
export default SettingsController;