Implement Channel Watching
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import Channel from '../../models/channel';
|
||||
import Watching from '../../models/channel-watching';
|
||||
import serialize from '../../serializers/channel';
|
||||
|
||||
/**
|
||||
@@ -22,9 +23,17 @@ module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
created_at: new Date(),
|
||||
user_id: user._id,
|
||||
title: title,
|
||||
index: 0
|
||||
index: 0,
|
||||
watching_count: 1
|
||||
});
|
||||
|
||||
// Response
|
||||
res(await serialize(channel));
|
||||
|
||||
// Create Watching
|
||||
await Watching.insert({
|
||||
created_at: new Date(),
|
||||
user_id: user._id,
|
||||
channel_id: channel._id
|
||||
});
|
||||
});
|
||||
|
60
src/api/endpoints/channels/unwatch.ts
Normal file
60
src/api/endpoints/channels/unwatch.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 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<any>}
|
||||
*/
|
||||
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
|
||||
}
|
||||
});
|
||||
});
|
58
src/api/endpoints/channels/watch.ts
Normal file
58
src/api/endpoints/channels/watch.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 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<any>}
|
||||
*/
|
||||
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
|
||||
}
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user