public async Task <ActionResult <Contract> > Patch( [FromBody] ContractPatchRequest request, int id) { var contract = await _context.Contracts.FindAsync(id); if (contract == null) { return(NotFound()); } contract.NumberInstallments = request.NumberInstallments; contract.AmountFinanced = request.AmountFinanced; contract = await _service.RemoveInstallments(id); if (ModelState.IsValid) { var contractPatch = await _service.InsertInstallmentsPatch(request, contract); return(contractPatch); } else { return(BadRequest(ModelState)); } }
/// <summary> /// Include installments according to the number set in the patch to update the contract /// </summary> /// <param name="ContractPatchRequest request, Contract model"></param> /// <returns> Update contract with new installments public async Task <ActionResult <Contract> > InsertInstallmentsPatch( [FromBody] ContractPatchRequest request, [FromBody] Contract model) { var dividedAmount = model.AmountFinanced / model.NumberInstallments; for (var i = 1; i < request.NumberInstallments; i++) { var installment = new Installment(); request.Installments.Add(installment); } model.Installments = request.Installments; foreach (var installmentItem in model.Installments) { installmentItem.Amount = dividedAmount; installmentItem.Status = SetStatus(installmentItem); } await _context.SaveChangesAsync(); return(model); }