Esempio n. 1
0
        /// <summary>
        /// Creates Counselor if no matching counselor was found in database.
        /// </summary>
        /// <param name="ccaVm"></param>
        /// <param name="cca"></param>
        /// <param name="student"></param>
        /// <returns></returns>
        private async Task CounselorCreate(CCAViewModel ccaVm, CCA cca, Student student)
        {
            // Homeschoolers will be assigned Counselor at the ID=0 entry (now Cory Kanth)
            try
            {
                if (cca.EnrollmentLocationID == GlobalVariables.PRIVATESCHOOLID || (cca.EnrollmentLocationID != GlobalVariables.HOMESCHOOLID && cca.CounselorID == 0))
                {
                    int counselorId = 0;
                    // Check for existing counselor entry by email address
                    if (ccaVm.CounselorEmail != null)
                    {
                        counselorId = await db.Counselors.Where(x => x.Email == ccaVm.CounselorEmail).Select(x => x.ID).FirstOrDefaultAsync().ConfigureAwait(false);
                    }

                    if (counselorId != 0)
                    {
                        cca.CounselorID = counselorId;
                    }
                    else // Create a counselor entry in the table.
                    {

                        var counselor = new Counselor()
                        {
                            Email = ccaVm.CounselorEmail,
                            FirstName = ccaVm.CounselorFirstName,
                            LastName = ccaVm.CounselorLastName,
                            Phone = ccaVm.CounselorPhoneNumber
                        };

                        // Find school name for display purposes 

                        if (cca.EnrollmentLocationID != GlobalVariables.PRIVATESCHOOLID)
                        {
                            counselor.EnrollmentLocationID = cca.EnrollmentLocationID;
                            counselor.EnrollmentLocationSchoolNameID = student.EnrollmentLocationSchoolNamesID;
                            var school = await cactus.CactusSchools.FirstOrDefaultAsync(m => m.ID == student.EnrollmentLocationSchoolNamesID).ConfigureAwait(false);

                            counselor.School = school.Name;

                        }
                        else
                        {
                            counselor.School = student.SchoolOfRecord;
                        }

                        db.Counselors.Add(counselor);

                        await db.SaveChangesAsync().ConfigureAwait(false);

                        // Lookup counselor in updated table to assign new ID to CCA
                        cca.CounselorID = await db.Counselors.Where(m => m.Email == ccaVm.CounselorEmail).Select(m => m.ID).FirstOrDefaultAsync().ConfigureAwait(false);
                    }
                }
            }
            catch
            {
                throw;
            }
        }
Esempio n. 2
0
        public async Task<ActionResult> Create([Bind(Include = "Email, FirstName, LastName, Phone, CactusID, EnrollmentLocationID, EnrollmentLocationSchoolNameID, School, CounselorID")] CounselorViewModel counselorVm)
        {
            if (ModelState.IsValid)
            {
                Mapper.CreateMap<CounselorViewModel, Counselor>();

                Counselor counselor;

                if (counselorVm.CounselorID == 0)
                {
                    counselor = new Counselor();
                }
                else
                {
                    counselor = await db.Counselors.FindAsync(counselorVm.CounselorID).ConfigureAwait(false);
                }

                // Mapper causes all properties in the ViewModel to update the Counselor object so errors or not required empty properties will overwrite
                // properties in the database. (CactusID is one known problem.)
                // TODO: write own mapper type method that will check for nulls or known errors.

                if ((counselorVm.CactusID == 0 || counselorVm.CactusID == null) && counselor.CactusID != null)
                    counselorVm.CactusID = counselor.CactusID;

                counselor = Mapper.Map<CounselorViewModel, Counselor>(counselorVm, counselor);

                counselor.UserId = User.Identity.GetUserId();;

                // Remove all non numeric chars
                counselor.Phone = JustDigits(counselor.Phone);


                // If counselor not found in database need to add
                if (counselor.ID == 0)
                    db.Counselors.Add(counselor);


                var count = await db.SaveChangesAsync();

                if (count != 0) // Set account setup to true if successfully added
                {
                    var user = db.Users.Find(counselor.UserId);
                    user.IsSetup = true;
                    await db.SaveChangesAsync().ConfigureAwait(false);
                }
                else
                {
                    ViewBag.Message = "Unable to save Counselor!";
                    return View("Error");
                }

                TempData["UserType"] = "Counselor School User";

                return RedirectToAction("EmailAdminToConfirm", "Account");
            }

            // Add model state errors since fields with errors are hidden on repost.

            var errors = ModelState.Select(x => x.Value.Errors).Where(y => y.Count > 0).ToList();
            foreach (var error in errors)
                ModelState.AddModelError("", error.Select(x => x.ErrorMessage).First());

            var leas = await cactus.CactusInstitutions.ToListAsync().ConfigureAwait(false);

            leas.Insert(0, new CactusInstitution() { Code = "", Name = "District", ID = 0 });

            ViewBag.EnrollmentLocationID = new SelectList(leas, "ID", "Name");
            ViewBag.EnrollmentLocationSchoolNameID = new List<SelectListItem>();
            ViewBag.CounselorID = new List<SelectListItem>();
            return View(counselorVm);
        }