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(); } }
public string SaveSurveyResult(FormCollection formData) { try { using (var connection = new SurveyAppEntitiesConnection()) { char[] delim = { '-' }; var saveOption = formData["Save-Option-Hidden"]; var userId = Int32.Parse(formData["User-Id-Hidden"]); var surveyId = Int32.Parse(formData["Survey-Id-Hidden"]); var surveyInfo = (from s in connection.SurveyLinkInfoes where s.UserId == userId && s.SurveyId == surveyId select s).SingleOrDefault(); if (saveOption == "1") { surveyInfo.SurveyStatusId = 2; } else if (saveOption == "2") { surveyInfo.SurveyStatusId = 3; } foreach (var key in formData.AllKeys) { if (!key.Contains("Hidden")) { bool saveResult = true; SurveyResult result1 = new SurveyResult() { SurveyId = surveyId, UserId = userId, }; if (key.Contains("Select")) //For Dropdown { result1.EntityId = Int32.Parse(key.Split(delim)[3]); result1.AttrId = Int32.Parse(formData[key].Split(delim)[3]); result1.Value = null; } else { int attr = 0; result1.EntityId = Int32.Parse(key.Split(delim)[1]); if (key.Split(delim).Length > 2) //contains attribute Id incase checkbox, textbox, textArea { attr = Int32.Parse(key.Split(delim)[3]); } else if (formData[key] != null) { attr = Int32.Parse(formData[key].Split(delim)[1]); //incase of radio } //To Save Values for TextBoxes and TextAreas var type = (from a in connection.Attributes.Include("AttributeTypes") where a.AttrId == attr select a.AttributeType.Type).SingleOrDefault(); if (type == null) { saveResult = false; } else if (type == "TextBox" || type == "TextArea") { if (formData[key] != "") { result1.Value = formData[key]; result1.AttrId = Int32.Parse(key.Split(delim)[3]); } else { saveResult = false; } } //For radio , checkbox, else if (type == "Radio") { result1.Value = null; result1.AttrId = Int32.Parse(formData[key].Split(delim)[1]); } else if (type == "CheckBox") { result1.Value = null; result1.AttrId = Int32.Parse(key.Split(delim)[3]); } } if (saveResult == true) { connection.SurveyResults.Add(result1); } } } connection.SaveChanges(); return("Survey Saved Sucessfully"); } } catch (Exception ex) { return(ex.Message); } }