コード例 #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)));
        }
コード例 #2
0
        public bool Validate(ModelStateDictionary modelState, CreateTorrentWidgetModel createTorrentWidgetModel)
        {
            if (!modelState.IsValid)
            {
                return(false);
            }

            bool result = true;

            string imageFileExtension = _pathFacade.GetExtension(createTorrentWidgetModel.UserImageData.FileName).ToLower();

            if (!Constants.ValidImageFiles.Contains(imageFileExtension))
            {
                modelState.AddModelError(
                    nameof(CreateTorrentWidgetModel.UserImageData),
                    string.Format(Constants.InvalidFileFormatMessage, imageFileExtension, Constants.ImageText, string.Join("; ", Constants.ValidImageFiles)));

                result = false;
            }

            string documentFileExtension = _pathFacade.GetExtension(createTorrentWidgetModel.UserTorrentData.FileName).ToLower();

            if (!Constants.ValidDocumentFiles.Contains(documentFileExtension))
            {
                modelState.AddModelError(
                    nameof(CreateTorrentWidgetModel.UserTorrentData),
                    string.Format(Constants.InvalidFileFormatMessage, documentFileExtension, Constants.TorrentText, string.Join("; ", Constants.ValidDocumentFiles)));

                result = false;
            }

            return(result);
        }
コード例 #3
0
        public void AddErrorMessageToTempData_When_ModelStateIsValidAndTorrentModelIsNull()
        {
            _modelStateValidatorServiceStub.Setup(x => x.Validate(It.IsAny <ModelStateDictionary>(), It.IsAny <CreateTorrentWidgetModel>()))
            .Returns(true);

            CreateTorrentWidgetModel torrentWidgetModel = null;

            _controller.Index(torrentWidgetModel);

            Assert.IsTrue(_controller.TempData.ContainsKey(SitefinityWebApp.Constants.ErrorMessageKey));
        }
コード例 #4
0
        public void CallTaxonomyServiceToCreateGenres_When_ModelStateIsInvalid()
        {
            _modelStateValidatorServiceStub.Setup(x => x.Validate(It.IsAny <ModelStateDictionary>(), It.IsAny <CreateTorrentWidgetModel>()))
            .Returns(false);

            CreateTorrentWidgetModel torrentWidgetModel = CreateTorrentWidgetModel();

            _controller.Index(torrentWidgetModel);

            _taxonomyServiceStub.Verify(x => x.GetTaxonNamesByTaxonomy(SitefinityWebApp.Constants.GenresTaxonomyName), Times.Exactly(1));
        }
コード例 #5
0
        public void NotCallTorrentService_When_ModelStateIsInvalid()
        {
            _modelStateValidatorServiceStub.Setup(x => x.Validate(It.IsAny <ModelStateDictionary>(), It.IsAny <CreateTorrentWidgetModel>()))
            .Returns(false);

            CreateTorrentWidgetModel torrentWidgetModel = CreateTorrentWidgetModel();

            _controller.Index(torrentWidgetModel);

            _torrentServiceStub.Verify(x => x.CreateTorrentWithPublish(It.IsAny <CreateTorrentDto>()), Times.Exactly(0));
        }
コード例 #6
0
        public void NotAddSuccessMessageToTempData_When_ModelStateIsInvalid()
        {
            _modelStateValidatorServiceStub.Setup(x => x.Validate(It.IsAny <ModelStateDictionary>(), It.IsAny <CreateTorrentWidgetModel>()))
            .Returns(false);

            CreateTorrentWidgetModel torrentWidgetModel = CreateTorrentWidgetModel();

            _controller.Index(torrentWidgetModel);

            Assert.IsFalse(_controller.TempData.ContainsKey(SitefinityWebApp.Constants.SuccessMessageKey));
        }
コード例 #7
0
        public static CreateTorrentDto CreateTorrent(CreateTorrentWidgetModel model)
        {
            CreateImageDto    image    = CreateImage(model);
            CreateDocumentDto document = CreateDocument(model);

            return(new CreateTorrentDto
            {
                Title = model.Title,
                Description = model.Description,
                AdditionalInfo = model.AdditionalInfo,
                Genres = model.Genres,
                ImageDto = image,
                DocumentDto = document
            });
        }
コード例 #8
0
        private CreateTorrentWidgetModel CreateMockTorrentWidgetModel(string imageExtension, string torrentExtension)
        {
            _userImageDataStub.Setup(x => x.FileName).Returns(imageExtension);
            _userTorrentDataStub.Setup(x => x.FileName).Returns(torrentExtension);
            var torrentWidgetModel = new CreateTorrentWidgetModel()
            {
                UserImageData   = _userImageDataStub.Object,
                UserTorrentData = _userTorrentDataStub.Object
            };

            _pathFacadeStub.SetupSequence(x => x.GetExtension(It.IsAny <string>()))
            .Returns(torrentWidgetModel.UserImageData.FileName).Returns(torrentWidgetModel.UserTorrentData.FileName);

            return(torrentWidgetModel);
        }
コード例 #9
0
        private static TFileDto CreateFileDto <TFileDto>(CreateTorrentWidgetModel model,
                                                         string fileTitle,
                                                         Func <CreateTorrentWidgetModel, HttpPostedFileBase> fileDataFunc)
            where TFileDto : CreateFileDtoBase, new()
        {
            HttpPostedFileBase fileData = fileDataFunc(model);
            var fileDto = new TFileDto
            {
                Title         = $"{fileTitle}-{model.Title}",
                Stream        = fileData.InputStream,
                FileName      = fileData.FileName,
                FileExtension = Path.GetExtension(fileData.FileName)
            };

            return(fileDto);
        }
コード例 #10
0
        public static CreateDocumentDto CreateDocument(CreateTorrentWidgetModel model)
        {
            var documentDto = CreateFileDto <CreateDocumentDto>(model, Constants.TorrentFileTitle, x => x.UserTorrentData);

            return(documentDto);
        }
コード例 #11
0
        public static CreateImageDto CreateImage(CreateTorrentWidgetModel model)
        {
            var imageDto = CreateFileDto <CreateImageDto>(model, Constants.TorrentImageTitle, x => x.UserImageData);

            return(imageDto);
        }
コード例 #12
0
        public ActionResult Index()
        {
            CreateTorrentWidgetModel model = CreateInitialTorrentWidgetModel();

            return(View(model));
        }