public async Task UploadImage_RedirectsToIndexAction()
        {
            int categoryIdTest = 1;
            var category       = new Category
            {
                CategoryId = categoryIdTest,
                Picture    = new byte[0]
            };
            var mockFile          = new Mock <IFormFile>();
            var categoryViewModel = new UploadCategoryImageViewModel()
            {
                CategoryId = categoryIdTest,
                ImageFile  = mockFile.Object
            };

            _mockDataRepository.Setup(repo => repo.GetCategoryByIdAsync(categoryIdTest))
            .Returns(Task.FromResult(category))
            .Verifiable();
            _mockDataRepository.Setup(m => m.UpdateCategoryAsync(category))
            .Verifiable();
            _mockDataRepository.Setup(m => m.CommitAsync())
            .Verifiable();
            var controller = new CategoriesController(_mockDataRepository.Object);

            var result = await controller.UploadImage(categoryViewModel);

            var redirectToActionResultResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal("Index", redirectToActionResultResult.ActionName);
            _mockDataRepository.Verify();
        }
        public async Task <IActionResult> UploadImage(UploadCategoryImageViewModel uploadCategoryImageViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(uploadCategoryImageViewModel));
            }

            var category = await _dataRepository.GetCategoryByIdAsync(uploadCategoryImageViewModel.CategoryId);

            if (category == null)
            {
                return(NotFound());
            }

            using (var memoryStream = new MemoryStream())
            {
                await uploadCategoryImageViewModel.ImageFile.CopyToAsync(memoryStream);

                category.Picture = memoryStream.ToArray();
            }

            await _dataRepository.UpdateCategoryAsync(category);

            await _dataRepository.CommitAsync();

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 3
0
        public IActionResult UploadImage(int id)
        {
            UploadCategoryImageViewModel model = new UploadCategoryImageViewModel();

            model.CategoryId     = id;
            model.CategoryName   = service.Category(id).CategoryName;
            model.ImageDataUrl   = service.GetCategoryImageAsDataUrl(id);
            model.UploadFileName = string.Empty;
            return(View(model));
        }
Exemplo n.º 4
0
        public IActionResult UploadImage(UploadCategoryImageViewModel model)
        {
            string fullPath = Path.Combine(Directory.GetCurrentDirectory() + "\\wwwroot\\images\\", model.UploadFileName ?? "NoPicture.jpg");

            if (!System.IO.File.Exists(fullPath))
            {
                ModelState.AddModelError("UploadFileName", "File not found.");
                return(View(model));
            }
            service.CategoryUploadJpegImage(model.CategoryId, fullPath);
            return(new RedirectToActionResult("Index", "Categories", null));
        }
        public async Task <IActionResult> UploadImage(UploadCategoryImageViewModel model)
        {
            var image = await model.File
                        .DumpToMemoryBufferAsync(ModelState, _categoryImageSizeLimit, new[] { _allowedCategoryImageExtension });

            if (!ModelState.IsValid)
            {
                return(View("UploadImage", model));
            }

            var command = new UpdateCategoryImageCommand
            {
                CategoryId = model.CategoryId,
                ImageBytes = image,
            };

            await _mediator.Send(command);

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            var query    = new GetSingleCategoryQuery(id.Value);
            var category = await _mediator.Send(query);

            var viewModel = new UploadCategoryImageViewModel
            {
                CategoryId           = category.CategoryId,
                CategoryName         = category.CategoryName,
                FileSizeLimitMb      = _categoryImageSizeLimit / 10000000,
                AllowedFileExtension = _allowedCategoryImageExtension,
            };

            return(View("UploadImage", viewModel));
        }