public static Task AddTask(string taskText, string userObjectId, string userName) { // Add a new task to the db TdlrContext db = new TdlrContext(); Task newTask = new Task { Status = "NotStarted", TaskText = taskText, Creator = userObjectId, SharedWith = new List<AadObject>(), CreatorName = userName, }; // Get the AadObject representing from the user if it exists AadObject user = db.AadObjects.Find(userObjectId); if (user != null) { // Update the user's display name in case it has changed user.DisplayName = userName; } else { user = new AadObject { AadObjectID = userObjectId, DisplayName = userName, }; } newTask.SharedWith.Add(user); db.Tasks.Add(newTask); db.SaveChanges(); return newTask; }
public static void UpdateShares(int taskId, List<Share> shares) { //Share a task with a user or group TdlrContext db = new TdlrContext(); Task task = db.Tasks.Find(taskId); // Maintain that the task is shared with the owner AadObject user = task.SharedWith.Where(u => u.AadObjectID == task.Creator).FirstOrDefault(); task.SharedWith = new List<AadObject>(); task.SharedWith.Add(user); foreach (Share share in shares) { AadObject aadObject = db.AadObjects.Find(share.objectID); if (aadObject != null) { aadObject.DisplayName = share.displayName; } else { aadObject = new AadObject { AadObjectID = share.objectID, DisplayName = share.displayName, }; } task.SharedWith.Add(aadObject); } db.SaveChanges(); }