/// <summary> /// Adds a todo-item to the data context. /// </summary> /// <param name="item">Item to add.</param> /// <returns>Primary key for inserted item.</returns> public async Task <int> AddTodo(TodoViewModel item) { using (var context = new TolldoDbContext()) { // Create item to add var itemToAdd = _mapper.Map <Todo>(item); // Add item to database context.Add(itemToAdd); await context.SaveChangesAsync(); return(itemToAdd.Id); } }
/// <summary> /// Adds a subtask-item to the data context. /// </summary> /// <param name="item">Item to add.</param> /// <param name="id">Foreign key to the item.</param> /// <returns>Primary key for inserted item.</returns> public async Task <int> AddSubtask(SubtaskViewModel item, int id) { using (var context = new TolldoDbContext()) { // Create item to add var itemToAdd = _mapper.Map <Subtask>(item); // Add foreign key itemToAdd.TodoTaskId = id; // Add item to database context.Add(itemToAdd); await context.SaveChangesAsync(); return(itemToAdd.Id); } }
/// <summary> /// Deletes a subtask-item from the data context. /// </summary> /// <param name="item">Item to delete.</param> /// <returns>Success value.</returns> public async Task <bool> DeleteSubtask(SubtaskViewModel item) { using (var context = new TolldoDbContext()) { // Get item to remove var itemToRemove = await context.Subtasks.Where(i => i.Id == item.Id).FirstOrDefaultAsync(); if (itemToRemove == null) { return(false); } // Remove item from database context.Remove(itemToRemove); return(await context.SaveChangesAsync() > 0 ? true : false); } }
/// <summary> /// Updates a subtask-item in the data context. /// </summary> /// <param name="item">Item to update.</param> /// <returns>Success value.</returns> public async Task <bool> UpdateSubtask(SubtaskViewModel item) { using (var context = new TolldoDbContext()) { // Get item to update var itemToUpdate = await context.Subtasks.Where(i => i.Id == item.Id).FirstOrDefaultAsync(); _mapper.Map(item, itemToUpdate); if (itemToUpdate == null) { return(false); } // Update item in database context.Update(itemToUpdate); return(await context.SaveChangesAsync() > 0 ? true : false); } }
/// <summary> /// Updates a todo-item in the data context. /// </summary> /// <param name="item">Item to update.</param> /// <returns>Success value.</returns> public async Task <bool> UpdateTodo(TodoViewModel item) { using (var context = new TolldoDbContext()) { // Get item to update var itemToUpdate = await context.Todos.Where(i => i.Id == item.Id).FirstOrDefaultAsync(); _mapper.Map(item, itemToUpdate); if (itemToUpdate == null) { return(false); } // Update item in database itemToUpdate.Tasks = null; context.Update(itemToUpdate); // Ignore tasks context.Entry(itemToUpdate).Collection(x => x.Tasks).IsModified = false; return(await context.SaveChangesAsync() > 0 ? true : false); } }