public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _loggerService.LogWarn($"{location}: Author with id: {id} update attempted.");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    _loggerService.LogWarn($"{location}: Author update failed with bad data.");
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    _loggerService.LogWarn($"{location}: Author Data was incomplete.");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Update operation failed."));
                }
                _loggerService.LogInfo($"{location}: Author with id: {id} sucessfully updated.");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Update(int Id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo("Attempt Author Update");
                if (Id < 1 || authorDTO == null || Id != authorDTO.Id)
                {
                    _logger.LogWarn("Emtpy request was submitted");
                    return(BadRequest());
                }
                var Exists = await _authorRepository.isExists(Id);

                if (!Exists)
                {
                    return(NotFound());
                }
                var author = await _authorRepository.FindById(Id);

                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"Author Update  failed"));
                }
                _logger.LogInfo("Author Updated");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo($"Author Update attempted");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var isExists = await _authorRepository.IsExists(id);

                if (!isExists)
                {
                    _logger.LogWarn("Id not found");
                    return(NotFound());
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"Author update failed"));
                }
                _logger.LogInfo($"Successfully updated author");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
Beispiel #4
0
        public void Execute(AuthorUpdateDTO request)
        {
            var author = Context.Authors
                         .Where(a => a.Id == request.Id && a.DeletedAt == null)
                         .FirstOrDefault();

            if (author == null)
            {
                throw new EntityNotFoundException("Author", request.Id);
            }

            if (request.FirstName != null)
            {
                if (author.FirstName != request.FirstName)
                {
                    author.FirstName = request.FirstName;
                    author.UpdatedAt = DateTime.Now;
                }
            }

            if (request.LastName != null)
            {
                if (author.LastName != request.LastName)
                {
                    author.LastName  = request.LastName;
                    author.UpdatedAt = DateTime.Now;
                }
            }

            Context.SaveChanges();
        }
Beispiel #5
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorUpdateDTO)
        {
            try
            {
                _logger.LogInfo($"End user attempted to update an existing Author with ID number: {id}.");
                if (id < 1 || authorUpdateDTO == null || id != authorUpdateDTO.Id)
                {
                    _logger.LogWarn($"End user sent an HTTP 1.1 POST request to create a new Author with invalid Author data!");
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorUpdateDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"Failed updating Author!"));
                }
                _logger.LogWarn($"Successfully updated Author with ID number {id}!");
                return(NoContent());
            }
            catch (Exception exception)
            {
                return(InternalError($"{exception.Message} - {exception.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorUpdate)
        {
            var location = GetControllerNames();

            try
            {
                _logger.LogInfo($"{location}: Update Attempted on record with id: {id}");
                if (id < 1 || authorUpdate == null || id != authorUpdate.Id)
                {
                    _logger.LogWarn($"{location}: Update failed with bad data");
                    return(BadRequest());
                }
                var isExist = await _authorRepository.IsExists(id);

                if (!isExist)
                {
                    _logger.LogWarn($"{location}: Failed to retrieve record with id: {id}");
                    return(BadRequest());
                }
                var author    = _mapper.Map <Author>(authorUpdate);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Update failed"));
                }
                _logger.LogWarn($"{location}: Record with id: {id} successfully updated");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> UpdateAuthor(int id, [FromBody] AuthorUpdateDTO dto)
        {
            _logger.LogInfo($"Update author entered with id: {id}");
            try
            {
                if (id < 1 || dto == null || dto.Id != id)
                {
                    _logger.LogWarn($"Update author with bad data id: {id}");
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Update author with bad ModelState: {ModelState}");
                    return(BadRequest(ModelState));
                }

                var author  = _mapper.Map <Author>(dto);
                var success = await _authorRepository.Update(author);

                if (!success)
                {
                    _logger.LogError($"Update author returned unsuccessful: {id}");
                    return(InternalError($"UpdateAuthor method failed - {success}"));
                }
                _logger.LogInfo($"Update author succeeded for id {id}");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError(e.Message));
            }
        }
Beispiel #8
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                Info($"Author update attempted");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    Warn($"Empty request or id < 1 was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    Warn($"Invalid modelstate '{ModelState}'");
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"Author {id} '{author}' update failed."));
                }
                Info($"Author updated");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError(e));
            }
        }
