Example #1
0
        public ActionResult Index(CreateTorrentWidgetModel model)
        {
            if (!_modelStateValidatorService.Validate(ModelState, model))
            {
                // quick solution to populate genres when invalid data is passed on Add torrent
                var genres = _taxonomyService.GetTaxonNamesByTaxonomy(Constants.GenresTaxonomyName);
                model.Genres = genres;
                return(View(model));
            }

            StringBuilder sb = new StringBuilder();

            try
            {
                CreateTorrentDto torrent = DtoCreator.CreateTorrent(model);
                _torrentService.CreateTorrentWithPublish(torrent);

                sb.AppendLine(string.Format(Constants.CreateTorrentSuccessMessage, model.Title));
                TempData.Add(Constants.SuccessMessageKey, sb.ToString());
            }
            catch (Exception exc)
            {
                sb.AppendLine(exc.Message);

                if (exc.InnerException != null)
                {
                    sb.AppendLine(exc.InnerException.Message);
                }

                TempData.Add(Constants.ErrorMessageKey, sb.ToString());
            }

            return(RedirectToAction(nameof(CreateTorrentWidgetController.Index)));
        }
Example #2
0
        public void CreateTorrentWithPublish(CreateTorrentDto createTorrentDto)
        {
            // Set the provider name for the DynamicModuleManager here. All available providers are listed in
            // Administration -> Settings -> Advanced -> DynamicModules -> Providers
            var providerName = "OpenAccessProvider";

            // Set a transaction name and get the version manager
            var transactionName = "someTransactionName";
            var versionManager  = VersionManager.GetManager(null, transactionName);

            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName, transactionName);
            Type           torrentType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Torrents.Torrent");
            DynamicContent torrentItem = dynamicModuleManager.CreateDataItem(torrentType);

            // This is how values for the properties are set
            torrentItem.SetValue("AdditionalInfo", createTorrentDto.AdditionalInfo);
            torrentItem.SetValue("Description", createTorrentDto.Description);
            torrentItem.SetValue("Title", createTorrentDto.Title);
            torrentItem.SetValue("CreationDate", DateTime.Now);

            List <Guid> taxonIds = _taxonomyService.GetTaxonIdsByTaxonomy(createTorrentDto.Genres, Constants.GenresTaxonomyName);

            if (taxonIds.Any())
            {
                torrentItem.Organizer.AddTaxa(Constants.GenresTaxonomyName, taxonIds.ToArray());
            }

            Image imageFileItem = _imageService.CreateImageWithNativeAPI(createTorrentDto.ImageDto);

            if (imageFileItem != null)
            {
                // This is how we relate an item
                torrentItem.CreateRelation(imageFileItem, "ImageFile");
            }

            Document torrentFileItem = _documentService.CreateDocumentNativeAPI(createTorrentDto.DocumentDto);

            if (torrentFileItem != null)
            {
                // This is how we relate an item
                torrentItem.CreateRelation(torrentFileItem, "TorrentFile");
            }

            torrentItem.SetString("UrlName", $"{createTorrentDto.Title}{torrentItem.Id}");
            torrentItem.SetValue("Owner", ClaimsManager.GetCurrentIdentity().UserId);
            torrentItem.SetValue("PublicationDate", DateTime.UtcNow);

            torrentItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");

            // Create a version and commit the transaction in order changes to be persisted to data store
            versionManager.CreateVersion(torrentItem, false);

            // We can now call the following to publish the item
            ILifecycleDataItem publishedTorrentItem = dynamicModuleManager.Lifecycle.Publish(torrentItem);

            // You need to set appropriate workflow status
            torrentItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");

            // Create a version and commit the transaction in order changes to be persisted to data store
            versionManager.CreateVersion(torrentItem, true);

            // Now the item is published and can be seen in the page

            // Commit the transaction in order for the items to be actually persisted to data store
            TransactionManager.CommitTransaction(transactionName);
        }