2017-10-30 17:30:32 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import $ from 'cafy';
|
2017-10-31 22:35:31 +09:00
|
|
|
import Channel from '../../models/channel';
|
2017-11-01 19:33:08 +09:00
|
|
|
import Watching from '../../models/channel-watching';
|
2017-10-31 22:35:31 +09:00
|
|
|
import serialize from '../../serializers/channel';
|
2017-10-30 17:30:32 +09:00
|
|
|
|
|
|
|
/**
|
2017-10-31 21:42:11 +09:00
|
|
|
* Create a channel
|
2017-10-30 17:30:32 +09: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 21:42:11 +09:00
|
|
|
// Create a channel
|
|
|
|
const channel = await Channel.insert({
|
2017-10-30 17:30:32 +09:00
|
|
|
created_at: new Date(),
|
|
|
|
user_id: user._id,
|
2017-11-01 01:38:19 +09:00
|
|
|
title: title,
|
2017-11-01 19:33:08 +09:00
|
|
|
index: 0,
|
|
|
|
watching_count: 1
|
2017-10-30 17:30:32 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Response
|
2017-10-31 21:42:11 +09:00
|
|
|
res(await serialize(channel));
|
2017-11-01 19:33:08 +09:00
|
|
|
|
|
|
|
// Create Watching
|
|
|
|
await Watching.insert({
|
|
|
|
created_at: new Date(),
|
|
|
|
user_id: user._id,
|
|
|
|
channel_id: channel._id
|
|
|
|
});
|
2017-10-30 17:30:32 +09:00
|
|
|
});
|