You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
/**
|
|
* Mock database, replace this with your db models import, required to perform query to your database.
|
|
*/
|
|
const db = {
|
|
channels: [
|
|
{
|
|
channelId: 1,
|
|
phone: 'string',
|
|
formattedPhone: 'string',
|
|
profilePicture: 'string',
|
|
name: 'string',
|
|
connectStatus: 'string',
|
|
channelStatus: 'string',
|
|
createTime: 0,
|
|
},
|
|
],
|
|
};
|
|
function sleep(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
exports.getOne = async ctx => {
|
|
const { id } = ctx.params;
|
|
const channel = db.channels.find(channel => channel.channelId === Number(id));
|
|
await sleep(500);
|
|
ctx.assert(channel, 200, "The requested channel doesn't exist");
|
|
return channel;
|
|
};
|
|
|
|
exports.getAll = async ctx => {
|
|
return db.channels;
|
|
};
|
|
|
|
exports.createOne = async ctx => {
|
|
const { name } = ctx.request.body;
|
|
ctx.assert(name, 200, 'The channel info is malformed!');
|
|
const id = generateId();
|
|
const newChannel = {
|
|
id,
|
|
name,
|
|
timestamp: Date.now(),
|
|
};
|
|
db.channels.push(newChannel);
|
|
const createdChannel = db.channels.find(channel => channel.id === id);
|
|
return createdChannel;
|
|
};
|