From 90f8fe7e538bb7e52d2558152a0390e693f39b11 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Thu, 29 Mar 2018 01:20:40 +0900 Subject: Introduce processor --- src/api/endpoints/channels/create.ts | 39 ------------------ src/api/endpoints/channels/posts.ts | 78 ----------------------------------- src/api/endpoints/channels/show.ts | 30 -------------- src/api/endpoints/channels/unwatch.ts | 60 --------------------------- src/api/endpoints/channels/watch.ts | 58 -------------------------- 5 files changed, 265 deletions(-) delete mode 100644 src/api/endpoints/channels/create.ts delete mode 100644 src/api/endpoints/channels/posts.ts delete mode 100644 src/api/endpoints/channels/show.ts delete mode 100644 src/api/endpoints/channels/unwatch.ts delete mode 100644 src/api/endpoints/channels/watch.ts (limited to 'src/api/endpoints/channels') diff --git a/src/api/endpoints/channels/create.ts b/src/api/endpoints/channels/create.ts deleted file mode 100644 index 695b4515b3..0000000000 --- a/src/api/endpoints/channels/create.ts +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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} - */ -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({ - created_at: new Date(), - user_id: user._id, - title: title, - index: 0, - watching_count: 1 - }); - - // Response - res(await pack(channel)); - - // Create Watching - await Watching.insert({ - created_at: new Date(), - user_id: user._id, - channel_id: channel._id - }); -}); diff --git a/src/api/endpoints/channels/posts.ts b/src/api/endpoints/channels/posts.ts deleted file mode 100644 index d722589c20..0000000000 --- a/src/api/endpoints/channels/posts.ts +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import { default as Channel, IChannel } from '../../models/channel'; -import Post, { pack } from '../../models/post'; - -/** - * Show a posts of a channel - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 1000, limitErr] = $(params.limit).optional.number().range(1, 1000).$; - if (limitErr) return rej('invalid limit param'); - - // Get 'since_id' parameter - const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$; - if (sinceIdErr) return rej('invalid since_id param'); - - // Get 'until_id' parameter - const [untilId, untilIdErr] = $(params.until_id).optional.id().$; - if (untilIdErr) return rej('invalid until_id param'); - - // Check if both of since_id and until_id is specified - if (sinceId && untilId) { - return rej('cannot set since_id and until_id'); - } - - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - // Fetch channel - const channel: IChannel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - - //#region Construct query - const sort = { - _id: -1 - }; - - const query = { - channel_id: channel._id - } as any; - - if (sinceId) { - sort._id = 1; - query._id = { - $gt: sinceId - }; - } else if (untilId) { - query._id = { - $lt: untilId - }; - } - //#endregion Construct query - - // Issue query - const posts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(posts.map(async (post) => - await pack(post, user) - ))); -}); diff --git a/src/api/endpoints/channels/show.ts b/src/api/endpoints/channels/show.ts deleted file mode 100644 index 332da64675..0000000000 --- a/src/api/endpoints/channels/show.ts +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel, { IChannel, pack } from '../../models/channel'; - -/** - * Show a channel - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - // Fetch channel - const channel: IChannel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - - // Serialize - res(await pack(channel, user)); -}); diff --git a/src/api/endpoints/channels/unwatch.ts b/src/api/endpoints/channels/unwatch.ts deleted file mode 100644 index 19d3be118a..0000000000 --- a/src/api/endpoints/channels/unwatch.ts +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel from '../../models/channel'; -import Watching from '../../models/channel-watching'; - -/** - * Unwatch a channel - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - //#region Fetch channel - const channel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - //#endregion - - //#region Check whether not watching - const exist = await Watching.findOne({ - user_id: user._id, - channel_id: channel._id, - deleted_at: { $exists: false } - }); - - if (exist === null) { - return rej('already not watching'); - } - //#endregion - - // Delete watching - await Watching.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() - } - }); - - // Send response - res(); - - // Decrement watching count - Channel.update(channel._id, { - $inc: { - watching_count: -1 - } - }); -}); diff --git a/src/api/endpoints/channels/watch.ts b/src/api/endpoints/channels/watch.ts deleted file mode 100644 index 030e0dd411..0000000000 --- a/src/api/endpoints/channels/watch.ts +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Channel from '../../models/channel'; -import Watching from '../../models/channel-watching'; - -/** - * Watch a channel - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => new Promise(async (res, rej) => { - // Get 'channel_id' parameter - const [channelId, channelIdErr] = $(params.channel_id).id().$; - if (channelIdErr) return rej('invalid channel_id param'); - - //#region Fetch channel - const channel = await Channel.findOne({ - _id: channelId - }); - - if (channel === null) { - return rej('channel not found'); - } - //#endregion - - //#region Check whether already watching - const exist = await Watching.findOne({ - user_id: user._id, - channel_id: channel._id, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return rej('already watching'); - } - //#endregion - - // Create Watching - await Watching.insert({ - created_at: new Date(), - user_id: user._id, - channel_id: channel._id - }); - - // Send response - res(); - - // Increment watching count - Channel.update(channel._id, { - $inc: { - watching_count: 1 - } - }); -}); -- cgit v1.2.3-freya