//要避免重复加入

        public async Task <IActionResult> AddSchoolAction(AddSchoolAction school)
        {
            var user = await _userManager.GetUserAsync(User);

            var suburbId  = _context.Suburbs.First(a => a.Name == school.SuburbName && a.PostCode == school.PostCode).Id;
            var newSchool = new School {
                DomainExtension = school.DomainExtension, SchoolName = school.SchoolName, NormalizedName = school.SchoolName.ToUpper(), SuburbId = suburbId, NewRequest = true, RequestedBy = user.Id
            };

            _context.Schools.Add(newSchool);
            _context.SaveChanges();
            return(RedirectToAction("EditPersonalInfo"));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddSchoolAction(AddSchoolAction school)
        {
            const int schoolStatus = SchoolStatus.InUse;

            InitialSystemInfo();
            //1. Check if correct input from front-end
            if (!ModelState.IsValid)
            {
                foreach (var model in ModelState)
                {
                    if (model.Value.Errors.Count == 0)
                    {
                        continue;
                    }
                    if ((string)TempData["Error"] != "")
                    {
                        TempData["Error"] += "\n";
                    }
                    if (model.Key == "PostCode")
                    {
                        TempData["Error"] += "Correct postcode is required";
                    }
                    else
                    {
                        foreach (var error in model.Value.Errors)
                        {
                            TempData["Error"] += error.ErrorMessage;
                        }
                    }
                }
                return(RedirectToAction("AddSchool"));
            }
            //2. Check if user entered correct suburb
            var suburbs = _context.Suburbs
                          .Where(a => a.Name == school.SuburbName.ToUpper() && a.PostCode == school.PostCode);

            if (!suburbs.Any())
            {
                TempData["Error"] = "Cannot find your Suburb, Please select correct one";
                return(RedirectToAction("AddSchool"));
            }
            var checkWhetherUpdate = _context.Schools.Where(a =>
                                                            (a.SchoolName == school.SchoolName && a.SuburbId == suburbs.First().Id) &&
                                                            (a.DomainExtension == school.DomainExtension)
                                                            );
            School newSchool;

            //2.Check if new a school or already in database
            //2.1Update a school
            if (checkWhetherUpdate.Any())
            {
                var oldStatus       = checkWhetherUpdate.First().Status;
                var oldLocationName = _context.Suburbs
                                      .First(a => a.Id == checkWhetherUpdate.First().SuburbId).Name;
                newSchool                 = checkWhetherUpdate.First();
                newSchool.SchoolName      = school.SchoolName;
                newSchool.DomainExtension = school.DomainExtension;
                newSchool.Status          = schoolStatus;
                if ((string)TempData["Inform"] != "")
                {
                    TempData["Inform"] += "\n";
                }
                TempData["Inform"] += "It is not a new school. "
                                      + newSchool.SchoolName + " in "
                                      + oldLocationName + " campus was in ";
                if (oldStatus == SchoolStatus.InUse)
                {
                    TempData["Inform"] += "\"In Use\"";
                }
                else if (oldStatus == SchoolStatus.InRequest)
                {
                    TempData["Inform"] += "\"In Request\"";
                }
                else
                {
                    TempData["Inform"] += "\"No Longer Used\"";
                }
            }
            //2.2Insert a new school
            else
            {
                newSchool = new School
                {
                    DomainExtension = school.DomainExtension.ToLower(),
                    SchoolName      = school.SchoolName,
                    NormalizedName  = school.SchoolName.ToUpper(),
                    SuburbId        = suburbs.First().Id,
                    AddedBy         = (await _userManager.GetUserAsync(User)).Id,
                    Status          = SchoolStatus.InUse
                };
            }
            //3. Update user required school
            _context.Schools.Update(newSchool);
            await _context.SaveChangesAsync();

            //4. To update school name if school name changes
            await UpdateRelatedInfo(newSchool.SchoolName, newSchool.DomainExtension);

            TempData["Success"] = "School inserted successfully";
            return(RedirectToAction("AddSchool"));
        }