protected void ButtonAddTask_Click(object sender, EventArgs e) { if (IsValid) { var task = new Task { Name = TextBoxName.Text, CategoryId = Convert.ToInt32(DropDownListCategories.SelectedValue), StartDate = CalendarStartTime.SelectedDate, DueDate = CalendarDueDate.SelectedDate, Completed = CheckBoxIsCompleted.Checked, Priority = Convert.ToInt32(DropDownListPriority.SelectedValue), PercentComplete = Convert.ToInt16(TextBoxPercentComplete.Text), UserName = User.Identity.Name }; var validator = new TaskValidator(); if (validator.Validate(task)) { var repository = TaskRepository.Instance; repository.AddTask(task); LabelResults.Text = "Successfully added task"; BulletedListErrorMessages.Items.Clear(); } else { BulletedListErrorMessages.DataSource = validator.ErrorMessages; BulletedListErrorMessages.DataBind(); } } }
protected void ButtonEditTask_Click(object sender, EventArgs e) { if (IsValid) { try { // Need to retrieve the task again in case there are multiple tabs open on the browser. // This ensures that the task actually still exists and was not already edited to 'completed' status. int taskId = Convert.ToInt32(Request["taskId"]); var repository = TaskRepository.Instance; Task task = repository.GetTask(taskId); if (task == null || task.Completed) { throw new Exception(string.Format("Could not edit task with id '{0}'. It does not exist or is already in the completed status", taskId)); } if (task.UserName != User.Identity.Name) { throw new Exception("You cannot edit a task that does not belong to you!"); } task.CategoryId = Convert.ToInt32(DropDownListCategories.SelectedValue); task.StartDate = CalendarStartTime.SelectedDate; task.DueDate = CalendarDueDate.SelectedDate; task.Completed = CheckBoxIsCompleted.Checked; task.Priority = Convert.ToInt32(DropDownListPriority.Text); task.PercentComplete = Convert.ToInt16(TextBoxPercentComplete.Text); var validator = new TaskValidator(); if (validator.Validate(task)) { repository.UpdateTask(task); ButtonEditTask.Enabled = !task.Completed; LabelResults.Text = "Successfully edited task"; BulletedListErrorMessages.Items.Clear(); } else { BulletedListErrorMessages.DataSource = validator.ErrorMessages; BulletedListErrorMessages.DataBind(); } } catch (Exception ex) { LabelResults.Text = ex.Message; } } }