Beispiel #9
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    return(BadRequest());
                }

                var isExist = await _authorRepository.isExists(id);

                if (!isExist)
                {
                    return(NotFound());
                }


                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"Update Operation Failed"));
                }
                return(NoContent());
            }catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
        public async Task <ActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    return(BadRequest());
                }
                loggerService.LogInfo("Author submission submitted");
                if (authorDTO == null)
                {
                    loggerService.LogWarning("empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    loggerService.LogWarning("Author data was Incomplete");
                    return(BadRequest(ModelState));
                }
                var author    = mapper.Map <Author>(authorDTO);
                var isSuccess = await authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError("Author Updation Failed"));
                }
                loggerService.LogInfo("Author Updated successfully");
                return(NoContent());
            }
            catch (Exception ex)
            {
                return(InternalError($"{ex.Message} - {ex.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"Update operation failed"));
                }
                _logger.logWarn($"Author with id: {id} successfully updated");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo("Update started");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    _logger.LogWarn("Empty request");
                    return(BadRequest());
                }
                var isExists = await _authorRepository.IsExists(id);

                if (!isExists)
                {
                    _logger.LogWarn("Author does not exists");
                    return(BadRequest());
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError("Author update failed"));
                }
                _logger.LogInfo("Author updated");
                return(NoContent());
            }
            catch (Exception ex)
            {
                return(InternalError($"Something went wrong: {ex.Message}"));
            }
        }
Beispiel #13
0
        public async Task <IActionResult> UpdatePut(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo($"Author Update attempted");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    _logger.LogInfo($"Id {id} does not exist! Or ");
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogInfo($"Author data incomplete or not accepted");
                    return(BadRequest(ModelState));
                }
                var authorUpdate = _map.Map <Author>(authorDTO);
                var isSuccess    = await _authorRepository.Update(authorUpdate);

                if (!isSuccess)
                {
                    return(internalError($"Author update failed"));
                }

                return(NoContent());
            }
            catch (Exception e)
            {
                return(internalError($"{e.Message}-{e.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    return(BadRequest(ModelState));
                }
                var isExist = await _authorRepository.isExist(id);

                if (!isExist)
                {
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(StatusCode(500));
                }

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(StatusCode(500));
            }
        }
Beispiel #15
0
        public async Task <ActionResult> UpdateAuthorById(int id, AuthorUpdateDTO authorUpdateDTO)
        {
            try
            {
                var author = await _db.Authors.GetById(id);

                if (author == null)
                {
                    _logger.LogError($"PUT api/authors/{id} - Not Found");
                    return(NotFound());
                }

                _mapper.Map(authorUpdateDTO, author);
                await _db.Authors.Update(author);

                await _db.Save();

                _logger.LogInformation($"PUT api/authors/{id} - No Content - Author updated");
                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError("GET api/authors - Problem with Database");
                return(StatusCode(500, "Internal Server Error. Cannot connect wiht Database!"));
            }
        }
Beispiel #16
0
        public async Task <IActionResult> UpdateAuthor(int Id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                _logger.LogInfo($"Author with Id: {Id} update attempted");
                if (Id < 0 || authorDTO == null || Id != authorDTO.Id)
                {
                    _logger.LogWarn($"An empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(ErrorResult("Author update failed"));
                }
                return(NoContent());
            }
            catch (Exception e)
            {
                return(ErrorResult($"{e.Message} - {e.InnerException}"));
            }
        }
Beispiel #17
0
        public IActionResult UpdateAuthor(Guid authorId, [FromBody] AuthorUpdateDTO author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            var authorEntity = _authorRepository.GetAuthor(authorId);

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

            var authorUpdateEntity = _mapper.Map <Author>(author);

            _authorRepository.UpdateAuthor(authorUpdateEntity);

            if (!_authorRepository.Save())
            {
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }

            return(CreatedAtRoute("GetAuthor", new { authorId = authorUpdateEntity.Id }, authorUpdateEntity));
        }
        public IActionResult UpdateAuthor(AuthorUpdateDTO author, Guid authorID)
        {
            try
            {
                authorService.UpdateAuthor(author, authorID);

                return(NoContent());
            }
            catch
            {
                return(NotFound());
            }
        }
