123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- // logic for routes
- import Settings from "../models/settings.model.js";
- export const getSingleSetting = async (req, res) => {
- let id = req.params.id;
- try {
- const setting = await Settings.findById(id);
- console.log(setting);
- res.status(200).json(setting);
- } catch (error) {
- res.status(404).json({ message: error.message });
- }
- }
- export const getSettingsOfUser = async (req, res) => {
- let id = req.params.id;
- try {
- const setting = await Settings.findOne({ userId: id });
- console.log('delivering', setting);
- res.status(200).json(setting);
- } catch (error) {
- res.status(404).json({ message: error.message });
- }
- }
- export const addSettings = async (req, res) => {
- console.log('trying to add the following Settings', req.body);
- const givenSettings = req.body;
- const newSettings = new Settings(givenSettings);
- console.log(newSettings);
- try {
- console.log('starting await');
- await newSettings.save();
- console.log('after await')
- res.status(201).json({ 'message': 'successfully added new Settings', 'Settings': newSettings });
- } catch (error) {
- res.status(409).json({ message: error.message });
- }
- }
- export const updateSettings = async (req, res) => {
- let newSettings = req.body;
- let id = req.params.id;
- try {
- Settings.findById(id, function (err, Settings) {
- Settings.set('userId', newSettings.userId);
- Settings.set('language', newSettings.language);
- Settings.save().then(Settings => {
- res.status(201).json({ 'info': 'Settings updated', Settings })
- });
- });
- } catch (error) {
- res.status(400).json({ 'info': `Update of Settings ${id} failed`, 'message': error.message });
- }
- }
- export const deleteSettings = async (req, res) => {
- let id = req.params.id;
- Settings.findByIdAndDelete(id, {}, function (err, Settings) {
- if (err) {
- res.status(400).json({ 'info': `Deletion of Settings ${id} failed`, 'message': err.message });
- } else {
- res.status(201).json({ 'info': 'Settings deleted, id: ', id, Settings })
- }
- });
- }
|