ChatHistory behaved like controller already, renamed.
This commit is contained in:
33
api/controllers/chatController.js
Normal file
33
api/controllers/chatController.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const path = require("path");
|
||||
const { history, clearHistory } = require(path.join(__dirname + "/../api/redis"));
|
||||
|
||||
const getAllHistory = (req, res) => {
|
||||
let { page, limit } = req.query;
|
||||
page = !isNaN(page) ? Number(page) : undefined;
|
||||
limit = !isNaN(limit) ? Number(limit) : undefined;
|
||||
|
||||
return history(page, limit)
|
||||
.then(messages => res.json(messages))
|
||||
.catch(error =>
|
||||
res.status(500).json({
|
||||
message: error.message,
|
||||
success: false
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const deleteHistory = (req, res) => {
|
||||
return clearHistory()
|
||||
.then(message => res.json(message))
|
||||
.catch(error =>
|
||||
res.status(500).json({
|
||||
message: error.message,
|
||||
success: false
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getAllHistory,
|
||||
deleteHistory
|
||||
};
|
||||
49
api/controllers/userController.js
Normal file
49
api/controllers/userController.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const path = require("path");
|
||||
const userRepository = require(path.join(__dirname, "../user"));
|
||||
|
||||
function register(req, res, next) {
|
||||
const { username, password } = req.body;
|
||||
|
||||
return userRepository
|
||||
.register(username, password)
|
||||
.then(user => userRepository.login(req, user))
|
||||
.then(_ =>
|
||||
res.send({
|
||||
messsage: `Bruker registrert. Velkommen ${username}`,
|
||||
success: true
|
||||
})
|
||||
)
|
||||
.catch(error => {
|
||||
const { statusCode, message } = error;
|
||||
|
||||
return res.status(statusCode || 500).send({
|
||||
message: message || "Unable to sign in with given username and passowrd",
|
||||
success: false
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const login = (req, res, next) => {
|
||||
return userRepository
|
||||
.authenticate(req)
|
||||
.then(user => userRepository.login(req, user))
|
||||
.then(user => {
|
||||
res.send({
|
||||
message: `Velkommen ${user.username}`,
|
||||
success: true
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
const { statusCode, message } = error;
|
||||
|
||||
return res.status(statusCode || 500).send({
|
||||
message: message || "Unable to sign in with given username and passowrd",
|
||||
success: false
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
login
|
||||
};
|
||||
Reference in New Issue
Block a user