示例#1
0
        public IActionResult EditPersonalInfo(EditPersonalInfo dto)
        {
            var student        = _studentRepo.GetById(dto.StudentId);
            var favoriteCourse = Course.FromId(dto.FavoriteCourseId);
            var suffix         = Suffix.FromId(dto.NameSuffixId);

            var emailResult = Email.Create(dto.Email);

            if (emailResult.IsFailure)
            {
                return(BadRequest(emailResult.Error));
            }

            var nameResult = Name.Create(dto.FirstName, dto.LastName, suffix);

            if (nameResult.IsFailure)
            {
                return(BadRequest(nameResult.Error));
            }

            student.EditPersonalInfo(nameResult.Value, emailResult.Value, favoriteCourse);

            _schoolContext.SaveChanges();

            return(Ok());
        }
        //
        public async Task <IActionResult> EditPersonalInfo()
        {
            ProcessSystemInfo();
            var user = await _userManager.GetUserAsync(User);

            var employee = _context.Employees.First(a => a.Id == user.Id);
            var info     = new EditPersonalInfo();

            info.Name             = employee.Name;
            info.ContactEmail     = employee.ContactEmail;
            info.PhoneNumber      = employee.PhoneNumber;
            info.ShortDescription = employee.ShortDescription;
            if (employee.SchoolId != null)
            {
                var school = _context.Schools.First(a => a.Id == employee.SchoolId);
                info.SchoolName = school.SchoolName;
                var schoolSuburb = _context.Suburbs.First(a => a.Id == school.SuburbId);
                info.CampusName     = schoolSuburb.Name;
                info.CampusPostCode = schoolSuburb.PostCode;
            }

            if (employee.SuburbId != null)
            {
                var suburb = _context.Suburbs.First(a => a.Id == employee.SuburbId);
                info.LivingDistrict = suburb.Name;
                info.PostCode       = suburb.PostCode;
            }

            return(View(info));
        }
        //TODO need to update school status
        public async Task <IActionResult> EditPersonalInfoAction(EditPersonalInfo personalInfo)
        {
            InitialSystemInfo();
            var user = await _userManager.GetUserAsync(User);

            if (!ModelState.IsValid)
            {
                ProcessModelState();
                return(RedirectToAction("EditPersonalInfo"));
            }

            Suburb suburb = null;
            School school = null;

            try
            {
                suburb = _context.Suburbs.First(a => a.Name == personalInfo.LivingDistrict && a.PostCode == personalInfo.PostCode);
            }
            catch (InvalidOperationException)
            {
                if ((string)TempData["Error"] != "")
                {
                    TempData["Error"] += "\n";
                }

                TempData["Error"] += "Please enter correct living area information!";
            }
            try
            {
                var suburbId = _context.Suburbs.First(a =>
                                                      a.Name == personalInfo.CampusName.ToUpper() && a.PostCode == personalInfo.PostCode).Id;
                school = _context.Schools.First(a => a.SchoolName == personalInfo.SchoolName && a.SuburbId == suburbId);
            }
            catch (InvalidOperationException)
            {
                if ((string)TempData["Error"] != "")
                {
                    TempData["Error"] += "\n";
                }

                TempData["Error"] += "Please enter correct school information!";
            }

            var employee = _context.Employees.First(a => a.Id == user.Id);

            if (employee.ContactEmail != personalInfo.ContactEmail)
            {
                try
                {
                    employee.ContactEmail = personalInfo.ContactEmail;
                    _context.Employees.Update(employee);
                    _context.SaveChanges();
                    if ((string)TempData["Success"] != "")
                    {
                        TempData["Success"] += "\n";
                    }
                    TempData["Success"] += "Your email successfully updated!";
                }
                catch (InvalidOperationException)
                {
                    if ((string)TempData["Error"] != "")
                    {
                        TempData["Error"] += "\n";
                    }
                    TempData["Error"] += "Sorry, fail to update your contact email";
                }
            }
            if (employee.Name != personalInfo.Name)
            {
                try
                {
                    employee.Name = personalInfo.Name;
                    _context.Employees.Update(employee);
                    _context.SaveChanges();
                    if ((string)TempData["Success"] != "")
                    {
                        TempData["Success"] += "\n";
                    }
                    TempData["Success"] += "Your name successfully updated!";
                }
                catch (InvalidOperationException)
                {
                    if ((string)TempData["Error"] != "")
                    {
                        TempData["Error"] += "\n";
                    }
                    TempData["Error"] += "Sorry, fail to update your name";
                }
            }
            if (employee.PhoneNumber != personalInfo.PhoneNumber)
            {
                try
                {
                    employee.PhoneNumber = personalInfo.PhoneNumber;
                    _context.Employees.Update(employee);
                    _context.SaveChanges();
                    if ((string)TempData["Success"] != "")
                    {
                        TempData["Success"] += "\n";
                    }
                    TempData["Success"] += "Your phone number successfully updated!";
                }
                catch (InvalidOperationException)
                {
                    if ((string)TempData["Error"] != "")
                    {
                        TempData["Error"] += "\n";
                    }
                    TempData["Error"] += "Sorry, fail to update your phone number";
                }
            }
            if (employee.ShortDescription != personalInfo.ShortDescription)
            {
                try
                {
                    employee.ShortDescription = personalInfo.ShortDescription;
                    _context.Employees.Update(employee);
                    _context.SaveChanges();
                    if ((string)TempData["Success"] != "")
                    {
                        TempData["Success"] += "\n";
                    }
                    TempData["Success"] += "Your description successfully updated!";
                }
                catch (InvalidOperationException)
                {
                    if ((string)TempData["Error"] != "")
                    {
                        TempData["Error"] += "\n";
                    }
                    TempData["Error"] += "Sorry, fail to update your description";
                }
            }

            if (suburb != null)
            {
                try
                {
                    employee.SuburbId = suburb.Id;
                    _context.Employees.Update(employee);
                    _context.SaveChanges();
                    if ((string)TempData["Success"] != "")
                    {
                        TempData["Success"] += "\n";
                    }
                    TempData["Success"] += "Your suburb successfully updated!";
                }
                catch (InvalidOperationException)
                {
                    if ((string)TempData["Error"] != "")
                    {
                        TempData["Error"] += "\n";
                    }
                    TempData["Error"] += "Sorry, fail to update your suburb";
                }
            }
            if (school != null)
            {
                try
                {
                    employee.SchoolId = school.Id;
                    _context.Employees.Update(employee);
                    _context.SaveChanges();
                    if ((string)TempData["Success"] != "")
                    {
                        TempData["Success"] += "\n";
                    }
                    TempData["Success"] += "Your school successfully updated!";
                }
                catch (InvalidOperationException)
                {
                    if ((string)TempData["Error"] != "")
                    {
                        TempData["Error"] += "\n";
                    }
                    TempData["Error"] += "Sorry, fail to update your school";
                }
            }

            return(RedirectToAction("EditPersonalInfo"));
        }