Sharkey/src/server/api/endpoints/channels/create.ts

36 lines
792 B
TypeScript
Raw Normal View History

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