public void Update(TodoItem todoItem, Guid userId) { if (todoItem.UserId != userId) { throw new TodoAccessDeniedException("Can't update item. User is not owner of item."); } _context.Entry(todoItem).State = EntityState.Modified; _context.SaveChanges(); }
public bool MarkAsCompleted(Guid todoId, Guid userId) { var todoItem = Get(todoId, userId); if (todoItem == null || !todoItem.MarkAsCompleted()) { return(false); } using (var db = new TodoDbContext(_connectionString)) { db.Entry(todoItem).State = EntityState.Modified; db.SaveChanges(); } return(true); }
public void Update(TodoItem todoItem, Guid userId) { var oItem = Get(todoItem.Id, userId); if (oItem == null) { Add(todoItem); return; } using (var db = new TodoDbContext(_connectionString)) { db.Entry(todoItem).State = EntityState.Modified; db.SaveChanges(); } }
/// <summary > /// Updates given TodoItem in database . /// If TodoItem does not exist , method will add one . Throw ///TodoAccessDeniedException with appropriate message if user is not the owner of the Todo item /// </ summary > /// <param name =" todoItem "> Todo item </ param > /// <param name =" userId ">Id of the user that is trying to update the data</ param> public void Update(TodoItem todoItem, Guid userId) { if (todoItem.UserId != userId) { throw new TodoAccessDeniedException("User cannot update TodoItem beacuse he isn't the owner!"); } if (_context.TodoItems.Any(item => item.Id == todoItem.Id)) { Add(todoItem); } else { _context.Entry(todoItem).State = EntityState.Modified; _context.SaveChanges(); } }