public async Task <bool> EditPerson(Person person, IList <int> topics) { try { var existingPerson = await _context.People.Include(x => x.Subscriptions).ThenInclude(x => x.Topic).SingleOrDefaultAsync(m => m.Id == person.Id); var newTopics = topics; var deletedTopics = existingPerson.Subscriptions.Where(x => !newTopics.Contains(x.Topic.Id)).ToList(); var addedTopics = newTopics.Except(existingPerson.Subscriptions.Select(o => o.TopicId)); foreach (Subscription subscription in deletedTopics) { existingPerson.Subscriptions.Remove(subscription); } foreach (int newTopic in addedTopics) { existingPerson.Subscriptions.Add(new Subscription() { Person = person, TopicId = newTopic }); } existingPerson.FirstName = person.FirstName; existingPerson.LastName = person.LastName; existingPerson.Telephone = person.Telephone; existingPerson.EMail = person.EMail; _context.Update(existingPerson); await _context.SaveChangesAsync(); return(true); } catch (DbUpdateConcurrencyException) { if (!PersonExists(person.Id)) { return(false); } else { throw; } } }
public async Task <bool> EditTopic(Topic topic) { try { _context.Update(topic); await _context.SaveChangesAsync(); return(true); } catch (DbUpdateConcurrencyException) { if (!TopicExists(topic.Id)) { return(false); } else { throw; } } }