public static Apprenticeship ToEntity(this AddEditApprenticeshipViewModel model, ProviderPortalEntities db)
        {
            var userContext = UserContext.GetUserContext();
            var userId      = Permission.GetCurrentUserId();

            if (!userContext.IsProvider())
            {
                return(null);
            }
            Apprenticeship apprenticeship = model.ApprenticeshipId != 0
                ? db.Apprenticeships.FirstOrDefault(
                x =>
                x.ApprenticeshipId == model.ApprenticeshipId &&
                x.ProviderId == userContext.ItemId.Value)
                : null;

            apprenticeship = apprenticeship ?? new Apprenticeship
            {
                CreatedByUserId    = userId,
                CreatedDateTimeUtc = DateTime.UtcNow,
                ProviderId         = userContext.ItemId.Value,
                RecordStatusId     = (int)Constants.RecordStatus.Pending
            };
            apprenticeship.ModifiedByUserId     = userId;
            apprenticeship.ModifiedDateTimeUtc  = DateTime.UtcNow;
            apprenticeship.AddedByApplicationId = (int)Constants.Application.Portal;
            apprenticeship.MarketingInformation = Markdown.Sanitize(model.MarketingInformation);
            apprenticeship.Url = UrlHelper.GetFullUrl(model.Url);
            apprenticeship.ContactTelephone = model.ContactTelephone;
            apprenticeship.ContactEmail     = model.ContactEmail;
            apprenticeship.ContactWebsite   = UrlHelper.GetFullUrl(model.ContactWebsite);

            Apprenticeship frameworkOrStandard = ApprenticeshipExtensions.DecodeSearchFrameworkOrStandard(model.FrameworkOrStandardId) ?? ApprenticeshipExtensions.DecodeSearchFrameworkOrStandardByName(model.FrameworkOrStandard);

            if (frameworkOrStandard != null)
            {
                apprenticeship.StandardCode  = frameworkOrStandard.StandardCode;
                apprenticeship.Version       = frameworkOrStandard.Version;
                apprenticeship.FrameworkCode = frameworkOrStandard.FrameworkCode;
                apprenticeship.ProgType      = frameworkOrStandard.ProgType;
                apprenticeship.PathwayCode   = frameworkOrStandard.PathwayCode;
            }
            return(apprenticeship);
        }
 public static bool Validate(this AddEditApprenticeshipViewModel model, ProviderPortalEntities db, ModelStateDictionary modelState)
 {
     if (modelState["FrameworkOrStandard"].Errors.Count == 0)
     {
         Apprenticeship frameworkOrStandard = ApprenticeshipExtensions.DecodeSearchFrameworkOrStandard(model.FrameworkOrStandardId) ?? ApprenticeshipExtensions.DecodeSearchFrameworkOrStandardByName(model.FrameworkOrStandard);
         if (frameworkOrStandard == null)
         {
             modelState.AddModelError("FrameworkOrStandard", AppGlobal.Language.GetText("Apprenticeship_Edit_FrameworkOrStandardRequired", "The Framework / Standard Name field is required."));
         }
         else
         {
             Int32          providerId = UserContext.GetUserContext().ItemId ?? 0;
             Apprenticeship app        = db.Apprenticeships.FirstOrDefault(x => x.ProviderId == providerId && x.StandardCode == frameworkOrStandard.StandardCode && x.Version == frameworkOrStandard.Version && x.FrameworkCode == frameworkOrStandard.FrameworkCode && x.ProgType == frameworkOrStandard.ProgType && x.PathwayCode == frameworkOrStandard.PathwayCode && (model.ApprenticeshipId == 0 || x.ApprenticeshipId != model.ApprenticeshipId));
             if (app != null)
             {
                 modelState.AddModelError("FrameworkOrStandard", AppGlobal.Language.GetText("Apprenticeship_Create_FrameworkOrStandardMustBeUnique", "The Framework / Standard Name supplied is already in use."));
             }
         }
     }
     return(modelState.IsValid);
 }