public async Task<IHttpActionResult> EventMembers(EventMembersDTO model) { if (!ModelState.IsValid) return BadRequest(); var result = await _memberService.UpdateEventMembers(model); if(result.Success) return Ok(); return BadRequest(result.Error); }
public async Task<ValidationResult> UpdateEventMembers(EventMembersDTO model) { try { //First load the members var members = await _eventMemberRepo.Get(x => x.EventId == model.EventId) .ToListAsync(); //Split the Ids string and create list of integers List<int> ids = model.MemberIds.Split(',').Select(int.Parse).ToList(); //Create a new list of members List<EventMember> newMembersList = ids.Select(id => new EventMember { EventId = model.EventId, UserId = id }).ToList(); foreach (var m in members) { _eventMemberRepo.Delete(m); } //First check we have at least the owner member if (newMembersList.Count < 1) return AddError("Unable to update event members"); //Insert the rebuilt list of users foreach (var newMem in newMembersList) { _eventMemberRepo.Insert(newMem); } await SaveChangesAsync(); Result.Success = true; return Result; } catch (Exception ex) { return AddError(ex.Message); } }