Beispiel #19
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                this.loggerService.LogInfo($"{this.GetControllerActionNames()}: Update attempted on record with Id: {id}");

                if (id <= 0 || id != authorDTO.Id)
                {
                    this.loggerService.LogWarning($"{this.GetControllerActionNames()}: Update failed with bad data - Id: {id}");
                    return(BadRequest("Invalid author identifier passed through."));
                }

                if (authorDTO == null)
                {
                    this.loggerService.LogWarning($"{this.GetControllerActionNames()}: Data was empty");
                    return(BadRequest(ModelState));
                }

                if (!ModelState.IsValid)
                {
                    this.loggerService.LogWarning($"Author properties are not valid.");
                    return(BadRequest(ModelState));
                }

                var doesExist = await this.authorRepository.DoesExist(id);

                if (!doesExist)
                {
                    return(NotFound($"{this.GetControllerActionNames()}: No record found with Id: {id}"));
                }

                var author    = this.mapper.Map <Author>(authorDTO);
                var isSuccess = await this.authorRepository.Update(author);

                if (isSuccess)
                {
                    this.loggerService.LogInfo($"{this.GetControllerActionNames()}: Update successful for record with Id: {id}");
                    return(NoContent());
                }
                else
                {
                    this.loggerService.LogError($"{this.GetControllerActionNames()}: Update failed for record with Id: {id}");
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Author update failed."));
                }
            }
            catch (Exception exc)
            {
                return(this.InternalError(exc));
            }
        }
Beispiel #20
0
        public async Task <ActionResult> UpdateAuthor(int id, [FromBody] AuthorUpdateDTO authorUpdateDTO)
        {
            var author = await _authorRepository.GetByIdAsync(id);

            if (author == null)
            {
                return(NotFound(new ApiResponse(404)));
            }

            _mapper.Map(authorUpdateDTO, author);
            await _authorRepository.Update(author);

            return(NoContent());
        }
Beispiel #21
0
 public IActionResult Put(int id, [FromBody] AuthorUpdateDTO author)
 {
     try
     {
         author.Id = id;
         updateCommand.Execute(author);
         return(NoContent());
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(new ErrorMessage {
             Message = e.Message
         }));
     }
 }
Beispiel #22
0
        public async Task <IActionResult> Update(int Id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            string location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Update attempted on record with Id: {Id}.");

                if (Id < 1 || authorDTO == null || Id != authorDTO.Id)
                {
                    _logger.LogWarn($"{location}: Update failed with bad data - Id: {Id}.");

                    return(BadRequest());
                }

                bool isExists = await _authorRepository.isExists(Id);

                if (!isExists)
                {
                    _logger.LogWarn($"{location}: Failed to retrieve record with Id: {Id}.");

                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}: Data was incomplete.");

                    return(BadRequest(ModelState));
                }

                Author author    = _mapper.Map <Author>(authorDTO);
                bool   isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Update failed for record with Id: {Id}."));
                }

                _logger.LogInfo($"{location}: Record with Id: {Id} successfully updated.");

                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Beispiel #23
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorUpdateDTO)
        {
            var controllerAction = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{controllerAction}: Attempted For ID {id}");

                if (id < 1 || authorUpdateDTO == null || id != authorUpdateDTO.ID)
                {
                    _logger.LogWarn($"{controllerAction}: Missing Data Or Invalid ID");
                    return(BadRequest(ModelState));
                }

                var exists = await _authorRepository.Exists(id);

                if (!exists)
                {
                    _logger.LogWarn($"{controllerAction}: ID {id} Not Found");
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{controllerAction}: Invalid Or Incomplete Data Submitted");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorUpdateDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"{controllerAction}: Failed"));
                }

                _logger.LogInfo($"{controllerAction}: Successful For ID {id}");
                return(NoContent());
            }

            catch (Exception e)
            {
                return(InternalError($"{controllerAction}: {e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorUpdateDTO)
        {
            var location = GetControllerActionname();

            try
            {
                _logger.LogInfo($"{location}: Attempting to update record with id : {id}");

                if (authorUpdateDTO is null || id < 1 || id != authorUpdateDTO.Id)
                {
                    _logger.LogInfo($"{location}: Update failed with bad data - id: {id}");
                    return(BadRequest());
                }

                var isExists = await _authorRepository.Exists(id);

                if (!isExists)
                {
                    _logger.LogWarn($"{location}: The record with id:{id} doesn't exists");
                    return(NotFound());
                }

                if (ModelState.IsValid == false)
                {
                    _logger.LogWarn($"{location}: The ModelState record with id: {id} is not valid");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorUpdateDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (isSuccess == false)
                {
                    _logger.LogInfo($"{location}: Record coudn't be updated");
                    return(StatusCode(500, "The record was not updated, please contact the administrator"));
                }

                _logger.LogInfo($"{location}: Record successfully updated");
                return(Ok(isSuccess));
            }
            catch (Exception e)
            {
                return(InternalError(e, location));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO bookDTO)
        {
            var location = GetControllerNames();

            try
            {
                _logger.LogInfo($"{location}: Book update start");
                if (id < 1 || bookDTO == null || id != bookDTO.Id)
                {
                    _logger.LogWarning($"{location} : Author update failed with Bad data");
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarning($"{location}: Book update failed with Metadata data");
                    return(BadRequest(ModelState));
                }

                var isExists = await _bookRepository.IsExists(id);

                if (!isExists)
                {
                    _logger.LogInfo($"{location} : Book update record not found");
                    return(NotFound());
                }

                var book      = _mapper.Map <Book>(bookDTO);
                var isSuccess = await _bookRepository.Update(book);

                if (!isSuccess)
                {
                    _logger.LogWarning($"{location}: Book update failed while posting data");
                    return(InternalError($"Updated failed"));
                }

                _logger.LogInfo($"{location} : Book update completed");
                return(NoContent());
            }
            catch (Exception a)
            {
                return(InternalError($"{a.Message} - {a.InnerException}"));
            }
        }
Beispiel #26
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            var location = GetCotrollerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Attempted to update Id: {id}");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    _logger.LogWarn($"{location}: Update failed with bad data");
                    return(BadRequest());
                }

                var isExist = await _authorRepository.IsExist(id);

                if (!isExist)
                {
                    _logger.LogWarn($"{location}: Record with Id: {id} was not found");
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}: Request data for update was incompleted");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalServerError($"{location}: Update failed"));
                }

                _logger.LogInfo($"{location} updated successfully.");
                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalServerError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
Beispiel #27
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Attempted Call for id:{id}");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    _logger.LogWarn($"{location}: Bad data for Id:{id}");
                    return(BadRequest());
                }

                var exists = await _authorRepository.Exists(id);

                if (!exists)
                {
                    _logger.LogWarn($"{location}: id:{id} was not found");
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}: Data was incomplete");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: id:{id} Failure"));
                }

                _logger.LogInfo($"{location}: id:{id} Successful");
                return(NoContent()); // success
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} {e.InnerException}"));
            }
        }
