public IActionResult ConvertExtensionVideo([FromBody] ConvertExtensionData data)
 {
     try
     {
         var userData   = jwtService.ParseData(this.User);
         var videoIndex = videoService.ConvertExtensionVideo(data, userData.UserId);
         return(Ok(videoIndex));
     }
     catch (ServiceException e)
     {
         return(BadRequest(e.Message));
     }
 }
        public VideoIndex ConvertExtensionVideo(ConvertExtensionData data, int userId)
        {
            var user = this.context.Users.SingleOrDefault(x => x.Id == userId);

            if (user == null)
            {
                throw new ServiceException("User Not Found!");
            }

            var extensionVid = this.context.ExtensionAddedVideos
                               .SingleOrDefault(x => x.Id == data.ExtensionVideoId);

            if (extensionVid == null)
            {
                throw new ServiceException("Extension Video Not Found!");
            }

            if (extensionVid.UserId != user.Id)
            {
                throw new ServiceException("Extension Vid Does Not Belong To You!");
            }

            if (data.ShouldAdd)
            {
                var newDirectory = context.Directories.SingleOrDefault(x => x.Id == data.ParentDirId);
                if (newDirectory == null)
                {
                    throw new ServiceException("New Directory Not Found!");
                }

                if (newDirectory.UserId != user.Id)
                {
                    throw new ServiceException("New Directory Does Not Belong To You!");
                }

                var ordersInfo = context.Directories
                                 .Select(x => new { id = x.Id, orders = x.Videos.Select(y => y.Order).ToArray() })
                                 .SingleOrDefault(x => x.id == newDirectory.Id);
                var order = ordersInfo.orders.Length == 0 ? 0 : ordersInfo.orders.Max() + 1;

                var desctiption = extensionVid.Name;
                if (extensionVid.Name.Length > 40)
                {
                    extensionVid.Name = extensionVid.Name.Substring(0, 37) + "...";
                }

                var isYouTube = extensionVid.Type == "youtube";
                var isVimeo   = extensionVid.Type == "vimeo";

                var video = new Video
                {
                    DirectoryId = newDirectory.Id,
                    Order       = order,
                    UserId      = user.Id,
                    Name        = extensionVid.Name,
                    Description = desctiption,
                    Url         = extensionVid.Url,
                    IsLocal     = false,
                    IsYouTube   = isYouTube,
                    IsVimeo     = isVimeo,
                    SeekTo      = 0,
                };

                context.Videos.Add(video);
                context.ExtensionAddedVideos.Remove(extensionVid);
                context.SaveChanges();
                var index = Mapper.Instance.Map <VideoIndex>(video);
                return(index);
            }
            else
            {
                context.ExtensionAddedVideos.Remove(extensionVid);
                context.SaveChanges();
                return(null);
            }
        }