public async void Update(int Id, UpdateDebtDTO updatedDebt) { try { var debt = _dbContext.Debts .Include(x => x.Parcels) .SingleOrDefault(x => x.Id == Id); if (debt is null) { throw new Exception("Not Found"); } if (updatedDebt.Description != null) { debt.Description = updatedDebt.Description; } if (updatedDebt.NegotiatorComissionPercentage != 0) { debt.NegotiatorComissionPercentage = updatedDebt.NegotiatorComissionPercentage; } if (updatedDebt.Parcels != null) { foreach (var updatedParcel in updatedDebt.Parcels) { var existingParcel = debt.Parcels.SingleOrDefault(x => x.Parcel == updatedParcel.Parcel); if (existingParcel is null) { throw new Exception("Not Found"); } if (existingParcel.Paid != updatedParcel.Paid) { existingParcel.Paid = updatedParcel.Paid; } } var parcelNotPaid = updatedDebt.Parcels.FirstOrDefault(x => x.Paid == false); if (parcelNotPaid is null) { debt.Paid = true; } } await _dbContext.SaveChangesAsync(); } catch (Exception e) { throw new Exception($"Houve um problema ao fazer esta operação.\n-> {e.Message}"); } }
public void Update(int debtId, UpdateDebtDTO updatedDebt) { try { _debtRepository.Update(debtId, updatedDebt); } catch (Exception e) { throw new Exception($"Houve um problema ao fazer esta operação.\n-> {e.Message}"); } }
public IActionResult Update(int Id, UpdateDebtDTO updateDebt) { try { _debtService.Update(Id, updateDebt); return(Ok("Updated")); } catch (Exception e) { return(BadRequest(e.Message)); } }