summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/channels/create.ts
blob: 1dc453c4a5e09f10b8734fc7e19108ee88a4be02 (plain)
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
39
/**
 * Module dependencies
 */
import $ from 'cafy';
import Channel from '../../models/channel';
import Watching from '../../models/channel-watching';
import { pack } from '../../models/channel';

/**
 * Create a channel
 *
 * @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');

	// Create a channel
	const channel = await Channel.insert({
		createdAt: new Date(),
		userId: user._id,
		title: title,
		index: 0,
		watchingCount: 1
	});

	// Response
	res(await pack(channel));

	// Create Watching
	await Watching.insert({
		createdAt: new Date(),
		userId: user._id,
		channelId: channel._id
	});
});