2017-10-30 09:30:32 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import $ from 'cafy';
|
2017-10-31 14:35:31 +01:00
|
|
|
import Channel from '../../models/channel';
|
2017-11-01 11:33:08 +01:00
|
|
|
import Watching from '../../models/channel-watching';
|
2018-02-02 00:21:30 +01:00
|
|
|
import { pack } from '../../models/channel';
|
2017-10-30 09:30:32 +01:00
|
|
|
|
|
|
|
/**
|
2017-10-31 13:42:11 +01:00
|
|
|
* Create a channel
|
2017-10-30 09:30:32 +01:00
|
|
|
*
|
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
|
|
|
// Get 'title' parameter
|
|
|
|
const [title, titleErr] = $(params.title).string().range(1, 100).$;
|
|
|
|
if (titleErr) return rej('invalid title param');
|
|
|
|
|
2017-10-31 13:42:11 +01:00
|
|
|
// Create a channel
|
|
|
|
const channel = await Channel.insert({
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user._id,
|
2017-10-31 17:38:19 +01:00
|
|
|
title: title,
|
2017-11-01 11:33:08 +01:00
|
|
|
index: 0,
|
2018-03-29 07:48:47 +02:00
|
|
|
watchingCount: 1
|
2017-10-30 09:30:32 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
2018-02-02 00:21:30 +01:00
|
|
|
res(await pack(channel));
|
2017-11-01 11:33:08 +01:00
|
|
|
|
|
|
|
// Create Watching
|
|
|
|
await Watching.insert({
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user._id,
|
|
|
|
channelId: channel._id
|
2017-11-01 11:33:08 +01:00
|
|
|
});
|
2017-10-30 09:30:32 +01:00
|
|
|
});
|