public async Task <IActionResult> Update(int id, [FromBody] ToyUpdateDTO toyDTO) { var location = GetControllerActionNames(); try { _logger.LogInfo($"{location}: Updated Attempted on record with id: {id}"); if (id < 1 || toyDTO == null || id != toyDTO.Id) { _logger.LogWarn($"{location}:Update failed with bad data - id: {id}"); return(BadRequest()); } var isExists = await _toyRepository.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 oldImage = await _toyRepository.GetImageFileName(id); var book = _mapper.Map <Toy>(toyDTO); var isSuccess = await _toyRepository.Update(book); if (!isSuccess) { return(internalError($"{location}: Update failed for record with id: {id}")); } if (!toyDTO.Image.Equals(oldImage)) { if (System.IO.File.Exists(GetImagePath(oldImage))) { System.IO.File.Delete(GetImagePath(oldImage)); } } if (!string.IsNullOrEmpty(toyDTO.File)) { byte[] imageBytes = Convert.FromBase64String(toyDTO.File); System.IO.File.WriteAllBytes(GetImagePath(toyDTO.Image), imageBytes); } _logger.LogInfo($"{location}: Record with id: {id} successfully updated"); return(NoContent()); } catch (Exception e) { return(internalError($"{location}: {e.Message} - {e.InnerException}")); } }