Ejemplo n.º 1
0
        public async Task <IActionResult> CreateAsync(PlatformViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("Create", viewModel));
            }

            var dto = _mapper.Map <PlatformDto>(viewModel);

            try
            {
                await _platformServices.CreateAsync(dto);
            }
            catch (ValidationException <Platform> e)
            {
                ModelState.AddModelError(e.Key, e.Message);
                _logger.LogWarning(e.Message);

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

            _logger.LogDebug($"Create new platform with name {viewModel.Name}");

            return(RedirectToAction(nameof(GetAllAsync)));
        }
Ejemplo n.º 2
0
        public void CreateAsync_ReturnsView_WhenCatchException()
        {
            var          viewModel = CreatePlatformViewModel();
            const string key       = nameof(viewModel.Name);
            var          value     = viewModel.Name;

            A.CallTo(() => _platformService.CreateAsync(A <PlatformDto> ._))
            .Throws(new EntityExistsWithKeyValueException <Platform>(key, value));

            var result = _platformController.CreateAsync(viewModel).Result;

            result.Should().BeViewResult();
        }