Example #1
0
        public async Task <IActionResult> Edit(LocationDetailsVM model)
        {
            var actionName = "Locations - Edit(Post):";

            try
            {
                _logger.LogInformation($"{actionName} Called");
                if (model is null)
                {
                    _logger.LogWarning($"{actionName} Bad Request - empty model");
                    return(RedirectToPage("/BadRequest"));
                }

                var item = _mapper.Map <LocationDetailsVM, Location>(model);

                var success = await _repo.UpdateAsync(item);

                if (!success)
                {
                    _logger.LogError($"{actionName} Failed");
                    return(RedirectToPage("/InternalServerError"));
                }

                _logger.LogInformation($"{actionName} Success");
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                _logger.LogError($"{actionName} Failed - {ex}");
                return(RedirectToPage("/InternalServerError"));
            }
        }
Example #2
0
        public async Task <IActionResult> Create(LocationDetailsVM model)
        {
            var actionName = "Locations - Create(Post):";

            try
            {
                _logger.LogInformation($"{actionName} Called");
                if (model is null)
                {
                    _logger.LogWarning($"{actionName} Failed - empty model");
                    return(RedirectToPage("/BadRequest"));
                }
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("x", "Please fix validation errors");
                    return(View("Create", model));
                }

                var item    = _mapper.Map <LocationDetailsVM, Location>(model);
                var success = await _repo.CreateAsync(item);

                if (!success)
                {
                    _logger.LogError($"{actionName} Failed");
                    return(RedirectToPage("/InternalServerError"));
                }

                _logger.LogInformation($"{actionName} Success");
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                _logger.LogError($"{actionName} Failed - {ex}");
                return(RedirectToPage("/InternalServerError"));
            }
        }