rudeshark.net/src/api/endpoints/bbs/threads/create.ts

30 lines
655 B
TypeScript
Raw Normal View History

2017-10-30 09:30:32 +01:00
/**
* Module dependencies
*/
import $ from 'cafy';
2017-10-31 13:42:11 +01:00
import Channel from '../../../models/channel';
import serialize from '../../../serializers/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({
2017-10-30 09:30:32 +01:00
created_at: new Date(),
user_id: user._id,
title: title
});
// Response
2017-10-31 13:42:11 +01:00
res(await serialize(channel));
2017-10-30 09:30:32 +01:00
});