// Atualizar public async Task <Divida> Update(DividaDto dividaDto) { try { var divida = await _repo.GetDividaById(dividaDto.Id); if (divida == null) { throw new ArgumentException("DIVIDA: Nenhum registro encontrado com esse id"); } dividaDto.DataModificacao = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy HH:mm")); _map.Map(dividaDto, divida); _repo.Update(divida); if (await _repo.SaveChanges()) { return(divida); } else { throw new ArgumentException("DIVIDA: Erro ao atualizar"); } } catch (System.Exception e) { throw new ArgumentException($"DIVIDA: Erro no atualizar. CODE: {e.Message}"); } }
public async Task <IActionResult> Put(int id, DividaDto model) { try { var divida = await _repo.GetAllDividasAsyncById(id); if (divida == null) { return(NotFound()); } _mapper.Map(model, divida); _repo.Update(divida); if (await _repo.SaveChangesAsync()) { return(Created($"/api/divida/{model.Id}", _mapper.Map <DividaDto>(divida))); } } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Erro no GetId")); } return(BadRequest()); }
public async Task <IActionResult> Post(DividaDto model) { try { var divida = _mapper.Map <Divida>(model); _repo.Add(divida); if (await _repo.SaveChangesAsync()) { return(Created($"/api/divida/{model.Id}", _mapper.Map <DividaDto>(divida))); } } catch (System.Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Erro no GetId {ex.Message}")); } return(BadRequest()); }
// Adicionar public async Task <DividaDto> Add(DividaDto dividaDto) { try { var entidade = _map.Map <Divida>(dividaDto); // Data De Registro da Divida entidade.DataRegistro = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy HH:mm")); _repo.Add(entidade); if (await _repo.SaveChanges()) { return(_map.Map <DividaDto>(entidade)); } else { throw new ArgumentException("DIVIDA: Erro ao adicionar"); } } catch (System.Exception e) { throw new ArgumentException($"DIVIDA: Erro ao adicionar. CODE: {e.Message}"); } }