Exemplo n.º 1
0
        void IJobAdsCommand.OpenJobAd(JobAd jobAd)
        {
            if (jobAd.Status == JobAdStatus.Open)
            {
                return;
            }
            if (!CanBeOpened(jobAd))
            {
                throw new InvalidOperationException(string.Format("Cannot open the '{0}' job ad, status: {1}, expiry time: {2}.", jobAd.Id, jobAd.Status, jobAd.ExpiryTime));
            }

            var previousStatus = jobAd.Status;

            // If it requires refreshes set it up.

            if (jobAd.Features.IsFlagSet(JobAdFeatures.Refresh))
            {
                _repository.CreateRefresh(jobAd.Id, DateTime.Now);
            }

            // If the expiry time is not explicitly set then set it now, or if it is set for the past.

            var newExpiryTime = jobAd.ExpiryTime == null || jobAd.ExpiryTime.Value < DateTime.Now
                ? RoundExpiryTime(GetDefaultExpiryTime(jobAd.Features))
                : null;

            _repository.ChangeStatus(jobAd.Id, JobAdStatus.Open, newExpiryTime, DateTime.Now);

            jobAd.Status = JobAdStatus.Open;
            if (newExpiryTime != null)
            {
                jobAd.ExpiryTime = newExpiryTime.Value;
            }

            // Fire events.

            var handlers = JobAdOpened;

            if (handlers != null)
            {
                handlers(this, new JobAdOpenedEventArgs(jobAd.Id, previousStatus));
            }
        }