public void AllocationCredit(CreditViewModel model, string userId) { using (OnlineSaverEntities context = new OnlineSaverEntities()) { //var creditList = context.SavingGoals.Where(x => (!x.CreditAllocations.Any() || x.CreditAllocations.Sum(y => y.AllocatedAmount) < x.Goal)); var allocateGoalList = context.SavingGoals.Where(x => x.SavedAmount < x.Goal && x.UserId == userId); if (allocateGoalList.Any()) { allocateGoalList = allocateGoalList.OrderBy(x => x.CreateDate); foreach (var goal in allocateGoalList) { var amountNeedBeAllocated = goal.Goal - goal.SavedAmount; if (model.CreditAmount >= amountNeedBeAllocated) { goal.SavedAmount += amountNeedBeAllocated; model.CreditAmount -= amountNeedBeAllocated.Value; } else { goal.SavedAmount += model.CreditAmount; model.CreditAmount = 0; } if (model.CreditAmount <= 0) { break; } } context.SaveChanges(); } } }
public void CreateSavingGoal(SavingGoal model) { using (var ctx = new OnlineSaverEntities()) { ctx.SavingGoals.Add(model); ctx.SaveChanges(); } }
public void CreateCredit(CreditViewModel model) { using (OnlineSaverEntities context = new OnlineSaverEntities()) { context.Credits.Add(new Credit() { Descrption = model.Description, ImportAmount = model.CreditAmount, UserId = model.UserId }); context.SaveChanges(); } }