private async Task<string> AddBundle(BundleUpdateViewModel model, string worldContentTypeId)
 {
     using (var repository = RepositoryFactory.GetInstance(Session))
     {
         var currencyRetriver = new CurrencyRetriver(HttpContext, Session, repository);
         var bundle = model.GetBundle();
         bundle.Teacher = ParseObject.CreateWithoutData<ParseUser>(model.TeacherId);
         bundle.Currency = currencyRetriver.GetCurrent().ConvertToDomain();
         bundle.ContentType = new WorldContentType();
         bundle.ContentType.ObjectId = worldContentTypeId;
         return await repository.AddBundle(bundle);
     }
 }
        private async Task UpdateBundle(BundleUpdateViewModel model)
        {
            using (var repository = RepositoryFactory.GetInstance(Session))
            {
                var currencyRetriver = new CurrencyRetriver(HttpContext, Session, repository);
                var existingBundle = await repository.FindBundleById(model.ObjectId);
                var bundle = model.GetBundle();
                var activeStatusString =
                    repository.FindClipStatuses()
                        .Single(o => o.Status == LessonStatus.Active.ToLower())
                        .GetLocalizedField("status");

                var statusChanged = existingBundle.Status.ObjectId != bundle.Status.ObjectId;
                if (statusChanged)
                {
                    var hasInactiveClips = model.BundleClips.Any(o => o.Status != activeStatusString);
                    ClipStatus newStatus = null;
                    newStatus = Task.Run(() => bundle.Status.FetchIfNeededAsync()).Result;


                    if (newStatus.Status == LessonStatus.Active.ToLower())
                    {
                        if (hasInactiveClips)
                        {
                            throw new StatusChangeException(MyMentorResources.updateBundleInactiveLessonInBundle);
                        }
                    }
                }
                bundle.Currency = currencyRetriver.GetCurrent().ConvertToDomain();
                await repository.UpdateBundle(bundle);
            }
        }
        public async Task<ActionResult> AddUpdateBundleData(BundleUpdateViewModel model)
        {
            var repository = RepositoryFactory.GetInstance(Session);
            try
            {
                var contentTypeRetriver = new WorldContentTypeRetriver(HttpContext,repository);
                var worldContentTypeId = contentTypeRetriver.GetWorldContentTypeId();

                var isNewBundle = model.ObjectId == null;
                var clipsIds = model.BundleClips.Select(o => o.Id).ToArray();
                if (!clipsIds.Any())
                {
                    return Json(new ServiceObjectResponse
                    {
                        Status = ServiceObjectResponse.Failure,
                        StatusReason = MyMentorResources.updateBundleNoLessonsSelected
                    });
                }

                ValidateBundlePrice(model);

                var teacher = await repository.FindUserByName(model.TeacherName);
                model.TeacherId = teacher.ObjectId;
                new NameManager(repository, Session).SetPortalBundleName(_worldId, model, teacher.ConvertToParseUserDto());

                if (isNewBundle)
                {
                    model.ObjectId = await AddBundle(model, worldContentTypeId);
                }
                else
                {
                    await UpdateBundle(model);
                }                
                lock (locker)
                {                    
                    Task.Run(() => repository.UpdateClipsInBundle(model.GetBundle(), clipsIds)).Wait();
                    var context = System.Web.HttpContext.Current;
                    new WebCacheProvider(context).ClearCachedItem(CacheKeys.ClipToBundle);                   
                }

                return Json(new ServiceObjectResponse
                {
                    Status = ServiceObjectResponse.Success,
                });
            }
            catch (Exception ex)
            {
                if (ex is DuplicateNameException ||
                    ex is StatusChangeException ||
                    ex is BundlePriceException)
                {
                    return Json(new ServiceObjectResponse
                    {
                        Status = ServiceObjectResponse.Failure,
                        StatusReason = ex.Message
                    });
                }

                mLogger.Log(LogLevel.Error, ex);
                return Json(new ServiceObjectResponse
                {
                    Status = ServiceObjectResponse.Failure,
                    StatusReason = MyMentorResources.errCanotAddBundleGeneral
                });
            }
            finally
            {
                repository.Dispose();
            }
        }