public async Task <IActionResult> CreateSource([FromBody] CreateUpdateSourceDto source) { try { if (source == null) { _logger.LogError("Source object sent from client is null."); return(BadRequest("Source object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid source object sent from client."); return(BadRequest("Invalid model object")); } var sourceEntity = _mapper.Map <Source>(source); sourceEntity.CreatedDate = DateTime.Now; _repository.Source.CreateSource(sourceEntity); await _repository.SaveAsync(); var createdSource = _mapper.Map <SourceDto>(sourceEntity); return(CreatedAtRoute("SourceById", new { id = createdSource.Id }, createdSource)); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CreateSource action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> UpdateSource(int id, [FromBody] CreateUpdateSourceDto source) { try { if (source == null) { _logger.LogError("Source object sent from client is null."); return(BadRequest("Source object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid source object sent from client."); return(BadRequest("Invalid model object")); } var sourceEntity = await _repository.Source.GetSourceByIdAsync(id); if (sourceEntity == null) { _logger.LogError($"Source with id: {id}, hasn't been found in db."); return(NotFound()); } _mapper.Map(source, sourceEntity); sourceEntity.UpdatedDate = DateTime.Now; _repository.Source.UpdateSource(sourceEntity); await _repository.SaveAsync(); return(NoContent()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside UpdateSource action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }