public IActionResult Create(Friend friend) { ModelState.Clear(); TryValidateModel(friend); if (ModelState.IsValid) { using (var db = new FriendsContext()) { // Create a channel var channel = _client.CreateChannel(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), "public", friend.PhoneNumber, friend.PhoneNumber, string.Empty); // join this channel if (channel.RestException!=null) { //an exception occurred making the REST call return Content(channel.RestException.Message); } else{ // Create a user var user = _client.CreateUser(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), friend.PhoneNumber.RemoveSpecialCharacters()); if (user.RestException!=null){ return Content(user.RestException.Message); } else{ // Create membership var member = _client.CreateMember(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), channel.Sid, user.Identity, string.Empty); if (member.RestException!=null){ return Content(member.RestException.Message); } else{ // Add complete user to the DB friend.ChannelSid = channel.Sid; friend.UserSid = user.Sid; db.Friends.Add(friend); db.SaveChanges(); } } } return RedirectToAction("Index"); } } return View(friend); }
public IActionResult Edit(Friend friend) { ModelState.Clear(); TryValidateModel(friend); if (ModelState.IsValid) { using (var db = new FriendsContext()) { db.Friends.Update(friend); var count = db.SaveChanges(); return RedirectToAction("Index"); } } return View(friend); }