Exemple #1
0
        private void UpdateJobAd(JobAd jobAd, JobAdModel jobAdModel)
        {
            // Validate it first after updating the content.

            jobAdModel.Summary = HtmlUtil.TextToHtml(jobAdModel.Summary);
            jobAdModel.Content = HtmlUtil.TextToHtml(jobAdModel.Content);

            IList <ValidationError> errors = new List <ValidationError>();

            try
            {
                jobAdModel.Validate();
            }
            catch (ValidationErrorsException ex)
            {
                // If there are errors then need to put things back the way they were.

                jobAdModel.Summary = HtmlUtil.HtmlToText(jobAdModel.Summary);
                jobAdModel.Content = HtmlUtil.HtmlToText(jobAdModel.Content);

                errors = ex.Errors.ToList();
            }

            // Need to explicitly check these.

            if (jobAdModel.ContactDetails == null || string.IsNullOrEmpty(jobAdModel.ContactDetails.EmailAddress))
            {
                errors.Add(new RequiredValidationError("email address"));
            }

            if (jobAdModel.ContactDetails != null && !string.IsNullOrEmpty(jobAdModel.ContactDetails.PhoneNumber))
            {
                IValidator validator = new PhoneNumberValidator();
                if (!validator.IsValid(jobAdModel.ContactDetails.PhoneNumber))
                {
                    errors = errors.Concat(validator.GetValidationErrors("PhoneNumber")).ToList();
                }
            }

            if (jobAdModel.ContactDetails != null && !string.IsNullOrEmpty(jobAdModel.ContactDetails.FaxNumber))
            {
                IValidator validator = new PhoneNumberValidator();
                if (!validator.IsValid(jobAdModel.ContactDetails.FaxNumber))
                {
                    errors = errors.Concat(validator.GetValidationErrors("FaxNumber")).ToList();
                }
            }

            if (jobAdModel.IndustryIds.IsNullOrEmpty())
            {
                errors.Add(new RequiredValidationError("industry"));
            }

            if (jobAdModel.JobTypes == JobTypes.None)
            {
                errors.Add(new RequiredValidationError("job type"));
            }

            if (jobAdModel.Location == null || string.IsNullOrEmpty(jobAdModel.Location.ToString()))
            {
                errors.Add(new RequiredValidationError("location"));
            }

            const JobAdFeaturePack featurePack = JobAdFeaturePack.BaseFeaturePack;
            var features          = _employerOrdersQuery.GetJobAdFeatures(featurePack);
            var defaultExpiryTime = _jobAdsCommand.GetDefaultExpiryTime(features);

            if (jobAdModel.ExpiryTime != null)
            {
                if (jobAdModel.ExpiryTime.Value > defaultExpiryTime)
                {
                    errors.Add(new JobAdExpiryValidationError("ExpiryTime", (defaultExpiryTime - DateTime.Now).Days + 1));
                }
            }

            if (errors.Count > 0)
            {
                throw new ValidationErrorsException(errors);
            }

            // Assign.

            jobAd.Title          = jobAdModel.Title;
            jobAd.ContactDetails = jobAdModel.ContactDetails;
            jobAd.Features       = features;
            jobAd.FeatureBoost   = _employerOrdersQuery.GetJobAdFeatureBoost(featurePack);

            // If the date has not been changed from the default then let it remain the default.

            jobAd.ExpiryTime = jobAd.ExpiryTime == null && jobAdModel.ExpiryTime != null && jobAdModel.ExpiryTime.Value.Date == defaultExpiryTime.Date
                ? null
                : jobAdModel.ExpiryTime;

            jobAd.Visibility.HideCompany        = jobAdModel.HideCompany;
            jobAd.Visibility.HideContactDetails = jobAdModel.HideContactDetails;

            jobAd.Integration.ExternalReferenceId = jobAdModel.ExternalReferenceId;

            jobAd.LogoId = jobAdModel.Logo == null ? null : jobAdModel.Logo.FileReferenceId;

            jobAd.Description.Content           = jobAdModel.Content;
            jobAd.Description.CompanyName       = jobAdModel.CompanyName;
            jobAd.Description.PositionTitle     = jobAdModel.PositionTitle;
            jobAd.Description.ResidencyRequired = jobAdModel.ResidencyRequired;
            jobAd.Description.JobTypes          = jobAdModel.JobTypes;
            jobAd.Description.Industries        = jobAdModel.IndustryIds == null
                ? new List <Industry>()
                : (from i in jobAdModel.IndustryIds select _industriesQuery.GetIndustry(i)).ToList();
            jobAd.Description.Summary      = jobAdModel.Summary;
            jobAd.Description.Salary       = jobAdModel.Salary;
            jobAd.Description.Package      = jobAdModel.Package;
            jobAd.Description.BulletPoints = jobAdModel.BulletPoints;
            jobAd.Description.Location     = jobAdModel.Location;
        }