/// <summary> /// Processes Test /// </summary> /// <param name="form"></param> /// <param name="user"></param> /// <returns>The list of errors for the test.</returns> /// <remarks> /// 1. creates new Test entity. /// 2. loops each question and creates a Answer entity for each question. /// I have added validation so that the user has to answer all questions. /// 3. I add the test to the user. /// 4. Save Changes to Database and redirect to Completed Page. /// </remarks> public IList<ValidationResult> ProcessTest(User user, FormCollection form) { List<ValidationResult> errors = new List<ValidationResult>(); try { this.AttachUserToContext(user); var test = new Models.Test() { DateCreated = DateTime.Now }; foreach (var item in context.Questions) { var optionid = form[item.QuestionId.ToString()]; if (optionid != null) { test.Answers.Add(new Models.Answer() { QuestionOptionId = int.Parse(optionid) }); } else { errors.Add(new ValidationResult() { Name = item.QuestionId.ToString(), Message = "Please Answer the question" }); } } if (errors.Count() > 0) return errors; user.Tests.Add(test); context.SaveChanges(); } catch (Exception ex) { errors.Add(new ValidationResult() { Name = "", Message = ex.Message }); } return errors; }
/// <summary> /// Create a new User object. /// </summary> /// <param name="userId">Initial value of the UserId property.</param> /// <param name="name">Initial value of the Name property.</param> /// <param name="phone">Initial value of the Phone property.</param> public static User CreateUser(global::System.Int32 userId, global::System.String name, global::System.String phone) { User user = new User(); user.UserId = userId; user.Name = name; user.Phone = phone; return user; }
/// <summary> /// Attaches/Adds the user to the context. /// </summary> /// <param name="user"></param> private void AttachUserToContext(User user) { if (user.UserId != 0) { context.Users.Attach(user); } else { context.Users.AddObject(user); } }
/// <summary> /// Deprecated Method for adding a new object to the Users EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToUsers(User user) { base.AddObject("Users", user); }