public ActionResult GuardianInformation(Applicant applicant)
        {
            // Is the lottery closed?
            if (IsLotteryClosed()) // TODO implement this as an class-level annotation instead
            {
                return(RedirectToAction(actionName: "LotteryClosed"));
            }

            // Make sure someone isn't playing with the ID from the form
            if (!IsAuthorizedApplicant(applicant) || !IsActiveSession()) // TODO Use AOP/Annotations to do this instead
            {
                return(RedirectToAction("Index"));
            }

            // Check required fields
            viewHelper.EmptyCheckGuardianInformation(ModelState, applicant);

            // Validate model
            if (ModelState.IsValid)
            {
                SaveGuardianInformation(applicant);

                return(RedirectToAction("SchoolSelection"));
            }

            // Invalid model
            viewHelper.PrepareGuardianInformationView(ViewBag);
            return(View(applicant));
        }
Пример #2
0
        public ActionResult EditApplicant(Applicant applicant, FormCollection formCollection)
        {
            // Verify that the applicant exists
            var queriedApplicant = db.Applicants.Find(applicant.ID);

            if (queriedApplicant == null)
            {
                return(HttpNotFound());
            }

            // Empty check student and guardian information
            viewHelper.EmptyCheckStudentInformation(ModelState, applicant);
            viewHelper.EmptyCheckGuardianInformation(ModelState, applicant);

            // School selection check //TODO Make this code shareable with the parent side
            var schoolIds = new List <int>();

            if (formCollection["programs"] == null || !formCollection["programs"].Any())
            {
                ModelState.AddModelError("programs", GoldenTicketText.NoSchoolSelected);
                PrepareEditApplicantView(applicant);
                return(View(applicant));
            }
            else
            {
                var programIdStrs = formCollection["programs"].Split(',').ToList();
                programIdStrs.ForEach(idStr => schoolIds.Add(int.Parse(idStr)));
            }

            if (!ModelState.IsValid)
            {
                PrepareEditApplicantView(applicant);
                return(View(applicant));
            }

            // Remove existing applications for this user
            var applieds = db.Applieds.Where(applied => applied.ApplicantID == applicant.ID).ToList();

            applieds.ForEach(a => db.Applieds.Remove(a));

            // Add new Applied associations (between program and program)
            var populatedApplicant = db.Applicants.Find(applicant.ID);

            foreach (var programId in schoolIds)
            {
                var applied = new Applied();
                applied.ApplicantID = applicant.ID;
                applied.SchoolID    = programId;

                // Confirm that the program ID is within the city lived in (no sneakers into other districts)
                var program = db.Schools.Find(programId);
                if (program != null && program.City.Equals(populatedApplicant.StudentCity, StringComparison.CurrentCultureIgnoreCase))
                {
                    db.Applieds.Add(applied);
                }
            }

            // Save changes to the database
            db.Applicants.AddOrUpdate(applicant);
            db.SaveChanges();

            return(RedirectToAction("ViewApplicant", new{ id = applicant.ID }));
        }