コード例 #1
0
 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();
         }
     }
 }
コード例 #2
0
 public void CreateSavingGoal(SavingGoal model)
 {
     using (var ctx = new OnlineSaverEntities()) {
         ctx.SavingGoals.Add(model);
         ctx.SaveChanges();
     }
 }
コード例 #3
0
 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();
     }
 }
コード例 #4
0
 public bool EnbleToCreateCredit(CreditViewModel model, string userId, out decimal needAllocatedAmount)
 {
     using (OnlineSaverEntities context = new OnlineSaverEntities())
     {
         var goals = context.SavingGoals.Where(x => x.UserId == userId);
         if (goals.Any())
         {
             needAllocatedAmount = goals.Sum(x => x.Goal) - goals.Sum(x => x.SavedAmount).Value;
             return(model.CreditAmount <= needAllocatedAmount);
         }
         needAllocatedAmount = 0;
         return(false);
     }
 }
コード例 #5
0
ファイル: GoalsService.cs プロジェクト: JinBiao23/OnlineSaver
 public List <SavingGoal> GetUserGoals(string userId)
 {
     using (var ctx = new OnlineSaverEntities()) {
         return(ctx.SavingGoals.Where(x => x.UserId == userId).ToList());
     }
 }