public ActionResult Create([Bind(Include = "ID,UserID,Description,DueDate,Complete")] Task task)

        {
            if (ModelState.IsValid)
            {
                task.UserID = Convert.ToInt32(Session["ID"]);
                db.Tasks.Add(task);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(task));
        }
Exemple #2
0
 public static bool DeleteTask(TaskWork task)
 {
     using (var _context = new DBTaskContext())
     {
         //Lấy thực thể kiểu User từ database
         var dbTask = (from t in _context.tbTaskWorks
                       where t.ID == task.ID
                       select t).SingleOrDefault();
         #region Xoá công việc khỏi quan hệ tbUser
         foreach (var user in dbTask.listUser)
         {
             foreach (var tempTask in user.tasks)
             {
                 if (tempTask.ID == task.ID)
                 {
                     user.tasks.Remove(tempTask);
                     break;
                 }
             }
         }
         #endregion
         _context.tbTaskWorks.Remove(dbTask);
         _context.SaveChanges();
         return(true);
     }
 }
Exemple #3
0
 //public static bool DeleteUser(string username)
 //{
 //    using (var _context = new DBTaskContext())
 //    {
 //        User user = (from u in _context.tbUsers
 //                     where u.username == username
 //                     select u).SingleOrDefault();
 //        _context.tbUsers.Remove(user);
 //        _context.SaveChanges();
 //        return true;
 //    }
 //}
 public static bool DeleteUser(User user)
 {
     using (var _context = new DBTaskContext())
     {
         //Lấy thực thể kiểu User từ database
         var dbUser = (from u in _context.tbUsers
                       where u.username == user.username
                       select u).SingleOrDefault();
         #region Xoá user khỏi danh sách công việc
         foreach (var task in dbUser.tasks)
         {
             foreach (var u in task.listUser)
             {
                 if (u.username == user.username)
                 {
                     task.listUser.Remove(u);
                     break;
                 }
             }
         }
         #endregion
         _context.tbUsers.Remove(dbUser);
         _context.SaveChanges();
         return(true);
     }
 }
Exemple #4
0
 public static bool UpdateUser(User user)
 {
     using (var _context = new DBTaskContext())
     {
         _context.tbUsers.AddOrUpdate(user);
         _context.SaveChanges();
         return(true);
     }
 }
 public static void initializeDB()
 {
     using (var _context = new DBTaskContext())
     {
         var user = new User {
             username = "******", ngaySinh = DateTime.Now
         };
         _context.tbUsers.Add(user);
         _context.SaveChanges();
     }
     MessageBox.Show("Finish!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
Exemple #6
0
 public static bool AddUser(User user)
 {
     try
     {
         using (var _context = new DBTaskContext())
         {
             _context.tbUsers.Add(user);
             _context.SaveChanges();
             return(true);
         }
     }
     catch
     {
         return(false);
     }
 }
Exemple #7
0
 public static bool UpdateTask(TaskWork task)
 {
     using (var _context = new DBTaskContext())
     {
         var dbTask = (from t in _context.tbTaskWorks
                       where t.ID == task.ID
                       select t).SingleOrDefault();
         foreach (var user in task.listUser)
         {
             var dbUser = (from u in _context.tbUsers
                           where u.username == user.username
                           select u).SingleOrDefault();
             dbUser.tasks.Add(dbTask);
         }
         task.listUser.Clear();
         _context.tbTaskWorks.AddOrUpdate(task);
         _context.SaveChanges();
         return(true);
     }
 }
Exemple #8
0
 public static bool AddTask(TaskWork task)
 {
     using (var _context = new DBTaskContext())
     {
         foreach (var user in task.listUser)
         {
             //var dbUser = (from u in _context.tbUsers
             //              where u.username == user.username
             //              select u).Single();
             var dbUser = _context.tbUsers.Single(x => x.username == user.username);
             if (dbUser.tasks is null)
             {
                 dbUser.tasks = new List <TaskWork>();
             }
             dbUser.tasks.Add(task);
         }
         task.listUser.Clear();
         _context.tbTaskWorks.AddOrUpdate(task);
         _context.SaveChanges();
         return(true);
     }
 }