/// <summary> /// Create a new task /// </summary> public bool Add(Task newTask) { try { if (newTask.Title.Length < 5) throw new ArgumentException("Task title too short"); if (MvcApplication.Store == null) throw new NullReferenceException("DocumentStore not available"); using (var session = MvcApplication.Store.OpenSession()) { // var user = session.GetCurrentUser(); var task = new Task(); task.Title = newTask.Title; task.Completed = newTask.Completed; task.LastUpdated = DateTime.Now; // task.UserId = user.Id; // task.UserFullName = user.FullName; session.Store(task); session.SaveChanges(); Clients.taskAdded(task); return true; } } catch (Exception ex) { Caller.reportError(ex.Message); return false; } }
/// <summary> /// Update a task using /// </summary> public bool Update(Task updatedTask) { using (var session = MvcApplication.Store.OpenSession()) { var oldTask = session.Query<Task>().FirstOrDefault(t => t.Id == updatedTask.Id); try { if (updatedTask.Title.Length < 5) throw new Exception("Title length to short"); if (oldTask == null) return false; else { oldTask.Title = updatedTask.Title; oldTask.Completed = updatedTask.Completed; oldTask.LastUpdated = DateTime.Now; session.SaveChanges(); Clients.taskUpdated(oldTask); Caller.reportSuccess("Task updated!"); return true; } } catch (Exception ex) { Caller.reportError("Content too short"); return false; } } }