public async Task <IActionResult> PutMeetup(long id, MeetupDTO _meetup) { if (id != _meetup.GDGMeetup.Id) { return(BadRequest()); // 400 } var meetup = _context.Meetups.Include(m => m.MeetupTags).FirstOrDefault(m => m.Id == id); var tags = _context.Tags.ToList(); var meetupTags = _context.MeetupTag.Include(mt => mt.GDGTag).Include(mt => mt.GDGMeetup).ToList(); meetup.Content = _meetup.GDGMeetup.Content; meetup.SpeakerName = _meetup.GDGMeetup.SpeakerName; meetup.MeetupTags = new List <MeetupTag>(); // Adding non existing tags foreach (GDGTag t in _meetup.Tags) { MeetupTag mt; // Check if the tag already exists in the database if (tags.Contains(t)) { // If Yes, check if the MeetupTag joining the Meetup and Tag already exists mt = meetupTags.FirstOrDefault(mt => (mt.GDGTag.Equals(t) && mt.GDGMeetup.Equals(meetup))); if (mt == null) { // If not, create it mt = new MeetupTag() { GDGMeetup = meetup, GDGTag = tags.First(tagInDB => tagInDB.Equals(t)) }; } } else { // If not, create the Tag and the meetup Tag GDGTag tag = new GDGTag() { TagString = t.TagString }; mt = new MeetupTag() { GDGMeetup = meetup, GDGTag = tag }; } meetup.MeetupTags.Add(mt); } _context.SaveChanges(); _context.Entry(meetup).State = EntityState.Modified; return(NoContent()); // 204 }
public async Task <IActionResult> PutTag(long id, GDGTag _tag) { var tag = await _context.Tags.FindAsync(id); if (tag == null) { return(BadRequest()); // 400 } tag.TagString = _tag.TagString; _context.Entry(tag).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TagExists(id)) { return(NotFound()); // 404 } else { throw; } } return(NoContent()); // 204 }