public async Task Update(TodoItem todoItem, Guid userId) { TodoItem item = await _context.TodoItems.FirstOrDefaultAsync(t => t.Id.Equals(todoItem.Id)); if (item != null && !item.UserId.Equals(userId)) { throw new TodoAccessDeniedException("User is not the owner of the Todo item"); } if (item == null) { _context.TodoItems.Add(todoItem); } else { _context.Entry(todoItem).State = EntityState.Modified; item = todoItem; } _context.SaveChanges(); }
public async Task UpdateAsync(TodoItem todoItem, Guid userId) { if (!_context.TodoItem.Contains(todoItem)) { await AddAsync(todoItem); } if (!todoItem.UserId.Equals(userId)) { throw new TodoAccessDeniedException("User " + userId + " is not the owner of the TodoItem."); } _context.Entry(GetAsync(todoItem.Id, userId)).CurrentValues.SetValues(todoItem); await _context.SaveChangesAsync(); }