protected void Page_Load(object sender, EventArgs e) { if (DB == null) { DB = DBHandler.Instance; } if (Session["LoggerUser"] == null) { Response.Redirect("Login.aspx"); } if (Session["sharingTasks"] != null) { sharingTasks.Text = (string)Session["sharingTasks"]; } else { sharingTasks.Text = String.Empty; } Session["sharingTasks"] = null; User loggedUser = (User)Session["LoggerUser"]; bool haveAnyTasks = false; var tasks = DB.getUserTasks(loggedUser); if (tasks != null) { haveAnyTasks = tasks.Length != 0; } if (haveAnyTasks) { makeTable(); } TaskCount.Visible = !haveAnyTasks; }
protected void ShareButton_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(UsersEmailsToShare.Text)) { string[] inputEmails = UsersEmailsToShare.Text.Split(','); if (inputEmails.Length > 0) { List <string> invalidEmails = new List <string>(); List <string> noUsersWithThisEmail = new List <string>(); for (int i = 0; i < inputEmails.Length; i++) { inputEmails[i] = inputEmails[i].Replace(" ", ""); bool isValid = inputEmails[i].IsValidEmail(); if (!isValid) { invalidEmails.Add(inputEmails[i]); } else { User userWithCurrentEmail = new User(); userWithCurrentEmail = DB.getUserByEmail(inputEmails[i]); if (userWithCurrentEmail == null) { noUsersWithThisEmail.Add(inputEmails[i]); } } } if (invalidEmails.Count > 0) { string errorText = ""; if (invalidEmails.Count > 1) { errorText = "You have entered invalid emails:<br/>"; } else { errorText = "You have entered invalid email: "; } foreach (string str in invalidEmails) { errorText += str + ", "; } errorText = errorText.TrimEnd(", ".ToCharArray()); Session["SharingTaskError"] = errorText; Response.Redirect(Request.RawUrl); } else if (noUsersWithThisEmail.Count > 0) { string errorText = ""; if (noUsersWithThisEmail.Count > 1) { errorText = "There are no users with this emails in the system:</br>"; } else { errorText = "There is no user with this email in the system: "; } foreach (string str in noUsersWithThisEmail) { errorText += str + "</br"; } errorText = errorText.TrimEnd(", ".ToCharArray()); Session["SharingTaskError"] = errorText; Response.Redirect(Request.RawUrl); } else { string resultText = ""; for (int i = 0; i < inputEmails.Length; i++) { User currentUser = DB.getUserByEmail(inputEmails[i]); TTask[] userTasks = DB.getUserTasks(currentUser); if (userTasks.Contains(selectedTask)) { resultText += "User " + currentUser.Username + " already has task: " + selectedTask.NameOfTask + "<br/>"; } else { currentUser.AddNewTask(selectedTask.ID, ((User)Session["LoggerUser"]).ID); resultText += "User " + currentUser.Username + " successfuly received the task!" + "<br/>"; } } DB.db.SaveChanges(); Session["sharingTasks"] = resultText; Response.Redirect("Default.aspx"); } } } else { Session["SharingTaskError"] = "Emails field cannot be left empty!"; Response.Redirect(Request.RawUrl); } }