/// <summary> /// Adds a new todo item do the database /// </summary> /// <param name="item">The item to persist</param> /// <returns>The Id of the newly inserted item in DB if successful, else 0</returns> public int AddTodoItem(TodoItem item) { string storedProcToExecute = "AddTodoItem"; int id = 0; using (SqlConnection connection = new SqlConnection(this.connectionString)) { using (SqlCommand myCommand = new SqlCommand(storedProcToExecute, connection)) { myCommand.CommandTimeout = 10; myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add("@Description", SqlDbType.VarChar).Value = item.Description; myCommand.Parameters.Add("@Deadline", SqlDbType.DateTime2).Value = item.Deadline; myCommand.Parameters.Add("@IsCompleted", SqlDbType.Bit).Value = item.IsCompleted; myCommand.Parameters.Add("@Details", SqlDbType.VarChar).Value = item.Details; SqlParameter outputIdParam = new SqlParameter("@Id", SqlDbType.Int) { Direction = ParameterDirection.Output }; myCommand.Parameters.Add(outputIdParam); connection.Open(); myCommand.ExecuteNonQuery(); id = outputIdParam.Value as int? ?? default(int); } } return id; }
public void CanAddItem() { string expectedDescription = "description"; string expectedDetails = "details"; bool expectedIsCompleted = true; DateTime expectedDeadline = DateTime.Now; TodoItem item = new TodoItem { Description = expectedDescription, Details = expectedDetails, IsCompleted = expectedIsCompleted, Deadline = expectedDeadline }; // using transaction scope here so I can test adding items without adding // garbage data to db everytime I run unit tests using (TransactionScope scope = new TransactionScope()) { int id = this.repository.AddTodoItem(item); Assert.IsTrue(id > 0); TodoItem actual = this.repository.GetTodoItem(id); Assert.IsNotNull(actual); Assert.AreEqual(expectedDescription, actual.Description); Assert.AreEqual(expectedDetails, actual.Details); Assert.AreEqual(expectedIsCompleted, actual.IsCompleted); Assert.AreEqual(expectedDeadline, actual.Deadline); } }
/// <summary> /// Updates the todo item with the specified id to be the same as the passed in item /// </summary> /// <param name="id">The id of the item to update</param> /// <param name="item">The updated version of the item</param> /// <exception cref="ArgumentNullException">Thrown when item is null</exception> public void UpdateTodoItem(int id, TodoItem item) { if(item == null) { throw new ArgumentNullException("item"); } this.repository.UpdateTodoItem(id, item); }
/// <summary> /// Add a todo item to the list /// </summary> /// <param name="item">The new item to add</param> /// <exception cref="ArgumentNullException">Throw when item is null</exception> public void AddTodoItem(TodoItem item) { if(item == null) { throw new ArgumentNullException("item"); } this.repository.AddTodoItem(item); }
public MainWindow() { InitializeComponent(); // Prepare the task creation panel PriorityComboBox.ItemsSource = TodoItem.PriorityFlags; _NewTodo = new TodoItem {Priority = PriorityFlag.Normal, DueDate = DateTime.Today}; NewItemPanel.DataContext = _NewTodo; //Making sure we do not call the web service at design time if (!DesignerProperties.GetIsInDesignMode(new DependencyObject())) { _Service = new ServiceReference1.TodoListServiceClient(); _Service.BeginGetItems(TodoListLoaded, null); } }
private void TodoItemCreated(IAsyncResult result) { if(result.IsCompleted) { // FORWARD TO UI THREAD Dispatcher.BeginInvoke( DispatcherPriority.Normal, (DispatcherOperationCallback)delegate(object arg) { // HANDLED ON UI THREAD _NewTodo.ID = _Service.EndCreateItem(result); _NewTodo.PropertyChanged += Item_PropertyChanged; _ToDoItems.Add(_NewTodo); _NewTodo = new TodoItem { Priority = PriorityFlag.Normal, DueDate = DateTime.Today }; NewItemPanel.DataContext = _NewTodo; TitleTextBox.Focus(); return null; }, null); } }
/// <summary> /// Get all todo items /// </summary> /// <returns>A list of all todo items</returns> public List<TodoItem> GetAllItems() { List<TodoItem> items = new List<TodoItem>(); string storedProcToExecute = "GetAllItems"; using (SqlConnection connection = new SqlConnection(this.connectionString)) { using (SqlCommand myCommand = new SqlCommand(storedProcToExecute, connection)) { myCommand.CommandTimeout = 10; myCommand.CommandType = CommandType.StoredProcedure; connection.Open(); using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) { TodoItem todoItem = new TodoItem { Id = Convert.ToInt32(reader["Id"]), Description = Convert.ToString(reader["Description"]), Deadline = Convert.ToDateTime(reader["Deadline"]), IsCompleted = Convert.ToBoolean(reader["IsCompleted"]), Details = Convert.ToString(reader["Details"]) }; items.Add(todoItem); } } } } return items; }
/// <summary> /// Update a todo item specified by the id to have the same properties as the given item /// </summary> /// <param name="id">The id of the item to update</param> /// <param name="item">The updated version of the item</param> public void UpdateTodoItem(int id, TodoItem item) { string storedProcToExecute = "UpdateTodoItem"; using (SqlConnection connection = new SqlConnection(this.connectionString)) { using (SqlCommand myCommand = new SqlCommand(storedProcToExecute, connection)) { myCommand.CommandTimeout = 10; myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add("@Id", SqlDbType.Int).Value = item.Id; myCommand.Parameters.Add("@Description", SqlDbType.VarChar).Value = item.Description; myCommand.Parameters.Add("@Deadline", SqlDbType.DateTime2).Value = item.Deadline; myCommand.Parameters.Add("@IsCompleted", SqlDbType.Bit).Value = item.IsCompleted; myCommand.Parameters.Add("@Details", SqlDbType.VarChar).Value = item.Details; connection.Open(); myCommand.ExecuteNonQuery(); } } }
/// <summary> /// Get a single todo item by id /// </summary> /// <param name="id">the id of the item to get</param> /// <returns>The todo item specified by the id if exists, else null</returns> public TodoItem GetTodoItem(int id) { TodoItem todoItem = null; string storedProcToExecute = "GetTodoItem"; using (SqlConnection connection = new SqlConnection(this.connectionString)) { using (SqlCommand myCommand = new SqlCommand(storedProcToExecute, connection)) { myCommand.CommandTimeout = 10; myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add("@Id", SqlDbType.Int).Value = id; connection.Open(); using (SqlDataReader reader = myCommand.ExecuteReader()) { if (reader.Read()) { todoItem = new TodoItem { Id = Convert.ToInt32(reader["Id"]), Description = Convert.ToString(reader["Description"]), Deadline = Convert.ToDateTime(reader["Deadline"]), IsCompleted = Convert.ToBoolean(reader["IsCompleted"]), Details = Convert.ToString(reader["Details"]) }; } } } } return todoItem; }
public void WhenGettingItem_IfItemNotNullGetsOKResponse() { TodoItem item = new TodoItem { Description = "description", Details = "details", Deadline = DateTime.Now, IsCompleted = false, }; this.mockManager.Setup(x => x.GetTodoItem(It.IsAny<int>())).Returns(item); var response = this.controller.Get(0); Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.OK); }
public void CanUpdateItem() { using (TransactionScope scope = new TransactionScope()) { TodoItem item = new TodoItem { Description = "description1", Details = "details1", IsCompleted = false, Deadline = DateTime.Now, }; int id = this.repository.AddTodoItem(item); TodoItem item2 = new TodoItem { Description = "description2", Details = "details2", IsCompleted = true, Deadline = DateTime.Now.AddDays(2), }; this.repository.UpdateTodoItem(id, item2); TodoItem actual = this.repository.GetTodoItem(id); Assert.IsNotNull(actual); Assert.AreNotEqual(item2.Description, actual.Description); Assert.AreNotEqual(item2.Details, actual.Details); Assert.AreNotEqual(item2.IsCompleted, actual.IsCompleted); Assert.AreNotEqual(item2.Deadline, actual.Deadline); } }