//INFO: Adds a new survey to the database public bool SubmitSurvey(SurveyDO surveyDO) { using (riverkeeperEntities RKEntities = new riverkeeperEntities()) { Console.WriteLine("Attempting to submit survey"); Survey surveydb = new Survey(); if (surveyDO != null) { surveydb.Name = surveyDO.Name; surveydb.CreationDate = DateTime.Now; surveydb.UserId = (from u in RKEntities.Users where u.EmailAddress.Equals(surveyDO.Email) select u.UserId).FirstOrDefault(); int numberOfQuestions = surveyDO.Answers.Count(); for (int i = 0; i < numberOfQuestions; i++) { AnswerDO answerDO = surveyDO.Answers.ElementAt(i); Answer answerdb = new Answer(); answerdb.Response = answerDO.Response; answerdb.QuestionId = answerDO.QuestionId; surveydb.Answers.Add(answerdb); } RKEntities.Surveys.Add(surveydb); } int dbResult = 0; try { dbResult = RKEntities.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } catch (DbUpdateException e) { Debug.WriteLine(e.InnerException); } if (dbResult == 0) { return(false); } return(true); } }
//INFO: Creates new user public bool CreateUser(UserDO userDO) { using (riverkeeperEntities RKEntities = new riverkeeperEntities()) { User userdb = new User(); if (userDO != null) { userdb.FirstName = userDO.FirstName; userdb.LastName = userDO.LastName; userdb.EmailAddress = userDO.EmailAddress; userdb.PhoneNumber = userDO.PhoneNumber; userdb.Type = userDO.Type; userdb.ZipCode = userDO.ZipCode; RKEntities.Users.Add(userdb); } int dbResult = 0; try { dbResult = RKEntities.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } if (dbResult != 1) { return(false); } return(true); } }
//INFO: Adds a question to the database. public bool CreateQuestion(QuestionDO questionDO) { using (riverkeeperEntities RKEntities = new riverkeeperEntities()) { Question questiondb = new Question(); questiondb.SurveyId = questionDO.SurveyId; questiondb.Wording = questionDO.Wording; questiondb.Type = (ResponseFormat)questionDO.Type; questiondb.PossibleAnswers = String.Join(",", questionDO.PossibleAnswers); RKEntities.Questions.Add(questiondb); int dbResult = 0; try { dbResult = RKEntities.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } if (dbResult != 1) { return(false); } return(true); } }