public void AddTag([FromBody] AddTagParameters inc) { var tag = context.Tags.Where(t => t.Id == inc.TagId).Include(t => t.Tasks).Single(); var task = context.Tasks.Where(tt => tt.Id == inc.TaskId).Include(t => t.Tags).Single(); if (tag != null && task != null) { var tasktag = new TaskTag { TagId = tag.Id, TaskId = task.Id }; task.Tags.Add(tasktag); tag.Tasks.Add(tasktag); context.SaveChanges(); } }
public void RemoveTag([FromBody] AddTagParameters inc) { Tag tag = context.Tags.Where(t => t.Id == inc.TagId).Include(t => t.Tasks).Single(); if (tag != null) { foreach (var connection in tag.Tasks) { if (inc.TaskId == connection.TaskId) { Task taskToDeleteFromTag = context.Tasks.Where(tt => tt.Id == inc.TaskId).Include(tt => tt.Tags).Single(); tag.Tasks.Remove(connection); taskToDeleteFromTag.Tags.Remove(connection); } else { throw new InvalidOperationException("Selected Tag is not a member of selected Task"); } } } }