public bool AddUser(string Name, string Email, int SelectedSurvey) { using (var connection = new SurveyAppEntitiesConnection()) { string globalId; //1. If user not there create one new var userId = (from u in connection.Users where u.Name == Name && u.Email == Email select u.Id).SingleOrDefault(); var i = (from s in connection.SurveyLinkInfoes where s.SurveyId == SelectedSurvey && s.UserId == userId select s.GUID).SingleOrDefault(); if (i != null) { return(false); } if (userId == null) { User u = new User() { Name = Name, Email = Email }; connection.Users.Add(u); connection.SaveChanges(); userId = (from newUser in connection.Users where newUser.Name == Name && newUser.Email == Email select newUser.Id).SingleOrDefault(); } //2. Create GUID for that USer globalId = Guid.NewGuid().ToString(); //3. create entry in USerSurvey table for Survey, User,Link,SurveyStatus SurveyLinkInfo link = new SurveyLinkInfo() { GUID = globalId, UserId = userId, SurveyId = SelectedSurvey, SurveyStatusId = 1 //(Not Started) }; connection.SurveyLinkInfoes.Add(link); connection.SaveChanges(); return(true); } }
public void AddUser(UserBaseModel user) { using (var connection = new SurveyAppEntitiesConnection()) { string globalId; var surveyId = Int32.Parse(user.SelectedSurvey); //1. If user not there create one new var userId = (from u in connection.Users where u.Name == user.Name && u.Email == user.Email select u.Id).SingleOrDefault(); if (userId == null || userId == 0) { User u = new User() { Name = user.Name, Email = user.Email }; connection.Users.Add(u); connection.SaveChanges(); userId = (from newUser in connection.Users where newUser.Name == user.Name && newUser.Email == user.Email select newUser.Id).SingleOrDefault(); } //2. Create GUID for that USer globalId = Guid.NewGuid().ToString(); //3. create entry in USerSurvey table for Survey, User,Link,SurveyStatus SurveyLinkInfo link = new SurveyLinkInfo() { GUID = globalId, UserId = userId, SurveyId = surveyId, SurveyStatusId = 1, //(Not Started) ApplicationKey = user.AppKey, ClientId = user.ClientId }; connection.SurveyLinkInfoes.Add(link); connection.SaveChanges(); } }