Example #1
0
        public async Task <IActionResult> Delete(int id)
        {
            try
            {
                _logger.LogInformation($"Deleting book with id of {id}");

                using (_booksManager)
                {
                    var currentData = await _booksManager.GetByIdAsync(id);

                    if (currentData == null)
                    {
                        _logger.LogWarning($"Can't find book with id of {id}");
                        return(NotFound());
                    }

                    var deleteResult = await _booksManager.DeleteItemAsync(currentData.Id);

                    if (deleteResult.Status == RepositoryActionStatus.Deleted)
                    {
                        _logger.LogInformation("Book deleted successfully.");
                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        _logger.LogWarning("Could not delete book from the database !");
                        return(BadRequest());
                    }
                }
            }
            catch (ValidationException ex)
            {
                _logger.LogWarning($"validation exception while delete book : {ex.ValidationResult.ErrorMessage}");
                return(BadRequest());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Throw exception while delete book : {ex}");
                throw ex;
            }
        }