async public Task <IHttpActionResult> RemoveMentor([FromUri] SPHostedParameters parameters, [FromBody] UserMentorSubmissionModel model) { var userid = 0; try { var spContext = SharePointApiControllerAcsContextProvider.Current.GetSharePointContext(ControllerContext); //var searchName = parameters.searchName; using (var clientContext = spContext.CreateUserClientContextForSPAppWeb()) { if (clientContext != null) { var spuser = clientContext.Web.CurrentUser; clientContext.Load(spuser, l => l.Title, l => l.Id, l => l.LoginName, l => l.Email); clientContext.ExecuteQuery(); userid = spuser.Id; var userInList = GetUserById(clientContext, userid); var mentorInList = GetUserById(clientContext, model.mentorId); var mentorUser = clientContext.Web.GetUserById(mentorInList.IdentityLoginId); clientContext.Load(mentorUser, l => l.Title, l => l.Id, l => l.LoginName, l => l.Email); clientContext.ExecuteQuery(); using (var pfeContext = new PfeContext()) { var currentProfile = await pfeContext.UserEntities.FirstOrDefaultAsync(w => w.IdentityObjectId == userid); var mentorProfile = await pfeContext.UserEntities.FirstOrDefaultAsync(f => f.IdentityObjectId == model.mentorId); var currentRelationship = await pfeContext.RelationshipEntities .FirstOrDefaultAsync(w => w.MenteeId == currentProfile.Id && w.MentorId == mentorProfile.Id); if (currentRelationship != null) { pfeContext.RelationshipEntities.Remove(currentRelationship); } var totalRows = pfeContext.SaveChanges(); LogProvider.LogInformation("Saving profile and removing relationships for {0} with total commit rows:{1}", userid, totalRows); } } } } catch (Exception ex) { LogProvider.LogError(ex, "Failed to save relatonships {0}", model.mentorId); return(BadRequest(string.Format("Failed to remove relationship for {0}", model.mentorId))); } return(Ok("successfully removed relationships, if they existed")); }
async public Task <IHttpActionResult> AddMentor([FromUri] SPHostedParameters parameters, [FromBody] UserMentorSubmissionModel model) { var userid = 0; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var spContext = SharePointApiControllerAcsContextProvider.Current.GetSharePointContext(ControllerContext); //var searchName = parameters.searchName; using (var clientContext = spContext.CreateUserClientContextForSPAppWeb()) { if (clientContext != null) { var spuser = clientContext.Web.CurrentUser; clientContext.Load(spuser, l => l.Title, l => l.Id, l => l.LoginName, l => l.Email); clientContext.ExecuteQuery(); userid = spuser.Id; var userInList = GetUserById(clientContext, userid); var mentorInList = GetUserById(clientContext, model.mentorId); var mentorUser = clientContext.Web.GetUserById(mentorInList.IdentityLoginId); clientContext.Load(mentorUser, l => l.Title, l => l.Id, l => l.LoginName, l => l.Email); clientContext.ExecuteQuery(); using (var pfeContext = new PfeContext()) { var currentProfile = await pfeContext.UserEntities.FirstOrDefaultAsync(w => w.IdentityObjectId == userid); if (currentProfile == null) { // new up currentProfile = new Data.Entities.ProfileEntity() { DisplayName = spuser.Title, Email = spuser.Email, IdentityObjectId = spuser.Id, IdentityLogin = spuser.LoginName }; pfeContext.UserEntities.Add(currentProfile); } var mentorProfile = await pfeContext.UserEntities.FirstOrDefaultAsync(f => f.IdentityObjectId == model.mentorId); if (mentorProfile == null) { // new up mentorProfile = new Data.Entities.ProfileEntity() { DisplayName = mentorUser.Title, Email = mentorUser.Email, Id = mentorUser.Id, IdentityLogin = mentorUser.LoginName, IdentityObjectId = mentorUser.Id }; pfeContext.UserEntities.Add(mentorProfile); } var mentorRelationships = new Data.Entities.ProfileRelationshipEntity() { MenteeLookup = currentProfile, MentorLookup = mentorProfile, Established = DateTime.Now, SuggestedOutcome = model.comments ?? "This demo is awesome" }; pfeContext.RelationshipEntities.Add(mentorRelationships); var totalRows = pfeContext.SaveChanges(); LogProvider.LogInformation("Saving profile and relationships for {0} with total commit rows:{1}", userid, totalRows); } } } } catch (Exception ex) { LogProvider.LogError(ex, "Failed to save relatonships {0}", model.mentorId); return(BadRequest(string.Format("Failed to save relationships for mentor {0}", model.mentorId))); } return(Ok("Successfully saved relationships.")); }