示例#1
0
        private static async Task CreateEmployerJobProfile(int id, string name, DateTime create, DateTime update, int field, string title, string description, bool requireExperience, int max, int min, float salary, ApplicationDbContext _context)
        {
            var user          = _context.Users.First(a => a.Email == name);
            var newJobProfile = new EmployerJobProfile
            {
                Id = id, EmployerId = user.Id, CreateDateTime = create, LastUpdateDateTime = update, FieldId = field, Title = title, Description = description, RequireJobExperience = requireExperience, MaxDayForAWeek = max, MinDayForAWeek = min, Salary = salary
            };
            var check = _context.EmployerJobProfiles.Where(a => a.Id == id);

            if (check.Any())
            {
                return;
            }

            try
            {
                await _context.EmployerJobProfiles.AddAsync(newJobProfile);

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                // ignored
            }
        }
        //如果有邀请则不可更改
        //检查是不是本employer的JobProfile
        //
        public async Task <IActionResult> EditJobProfileAction(
            JobProfile jobProfile,
            IEnumerable <JobProfileSkill> jobProfileSkill,
            IEnumerable <JobProfileComplusoryWorkDay> jobProfileComplusoryWorkDay,
            IEnumerable <JobProfileRequiredLocation> jobProfileRequiredLocation,
            IEnumerable <JobProfileRequiredSchool> jobProfileRequiredSchool
            )
        {
            var user = await _userManager.GetUserAsync(User);

            var field = new Field {
            };

            //0. Validate input data
            //0.1
            //Validate previous profile
            EmployerJobProfile preProfile = null;

            if (jobProfile.ProfileId != 0)
            {
                try
                {
                    preProfile = _context
                                 .EmployerJobProfiles
                                 .First(a => a.Id == jobProfile.ProfileId);
                }
                catch (System.InvalidOperationException ex) {
                    //No Such profile
                    return(RedirectToAction("EditJobProfile"));
                }
                //Try to get others profile
                if (user.Id != preProfile.EmployerId)
                {
                    return(RedirectToAction("EditJobProfile"));
                }
            }
            //0.2
            //If does not have skills
            var newSkills = new List <EmployerSkill>();

            foreach (var skill in jobProfileSkill)
            {
                try
                {
                    var newSkillId = _context.Skills.First(a => a.NormalizedName == skill.SkillName.ToUpper()).Id;
                    newSkills.Add(new EmployerSkill {
                        SkillId = newSkillId, Required = skill.SkillRequired
                    });
                }
                catch (InvalidOperationException ex)
                {
                    continue;
                }
            }
            if (!newSkills.Any())
            {
                return(RedirectToAction("EditJobProfile"));
            }
            //0.3
            //Validate field
            try
            {
                field = _context.Fields.First(a => a.Name == jobProfile.FieldName);
            }
            catch (InvalidOperationException ex)
            {
                return(RedirectToAction("EditJobProfile"));
            }
            //0.4
            //Validate Title Validate Description
            //@TODO

            //1. Insert Or Update new profile
            //1.1
            //Set up newer version profile
            var profile = new EmployerJobProfile
            {
                EmployerId = user.Id,
                //需要检查之前
                CreateDateTime     = DateTime.Now,
                LastUpdateDateTime = DateTime.Now,
                FieldId            = field.Id,
                Title                = jobProfile.Title,
                Description          = jobProfile.Description,
                RequireJobExperience = jobProfile.RequireJobExperience,
                MaxDayForAWeek       = jobProfile.MaxDay,
                MinDayForAWeek       = jobProfile.MinDay,
                Salary               = jobProfile.Salary
            };

            //1.2.1
            //If does not have pre profile
            if (preProfile == null)
            {
                preProfile = profile;
                _context.EmployerJobProfiles.Add(preProfile);
            }
            //1.2.2.
            //If has pre profile
            else
            {
                preProfile.LastUpdateDateTime = DateTime.Now;
                preProfile.FieldId            = field.Id;
                preProfile.Title                = jobProfile.Title;
                preProfile.Description          = jobProfile.Description;
                preProfile.RequireJobExperience = jobProfile.RequireJobExperience;
                preProfile.MaxDayForAWeek       = jobProfile.MaxDay;
                preProfile.MinDayForAWeek       = jobProfile.MinDay;
                preProfile.Salary               = jobProfile.Salary;
                _context.EmployerJobProfiles.Update(preProfile);
            }
            _context.SaveChanges();

            //2. Update Skills
            //2.1
            //Try to find & remove previous Skills
            try
            {
                var preSkills = _context.EmployerSkills.Where(a => a.EmployerJobProfileId == jobProfile.ProfileId).ToList();
                _context.EmployerSkills.RemoveRange(preSkills);
                _context.SaveChanges();
            }
            catch (System.InvalidOperationException ex)
            {
            }
            //2.2
            //Insert new skills
            foreach (var skill in newSkills)
            {
                skill.EmployerJobProfileId = preProfile.Id;
            }
            _context.EmployerSkills.AddRange(newSkills);
            _context.SaveChanges();

            //3. Update Compulsory work Day
            //3.1
            //Try to find & remove previous Compulsory work Day
            try
            {
                var preDays = _context.EmployerComplusoryWorkDays.Where(a => a.EmployerJobProfileId == jobProfile.ProfileId).ToList();
                _context.EmployerComplusoryWorkDays.RemoveRange(preDays);
                _context.SaveChanges();
            }
            catch (System.InvalidOperationException ex)
            {
            }
            //3.2
            //Add new Compulsory work Day
            var newDays = new List <EmployerComplusoryWorkDay>();

            foreach (var day in jobProfileComplusoryWorkDay)
            {
                newDays.Add(new EmployerComplusoryWorkDay {
                    EmployerJobProfileId = preProfile.Id, Day = day.Day
                });
            }
            if (newDays.Any())
            {
                _context.EmployerComplusoryWorkDays.AddRange(newDays);
                _context.SaveChanges();
            }

            //4. Update Location
            //4.1
            //Try to find & remove previous Location
            try
            {
                var preLocation = _context.EmployerWorkLocations.Where(a => a.EmployerJobProfileId == jobProfile.ProfileId).ToList();
                _context.EmployerWorkLocations.RemoveRange(preLocation);
                _context.SaveChanges();
            }
            catch (System.InvalidOperationException ex)
            {
            }
            //4.2
            //Add new Location
            var newLocations = new List <EmployerRequiredWorkLocation>();

            foreach (var location in jobProfileRequiredLocation)
            {
                try
                {
                    var suburbId = _context.Suburbs.First(
                        a => a.Name == location.LocationName &&
                        a.PostCode == location.PostCode).Id;
                    newLocations.Add(new EmployerRequiredWorkLocation {
                        EmployerJobProfileId = preProfile.Id, SuburbId = suburbId
                    });
                }
                catch (InvalidOperationException ex)
                {
                }
            }
            if (newLocations.Any())
            {
                _context.EmployerWorkLocations.AddRange(newLocations);
                _context.SaveChanges();
            }

            //5. Update School
            //5.1
            //Try to find & remove previous School
            try
            {
                var preSchools = _context.EmployerRequiredSchools.Where(a => a.EmployerJobProfileId == jobProfile.ProfileId).ToList();
                _context.EmployerRequiredSchools.RemoveRange(preSchools);
                _context.SaveChanges();
            }
            catch (System.InvalidOperationException ex)
            {
            }
            //5.2
            //Add new School
            var newSchools = new List <EmployerRequiredSchool>();

            foreach (var school in jobProfileRequiredSchool)
            {
                try
                {
                    var schoolId = _context.Schools.First(
                        a => a.SchoolName == school.SchoolName &&
                        a.Suburb.Name == school.Campus).Id;
                    newSchools.Add(new EmployerRequiredSchool {
                        EmployerJobProfileId = preProfile.Id, SchoolId = schoolId
                    });
                }
                catch (InvalidOperationException ex)
                {
                }
            }
            if (newLocations.Any())
            {
                _context.EmployerRequiredSchools.AddRange(newSchools);
                _context.SaveChanges();
            }


            return(RedirectToAction("EditJobProfile"));
        }