Esempio n. 1
0
        public ActionResult transferMoney()
        {
            string error = "";
            try
            {
                extreemtEntities db = new extreemtEntities();
                int transId = int.Parse(Request.Form["id"]);
                if (db.users.Where(u => u.userId == transId && u.genNumber == 1).Count() >= 0){

                    Account acc = new Account(this);
                    user loggedUser = acc.getLoggedUser();

                    /*
                    //temp
                    Session.Add("userId", "25478961");
                    int currentuserId = int.Parse(Session["userId"].ToString());
                    */
                    int amount = int.Parse(Request.Form["amount"]);
                    if (db.users.Where(u => u.userId == loggedUser.userId && u.genNumber == 1).Count() >= 0)
                    {

                        user transUser = db.users.Where(u => u.userId == transId && u.genNumber == 1).First();
                        if (loggedUser.userId != transUser.userId)
                        {
                            if (loggedUser.cashBank >= amount)
                            {
                                db.Entry(loggedUser).State = System.Data.EntityState.Modified;
                                db.Entry(transUser).State = System.Data.EntityState.Modified;
                                transUser.cashBank += amount;
                                loggedUser.cashBank -= amount;
                                db.SaveChanges();
                                error += "Amount Transfered Successfully";
                            }
                            else
                            {
                                error += "You Don't Have Enough Credit !";
                            }
                        }
                        else
                        {
                            if (loggedUser.cashBank >= amount)
                            {
                                loggedUser.productBank += amount;
                                loggedUser.cashBank -= amount;
                                db.Entry(transUser).CurrentValues.SetValues(loggedUser);
                                db.SaveChanges();
                                error += "Amount Transfered Successfully to Your Product Bank!";
                            }
                        }
                    }
                    else
                    {
                        error += "You Aren't Login";
                    }
                }
                else
                {
                    error += "No User With This Id";
                }
            }
            catch (Exception e)
            {
                error += "Error Occured , please make sure that you provide a VALID ID and AMOUNT";
            }
            TempData["error"] = error;
            return Redirect(Url.Action("transfer", "Account"));
        }