-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrollers.js
38 lines (27 loc) · 953 Bytes
/
controllers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const dataAccess = require('./data-access');
const services = require('./services');
const getChannels = async (_req, res) => {
const channels = await dataAccess.getChannels();
return res.status(200).json({ channels });
};
const createChannel = async (req, res) => {
const { name } = req.body;
const channelId = await services.createChannelAndGetId(name);
return res.status(201).send(`Channel added with ID: ${channelId}`);
};
const getMessagesByChannelId = async (req, res) => {
const { channelId } = req.params;
const messages = await dataAccess.getMessagesByChannel(channelId);
return res.status(200).json({ messages });
};
const createMessage = async (req, res) => {
const { text, channelId, userId } = req.body;
await dataAccess.createMessage(text, channelId, userId);
return res.status(201).send('Message added');
};
module.exports = {
getChannels,
createChannel,
getMessagesByChannelId,
createMessage,
};