Beispiel #28
0
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            try
            {
                this.logger.LogInfo("Author update attempted");

                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    this.logger.LogWarn($"Empty request was submitted - id: {id}");
                    return(this.BadRequest());
                }

                bool isExists = await this.authorRepository.IsExists(id);

                if (!isExists)
                {
                    this.logger.LogWarn($"Author with id:{id} not found");
                    return(this.NotFound());
                }

                if (!this.ModelState.IsValid)
                {
                    this.logger.LogWarn("Author data was incomplete");
                    return(this.BadRequest(this.ModelState));
                }

                var  author    = this.mapper.Map <Author>(authorDTO);
                bool isSuccess = await this.authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(this.InternalError("Author update failed"));
                }

                this.logger.LogInfo($"Author updated successfully:{author.Firstname}, {author.Lastname}");

                return(this.NoContent());
            }
            catch (Exception e)
            {
                return(this.InternalError($"{e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Attempted call");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    _logger.LogWarn($"{location}: Empty request was submitted for {id}");
                    return(BadRequest());
                }
                var isExists = await _authorRepository.isExists(id);

                if (!isExists)
                {
                    _logger.LogWarn($"{location}: Not Found for {id}");
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}: Data incomplete for { id}");

                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError($"{location}: Update Failed for {id}"));
                }
                _logger.LogInfo($"{location}: Successfully updated for { id}");

                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }
        public async Task <IActionResult> Update(int id, [FromBody] AuthorUpdateDTO authorDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo($"{location}: Update attempted on record with id: {id}");
                if (id < 1 || authorDTO == null || id != authorDTO.Id)
                {
                    _logger.LogWarn($"{location}: Update failed with with bad data - id: {id}");
                    return(BadRequest(ModelState));
                }
                var isExists = await _authorRepository.isExists(id);

                if (!isExists)
                {
                    _logger.LogWarn($"{location}: Failed to retrieve record with id: {id}");
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"{location}: Data was incomplete.");
                    return(BadRequest(ModelState));
                }

                var author    = _mapper.Map <Author>(authorDTO);
                var isSuccess = await _authorRepository.Update(author);

                if (!isSuccess)
                {
                    return(InternalError("Update Operation failed."));
                }
                _logger.LogInfo($"{location}: Update successful for record of id: {id}.");

                //Means it's an OK but I don't have anything to show you.
                return(NoContent());
            }
            catch (Exception ex)
            {
                return(InternalError($"{ex.Message} - {ex.InnerException}"));
            }
        }