public bool Delete(int id) { var result = false; try { using (var context = new ToDoContext()) { var toDo = context.ToDos.SingleOrDefault(x => x.Id == id); if (toDo != null) { context.ToDos.Remove(toDo); context.SaveChanges(); Clients.All.toDoRemoved(id); result = true; } } } catch (Exception) { Clients.Caller.raiseError("Unable to update ToDo Entry"); } return result; }
/// <summary> /// This add a new To Do entry into the context and then calls the client function /// </summary> /// <param name="newToDo">A To Do object</param> /// <returns>Bool if it was successful</returns> public bool Add(ToDo newToDo) { var result = false; try { using (var context = new ToDoContext()) { var toDo = context.ToDos.Create(); toDo.Title = newToDo.Title; toDo.Description = newToDo.Description; toDo.CompleteDate = newToDo.CompleteDate; context.ToDos.Add(toDo); context.SaveChanges(); Clients.All.toDoCreated(toDo); result = true; } } catch (Exception) { Clients.Caller.raiseError("Unable to create a new ToDo Entry."); } return result; }
/// <summary> /// This function gets all the To Do Entries in the context and then returns them /// to the client by calling a client side function /// </summary> public void GetAll() { using (var context = new ToDoContext()) { var toDos = context.ToDos.ToArray(); Clients.Caller.allToDosRetrieved(toDos); } }
/// <summary> /// This updates a To Do entry into the context and then calls the client function /// </summary> /// <param name="updatedToDo">A To Do object</param> /// <returns>Bool if it was successful</returns> public bool Update(ToDo updatedToDo) { var result = false; try { using (var context = new ToDoContext()) { var toDo = context.ToDos.SingleOrDefault(x => x.Id == updatedToDo.Id); if (toDo != null) { toDo.Title = updatedToDo.Title; toDo.Description = updatedToDo.Description; toDo.CompleteDate = updatedToDo.CompleteDate; context.ToDos.Add(toDo); context.SaveChanges(); Clients.All.toDoUpdated(toDo); result = true; } } } catch (Exception) { Clients.Caller.raiseError("Unable to update ToDo Entry."); } return result; }