public static List <AgendaTaskClass> GetAgendaTasks(string username, int agendaID) { using (boxEntities box = new boxEntities()) { // user id int userid = Profile.GetUserID(username); var result = new List <AgendaTaskClass>(); // check the user role if not creator then get the assigned to tasks only if (AgendaPrivacyClass.CheckRole(username, MeetingRoles.Creator, agendaID)) { result = (from o in box.TaskAssigns.Include("TaskUsers").Include("UserProfile") where o.AgendaID == agendaID select new AgendaTaskClass() { Title = o.TaskTitle, Desc = o.TaskDesc, TaskID = o.TaskID, DueDate = o.DueDate, CreatedDate = o.DateCreated, }).ToList(); foreach (var t in result) { t.Usernames = box.TaskUsers.Where(a => a.TaskID == t.TaskID).Select(a => a.UserProfile.UserName).ToArray(); } } else { result = (from o in box.TaskUsers where o.TaskAssign.AgendaID == agendaID && o.UserID == userid select new AgendaTaskClass() { Title = o.TaskAssign.TaskTitle, Desc = o.TaskAssign.TaskDesc, TaskID = o.TaskAssign.TaskID, DueDate = o.TaskAssign.DueDate, CreatedDate = o.TaskAssign.DateCreated, }).ToList(); } return(result); } }
public static AgendaTaskClass UpdateAgendaTask(int taskID, string username, int agendaID, string title, string desc, DateTime dueDate, string[] usernames) { using (boxEntities box = new boxEntities()) { usernames = usernames.Distinct().ToArray(); // get meetin id var m = (from o in box.Agenda where o.AgendaID == agendaID select o.MeetingID).FirstOrDefault(); if (AgendaPrivacyClass.CheckRole(username, MeetingRoles.Creator, agendaID)) { // get the header var task = (from o in box.TaskAssigns.Include("TaskUsers") where o.TaskID == taskID select o).FirstOrDefault(); task.TaskTitle = title; task.TaskDesc = desc; task.DueDate = dueDate; task.Creator = Profile.getUserID(username); box.SaveChanges(); // get the users int[] NewUsers = { }; if (usernames.Contains("All")) { // get all users in meeting ids NewUsers = AgendaPrivacyClass.ListParticipantIDs(agendaID); } else { NewUsers = Profile.GetUserIDs(usernames); } // get old users int[] OldUsers = task.TaskUsers.Select(a => a.UserProfile.UserID).ToArray(); int[] same = NewUsers.Intersect(OldUsers).ToArray(); // insert new foreach (var t in NewUsers) { if (!same.Contains(t)) { TaskUser u = new TaskUser(); u.UserID = t; u.TaskID = task.TaskID; u.AssignedDate = DateTime.Now; task.TaskUsers.Add(u); } } // delete foreach (var o in OldUsers) { if (!same.Contains(o)) { var dtask = task.TaskUsers.Where(a => a.UserID == o).FirstOrDefault(); if (dtask != null) { task.TaskUsers.Remove(dtask); } } } box.SaveChanges(); // return object AgendaTaskClass tt = new AgendaTaskClass(); tt.Title = task.TaskTitle; tt.Desc = task.TaskDesc; tt.DueDate = task.DueDate; tt.TaskID = task.TaskID; tt.CreatedDate = task.DateCreated; tt.Usernames = task.TaskUsers.Select(a => a.UserProfile.UserName).ToArray(); return(tt); } else { throw new UnauthorizedAccessException(); } } }