示例#1
0
        private List <int> AddGiveToMoney(MaaserGiven maaserGiven)
        {
            List <int> moneyIds = new List <int>();

            using (var ctx = new MaaserContext(_connString))
            {
                IEnumerable <MoneyEarned> money = ctx.MoneyEarned.Where(z => z.UserId == _user.Id);
                MoneyEarned m      = money.FirstOrDefault(mo => mo.PaidUp == false);
                decimal     amount = maaserGiven.Amount * 10;
                if (m == null)
                {
                    return(null);
                }

                while (m.PaidUp == false && amount != 0 && m.AmountLeft != 0)
                {
                    m.AmountLeft = m.AmountLeft - amount;
                    moneyIds.Add(m.Id);
                    if (m.AmountLeft < 0)
                    {
                        amount       = 0 - m.AmountLeft;
                        m.AmountLeft = 0;
                        m.PaidUp     = true;
                        ctx.MoneyEarned.Attach(m);
                        ctx.Entry(m).State = EntityState.Modified;
                        m = money.FirstOrDefault(mo => mo.PaidUp == false);
                        if (m == null)
                        {
                            break;
                        }
                    }
                    else if (m.AmountLeft == 0)
                    {
                        m.PaidUp = true;
                        ctx.MoneyEarned.Attach(m);
                        ctx.Entry(m).State = EntityState.Modified;
                    }
                    else
                    {
                        ctx.SaveChanges();
                        break;
                    }
                    ctx.SaveChanges();
                }

                return(moneyIds);
            }
        }
示例#2
0
 public void AddAmount(MoneyEarned money)
 {
     using (var ctx = new MaaserContext(_connString))
     {
         money.UserId = _user.Id;
         ctx.MoneyEarned.Add(money);
         ctx.SaveChanges();
     }
 }
示例#3
0
 public string AddUser(User user, string password)
 {
     user.HashedPassword = PasswordHelper.HashPassword(password);
     using (var ctx = new MaaserContext(_connString))
     {
         if (ctx.Users.FirstOrDefault(u => u.UserName == user.UserName) != null)
         {
             return("Username Already Used");
         }
         ctx.Users.Add(user);
         ctx.SaveChanges();
         return("");
     }
 }
示例#4
0
        public void AddMaaserGiven(MaaserGiven maaserGiven)
        {
            using (var ctx = new MaaserContext(_connString))
            {
                maaserGiven.UserId = _user.Id;
                ctx.MaaserGiven.Add(maaserGiven);
                ctx.SaveChanges();
                var money = AddGiveToMoney(maaserGiven);
                if (money.Count > 1)
                {
                    maaserGiven.GiveToMoney = money.Select(i => new GiveToMoney
                    {
                        MaaserGivenId = maaserGiven.Id, UserId = _user.Id, MoneyId = i
                    }).ToList();
                    ctx.MaaserGiven.Attach(maaserGiven);
                    ctx.Entry(maaserGiven).State = EntityState.Modified;
                }



                ctx.SaveChanges();
            }
        }