personal_website/controllers/userController.js
2023-09-23 22:02:05 +02:00

65 lines
1.8 KiB
JavaScript

var UserModel = require('../models/userModel');
module.exports = {
create: async function (req, res) {
console.log(req.body);
const pass = req.body.password;
const omdb_key = req.body.omdb_key;
const twitch_client_id = req.body.twitch_client_id;
const twitch_client_secret = req.body.twitch_client_secret;
try {
const userFound = await UserModel.findOne({ pass: pass });
if (userFound) {
return res.status(409).json({ message: 'User already exists' });
}
const user = new UserModel({
pass: pass,
omdb_key: omdb_key,
twitch_client_id: twitch_client_id,
twitch_client_secret: twitch_client_secret,
});
await user.save();
return res.redirect('/list');
}
catch (err) {
return res.status(500).json({ message: 'Error when creating user', error: err });
}
},
/**
* mediaController.delete()
*/
remove: async function (req, res) {
const pass = req.body.password;
try {
const userFound = await UserModel.findOne({ pass: pass });
if (!userFound) {
return res.status(404).json({ message: 'Wrong password' });
}
await userFound.remove();
return res.status(204).json({ message: 'User deleted' });
} catch (err) {
return res.status(500).json({ message: 'Error when deleting the user', error: err });
}
},
get: async function (req, res) {
const pass = req.body.password;
try {
const userFound = await UserModel.findOne({ pass: pass });
if (!userFound) {
return res.status(404).json({ message: 'Wrong password' });
}
const usersFound = await UserModel.find();
return res.status(200).json(usersFound);
} catch (err) {
return res.status(500).json({ message: 'Error when getting the user', error: err });
}
},
};