public CustomerCardRegistrationDataModel(Model contest, CustomerCardRegistrationModel userAnswers, bool isOfficial) : this(contest, isOfficial) { this.Questions = this.Questions.Select(x => { var userAnswer = userAnswers.Questions.FirstOrDefault(y => y.QuestionId == x.Id); return new CustomerCardQuestionDataModel { // Answer = userAnswer == null ? null : userAnswer.Answer, Id = x.Id, Text = x.Text // Question = x.Question, // Type = x.Type, // PossibleAnswers = x.PossibleAnswers }; }); }
public IHttpActionResult Register(bool official, CustomerCardRegistrationModel registrationData) { // check if the user has already registered for participation and redirect him to the correct action var participantFound = this.Data.Customers.Any(registrationData.ContestId, this.UserProfile.Id, official); if (participantFound) { return this.Get(registrationData.ContestId, official); } var contest = this.Data.CustomerCards.GetById(registrationData.ContestId); ValidateContest(contest, official); if (official && contest.HasCustomerCardPassword) { if (string.IsNullOrEmpty(registrationData.Password)) { return this.BadRequest("Password"); } else if (contest.CustomerCardPassword != registrationData.Password) { return this.BadRequest("Password"); } } var questionsToAnswerCount = official ? contest.Questions.Count(x => x.AskOfficialCustomers) : contest.Questions.Count(x => x.AskPracticeCustomers); if (questionsToAnswerCount != registrationData.Questions.Count()) { return this.BadRequest("Questions"); } var contestQuestions = contest.Questions.ToList(); var participant = new Customer(registrationData.ContestId, this.UserProfile.Id, official); this.Data.Customers.Add(participant); var counter = 0; foreach (var question in registrationData.Questions) { var contestQuestion = contestQuestions.FirstOrDefault(x => x.Id == question.QuestionId); var regularExpression = contestQuestion.RegularExpressionValidation; bool correctlyAnswered = false; if (!string.IsNullOrEmpty(regularExpression)) { correctlyAnswered = Regex.IsMatch(question.Answer, regularExpression); } participant.Answers.Add(new CustomerAnswer { CustomerCardQuestionId = question.QuestionId, Answer = question.Answer }); counter++; } if (!this.ModelState.IsValid) { return this.Ok(new CustomerCardRegistrationDataModel(contest, registrationData, official)); } this.Data.SaveChanges(); return this.Get(registrationData.ContestId, official); }