From eb08047657e739bcd9e592d76307befa3998482b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 25 Oct 2017 11:55:06 +0200 Subject: Use async/await in controllers --- server/lib/video-channel.ts | 46 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'server/lib/video-channel.ts') diff --git a/server/lib/video-channel.ts b/server/lib/video-channel.ts index 224179973..678ffe643 100644 --- a/server/lib/video-channel.ts +++ b/server/lib/video-channel.ts @@ -2,12 +2,11 @@ import * as Sequelize from 'sequelize' import { addVideoChannelToFriends } from './friends' import { database as db } from '../initializers' +import { logger } from '../helpers' import { AuthorInstance } from '../models' import { VideoChannelCreate } from '../../shared/models' -function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) { - let videoChannelUUID = '' - +async function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: AuthorInstance, t: Sequelize.Transaction) { const videoChannelData = { name: videoChannelInfo.name, description: videoChannelInfo.description, @@ -18,25 +17,34 @@ function createVideoChannel (videoChannelInfo: VideoChannelCreate, author: Autho const videoChannel = db.VideoChannel.build(videoChannelData) const options = { transaction: t } - return videoChannel.save(options) - .then(videoChannelCreated => { - // Do not forget to add Author information to the created video channel - videoChannelCreated.Author = author - videoChannelUUID = videoChannelCreated.uuid - - return videoChannelCreated - }) - .then(videoChannel => { - const remoteVideoChannel = videoChannel.toAddRemoteJSON() - - // Now we'll add the video channel's meta data to our friends - return addVideoChannelToFriends(remoteVideoChannel, t) - }) - .then(() => videoChannelUUID) // Return video channel UUID + const videoChannelCreated = await videoChannel.save(options) + + // Do not forget to add Author information to the created video channel + videoChannelCreated.Author = author + + const remoteVideoChannel = videoChannelCreated.toAddRemoteJSON() + + // Now we'll add the video channel's meta data to our friends + await addVideoChannelToFriends(remoteVideoChannel, t) + + return videoChannelCreated +} + +async function fetchVideoChannelByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) { + try { + const videoChannel = await db.VideoChannel.loadByHostAndUUID(podHost, uuid, t) + if (!videoChannel) throw new Error('Video channel not found') + + return videoChannel + } catch (err) { + logger.error('Cannot load video channel from host and uuid.', { error: err.stack, podHost, uuid }) + throw err + } } // --------------------------------------------------------------------------- export { - createVideoChannel + createVideoChannel, + fetchVideoChannelByHostAndUUID } -- cgit v1.2.3