/// <summary> /// Creates a new subject. /// </summary> /// <param name="model">Subject details to create.</param> /// <returns>0 on subject already exists, 1 on success, 2 on unexpected database error.</returns> public int Create(Subject model) { int result = 0; List <Subject> subjects = new List <Subject>(); if (model != null) { subjects = Details(model.UserId); } try { using (TaskDatabase db = new TaskDatabase()) { Subject query = (from subject in subjects where subject.Name == model.Name select subject).FirstOrDefault(); if (query == null) { result = db.Insert(model); } } } catch (Exception) { result = 2; } return(result); }
/// <summary> /// Creates a new grade. /// </summary> /// <param name="model">Grade details to create.</param> /// <returns>0 on failure, 1 on success, 2 on unexpected database error.</returns> public int Create(Grade model) { int result = 0; try { using (TaskDatabase db = new TaskDatabase()) { result = db.Insert(model); } } catch (Exception) { result = 2; } return(result); }
/// <summary> /// Register an unique account. /// </summary> /// <param name="model">User details to register.</param> /// <returns>0 on failure, 1 on success, 2 on unexpected database error.</returns> public int Register(User model) { int result = 0; bool isUsername = false; if (model != null) { if (!string.IsNullOrEmpty(model.Username)) { isUsername = Regex.IsMatch(model.Username, @"^(?=.{3,32}$)(?![_-])[a-zA-Z0-9-_ ]+(?<![_-])$", RegexOptions.None); } } if (isUsername == true) { try { using (TaskDatabase db = new TaskDatabase()) { User query = (from user in db.Users where user.Username == model.Username select user).SingleOrDefault(); if (query == null) { model.Password = EncryptionProvider.Encrypt(model.Password); result = db.Insert(model); } } } catch (Exception) { result = 2; } } return(result); }
//#region Task Context Menu //private void taskGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e) //{ // Grid grid = sender as Grid; // TreeViewItem item = (grid.TemplatedParent as ContentPresenter).TemplatedParent as TreeViewItem; // item.IsSelected = true; // Task task = item.Header as Task; // MenuItem menu = grid.ContextMenu.Items[0] as MenuItem; // Image img = new Image(); // img.Stretch = Stretch.None; // if (task.Status == Task.StatusPhase.Completed) // { // menu.Header = "_Flag Incomplete"; // img.Source = new BitmapImage(new Uri("pack://application:,,,/Daytimer.Images;component/Images/redflag.png", UriKind.Absolute)); // } // else // { // menu.Header = "_Mark Complete"; // img.Source = new BitmapImage(new Uri("pack://application:,,,/Daytimer.Images;component/Images/greencheck.png", UriKind.Absolute)); // } // menu.Icon = img; //} //private void deleteMenuItem_Click(object sender, RoutedEventArgs e) //{ // TreeViewItem item = (((sender as MenuItem).Parent as ContextMenu).TemplatedParent as ContentPresenter).TemplatedParent as TreeViewItem; // deleteTask(item); //} //private void completeMenuItem_Click(object sender, RoutedEventArgs e) //{ // MarkComplete(); //} //private void collapseAllGroups_Click(object sender, RoutedEventArgs e) //{ // foreach (TreeViewItem each in tasksTreeView.Items) // each.IsExpanded = false; //} //private void expandAllGroups_Click(object sender, RoutedEventArgs e) //{ // foreach (TreeViewItem each in tasksTreeView.Items) // each.IsExpanded = true; //} //private void deleteGroup_Click(object sender, RoutedEventArgs e) //{ // TreeViewItem header = (sender as MenuItem).CommandTarget as TreeViewItem; // int count = header.Items.Count; // for (int i = 0; i < count; i++) // { // TreeViewItem item = header.Items[i] as TreeViewItem; // // Delete the task from the file // Task task = item.Header as Task; // TaskDatabase.Delete(task); // } // if (AnimationHelpers.AnimationsEnabled) // { // AnimationHelpers.DeleteAnimation parentDeleteAnim = new AnimationHelpers.DeleteAnimation(header); // parentDeleteAnim.OnAnimationCompletedEvent += parentDeleteAnim_OnAnimationCompletedEvent; // parentDeleteAnim.Animate(); // if (tasksTreeView.Items.Count <= 1) // { // statusText.Text = "We didn't find anything to show here."; // AnimationHelpers.Fade fade = new AnimationHelpers.Fade(statusText, AnimationHelpers.FadeDirection.In); // } // } // else // { // tasksTreeView.Items.Remove(header); // if (tasksTreeView.Items.Count == 0) // { // statusText.Text = "We didn't find anything to show here."; // statusText.Visibility = Visibility.Visible; // statusText.Opacity = 1; // } // } //} //#endregion #region Drag-reorder items private void tasksTreeView_ItemReorder(object sender, ItemReorderEventArgs e) { if (!e.OldParent.HasItems) { tasksTreeView.Items.Remove(e.OldParent); } int index = e.NewParent.Items.IndexOf(e.Item); UserTask task = e.Item.Header as UserTask; string header = e.NewParent.Header.ToString(); // Hardcode "Today" since tasks due in previous days will also be // shown under "Today": the user will expect the task dragged in // to be changed to DateTime.Now.Date, not some date in the past. if (header == "Today") { task.DueDate = DateTime.Now.Date; } else { task.DueDate = e.NewParent.Tag as DateTime?; } if (task.StartDate > task.DueDate) { task.StartDate = task.DueDate; } e.Item.Header = new UserTask(false); e.Item.Header = task; if (task.IsOverdue && task.Status != UserTask.StatusPhase.Completed) { e.Item.Foreground = new SolidColorBrush(Color.FromArgb(255, 218, 17, 17)); } else { e.Item.Foreground = Brushes.Black; } ItemReorderEventArgs args = new ItemReorderEventArgs(e.Item, e.OldParent, e.NewParent, e.Copied, e.DragDirection); if (TasksView.ApplicationTasksView != null) { TaskReorderCommand.Execute(args, TasksView.ApplicationTasksView); } TaskReorderCommand.MassExecute(args, LoadedTasksPeekContents, this); if (!e.Copied) { TaskDatabase.Delete(task, false); } //if (e.OldParent == e.NewParent && e.DragDirection == DragDirection.Up) // TaskDatabase.Insert(index - 1, task); //else TaskDatabase.Insert(index, task); }