public async Task <bool> ValidateAndUpdateMidAsync(MIDViewModel mid, int id) { var currentMid = await FindAsync(id); bool isValid = false; var branch = await branchRepo.GetFirstOrDefaultAsync(predicate : b => b.Id == mid.BranchId, include : b => b.Include(bb => bb.MIDs)); int midCount = branch.MIDs.Count; if (mid.currencyPhp.Value && mid.currencyUsd.Value) { if (midCount < 10) { isValid = true; MID midPhpModel = new MID(); MID midUsdModel = new MID(); if (currentMid.currencyPhp.HasValue) { if (currentMid.currencyPhp.Value) { mapper.Map <MIDViewModel, MID>(mid, currentMid); currentMid.currencyUsd = false; await Update(currentMid); mapper.Map <MIDViewModel, MID>(mid, midUsdModel); midUsdModel.Id = 0; midUsdModel.BranchId = currentMid.BranchId; midUsdModel.currencyPhp = false; await InsertAsync(midUsdModel); } else { mapper.Map <MIDViewModel, MID>(mid, currentMid); currentMid.currencyPhp = false; await Update(currentMid); mapper.Map <MIDViewModel, MID>(mid, midPhpModel); midPhpModel.Id = 0; midPhpModel.BranchId = currentMid.BranchId; midPhpModel.currencyUsd = false; await InsertAsync(midPhpModel); } } } } else { isValid = true; mapper.Map <MIDViewModel, MID>(mid, currentMid); await Update(currentMid); } return(isValid); }
public async Task <IActionResult> UpdateMID([FromBody] MIDViewModel mid, int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } bool isValid = await midService.ValidateAndUpdateMidAsync(mid, id); if (isValid) { await midService.SaveChangesAsync(); } else { return(Ok(isValid)); } return(Ok(mid)); }