示例#1
0
        //Removed Task from table.
        public IActionResult Remove_Task(int id)
        {
            string user_id_string = HttpContext.Session.GetString("User_ID");
            int    user_id;



            if (user_id_string != null)
            {
                try
                {
                    user_id = Convert.ToInt32(user_id_string);
                }
                catch
                {
                    user_id = 0;
                    return(RedirectToAction("Index", "Home"));
                }

                var task = _context.ToDo_Task.Where(i => i.Task_ID == id).SingleOrDefault();
                _context.Remove(task);
                _context.SaveChangesAsync();

                return(RedirectToAction("Add_Task", "Todo_Task"));
            }
            else
            {
                TempData["msg"] = _CLSR.GetScriptAlertPopUp("Warning", "You have not login yet.", "", "D");
                return(RedirectToAction("Index", "Home"));
            }
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("User_ID,Role_ID,Email,Password,First_Name,Last_Name,Birthdate,Wrong_Password_Count,Last_Login,Last_Update,Gender,User_Status,User_isLock")] User user)
        {
            if (ModelState.IsValid)
            {
                int userID = 1;
                try
                {
                    userID = _context.User.Max(p => p.User_ID) + 1;
                }
                catch
                {
                    //ไม่เจอ ID สักอันแสดงว่า Table นี้ว่างอยู่ให้เริ่มที่ 1
                    userID = 1;
                }

                var checkUserIsAlready = _context.User.Where(i => i.Email == user.Email).Count();

                //Email is already exist!
                if (checkUserIsAlready > 0)
                {
                    TempData["msg"] = _CLSR.GetScriptAlertPopUp("Error", "Please contact Administrator", "", "E");
                    return(RedirectToAction("Create", "Users"));
                }
                else
                {
                    if (user.Email != null && user.Password != null && user.First_Name != null)
                    {
                        //ควรเปิด Transaction ทุกครั้งที่มีการเปลี่ยนแปลงข้อมูลใน Table
                        using (IDbContextTransaction dbTran = _context.Database.BeginTransaction())
                        {
                            user.Password = _CLSR.EncodeHMAC_SHA512(user.Password);

                            String cDate = _CLSR.GetDateNow("");
                            String cTime = _CLSR.GetTimeNow("");



                            //ROLE ID 1 คือ Admin
                            //ROLE ID 2 คือ Normal User
                            user.Role_ID              = 2;
                            user.Birthdate            = _CLSR.ConvertDatePicker(user.Birthdate);
                            user.Wrong_Password_Count = 0;
                            user.Last_Login           = cDate;
                            user.Last_Update          = cDate;
                            user.User_Status          = "Y";
                            user.User_isLock          = "N";



                            _context.Add(user);

                            await _context.SaveChangesAsync();


                            Trees basic_Tree = new Trees();

                            basic_Tree.User_ID      = user.User_ID;
                            basic_Tree.Tree_Level   = 1;
                            basic_Tree.Tree_EXP     = 0;
                            basic_Tree.Tree_Type_ID = 1;
                            basic_Tree.Tree_Name    = "Basic Tree";
                            basic_Tree.Tree_HP      = 100;
                            basic_Tree.Plant_Date   = cDate;
                            basic_Tree.Create_Date  = cDate;
                            basic_Tree.Update_Date  = cDate;
                            basic_Tree.Tree_Status  = "S";
                            basic_Tree.Tree_isDead  = "N";

                            _context.Add(basic_Tree);
                            await _context.SaveChangesAsync();

                            //ถ้าบันทึกข้อมูลเสร็จ ให้ Commit เพื่อยืนยันการเซฟข้อมูล
                            dbTran.Commit();

                            //ถ้าไม่สำเร็จให้ RollBack();

                            //try
                            //{
                            //    HCON.Login(user.Email, user.Password);
                            //}
                            //catch (Exception e)
                            //{
                            //    TempData["msg"] = "<script>alert('"+ e.Message +"')</script>";

                            //    return RedirectToAction("Index", "Home");
                            //}
                            TempData["msg"] = "<script>alert('Sign Up successfully')</script>";
                            //Temp Message เพื่อขึ้น Alert ป๊อปอัพแสดง
                            //TempData["msg"] = _CLSR.GetScriptAlertPopUp("Success", "Register Successfully!", "", "S");
                        }
                    }
                    else
                    {
                        TempData["msg"] = _CLSR.GetScriptAlertPopUp("Error", "Please contact Administrator", "", "E");
                        return(RedirectToAction("Create", "Users"));
                    }
                }
            }
            return(View(user));
        }