//public double GetBalance(string userId)
        //{
        //    double balance = 500;
        //    using (ApplicationDbContext db = new ApplicationDbContext())
        //    {
        //        var user = db.Money.FirstOrDefault(x => x.Id == userId);
        //        if (user == null)
        //        {
        //            Money money = new Money();
        //            money.Balance =Convert.ToDecimal( balance);
        //            money.Id = userId;
        //            db.Money.Add(money);
        //            db.SaveChanges();

        //        }
        //        else
        //        {
        //            balance = Convert.ToDouble(db.Money.FirstOrDefault(x => x.Id == userId).Balance);
        //        }

        //    }
        //    return balance;
        //}
        public bool BuyPackagePartGoal(string userId, int?packageId)
        {
            bool ok = false;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                try
                {
                    Package      package      = db.Packages.Find(packageId);
                    UserWithGoal userWithGoal = db.UsersWithGoals.Find(userId);
                    // Money money = db.Money.Find(userId);
                    userWithGoal.PackageId = package.Id;
                    // money.Balance = money.Balance - package.Price;
                    db.UsersWithGoals.Attach(userWithGoal);
                    // db.Money.Attach(money);
                    db.Entry(userWithGoal).State = System.Data.Entity.EntityState.Modified;
                    // db.Entry(money).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    ok = true;
                }
                catch
                {
                }
            }
            return(ok);
        }
        public void InsertUser(UserWithGoal applicationUser)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var userExsist = db.UsersFruits.FirstOrDefault(x => x.UserId == applicationUser.Id);
                if (userExsist != null)
                {
                    db.UsersWithGoals.Add(applicationUser);
                }
                else
                {
                    db.UsersWithGoals.Add(applicationUser);
                }

                db.SaveChanges();
            }
        }
Esempio n. 3
0
        public IHttpActionResult Update(UserWithGoal userWithGoal)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            string userId = User.Identity.GetUserId();

            userWithGoal.Id = userId;
            var updated = _userGoalRepositoty.SetGoal(userWithGoal);

            if (!updated)
            {
                return(NotFound());
            }

            return(Ok());
        }
Esempio n. 4
0
        public IHttpActionResult Update([FromBody] UserWithGoal userWithGoal, int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            string userId = User.Identity.GetUserId();

            userWithGoal.Id = userId;
            var updated = _packageRepositoty.BuyPackagePartGoal(userId, userWithGoal.PackageId);

            if (!updated)
            {
                return(NotFound());
            }

            return(Ok());
        }
Esempio n. 5
0
        public bool SetGoal(UserWithGoal userWithGoal)
        {
            bool goalbool = false;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                try
                {
                    db.UsersWithGoals.Attach(userWithGoal);
                    db.Entry(userWithGoal).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    goalbool = true;
                }
                catch
                {
                }
            }
            return(goalbool);
        }
        public void SaveUser(UserWithGoal applicationUser)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var userExsist = db.UsersFruits.FirstOrDefault(x => x.UserId == applicationUser.Id);
                if (userExsist != null)
                {
                    db.UsersWithGoals.Attach(applicationUser);
                    db.Entry(applicationUser).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    db.UsersWithGoals.Add(applicationUser);
                    db.UsersWithGoals.Attach(applicationUser);
                    db.Entry(applicationUser).State = System.Data.Entity.EntityState.Modified;
                }

                db.SaveChanges();
            }
        }
Esempio n. 7
0
        public double GetGoal(string userId)
        {
            double goal = 0;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var user = db.UsersWithGoals.FirstOrDefault(x => x.Id == userId);
                if (user == null)
                {
                    UserWithGoal userWithGoal = new UserWithGoal();
                    userWithGoal.Goal = 0;
                    userWithGoal.Id   = userId;
                    db.UsersWithGoals.Add(userWithGoal);
                    db.SaveChanges();
                    goal = 0;
                }
                else
                {
                    goal = db.UsersWithGoals.FirstOrDefault(x => x.Id == userId).Goal;
                }
            }
            return(goal);
        }
 public ActionResult Index(UserWithGoal applicationUser)
 {
     _usersRepository.SaveUser(applicationUser);
     return(RedirectToAction("About", applicationUser));
 }