Example #1
0
        public GreenTemplateSaveResult SaveGreenTemplate(GreenTemplateViewModel aUpdatedModel)
        {
            GreenTemplateSaveResult lSaveResult = new GreenTemplateSaveResult();

            DB.Template lUnderlyingTemplate = tdb.Templates.Single(t => t.Id == aUpdatedModel.TemplateId);

            ValidationRunner <GreenTemplateViewModel> lRunner
                = new ValidationRunner <GreenTemplateViewModel>(
                      new AllShallConstraintsIncludedRule(lUnderlyingTemplate),
                      new LeafLevelGreenConstraintsHaveDataType());

            ValidationResult lResult = lRunner.RunValidation(aUpdatedModel);

            if (!lResult.Pass)
            {
                StringBuilder lMessageBuilder = new StringBuilder();

                foreach (RuleValidationResult lRuleResult in lResult.Results)
                {
                    if (lRuleResult.Pass)
                    {
                        continue;
                    }

                    lMessageBuilder.AppendLine(string.Format("Error: {0}", lRuleResult.Message));
                }

                lSaveResult.ViewModel         = aUpdatedModel;
                lSaveResult.FailedValidation  = true;
                lSaveResult.ValidationMessage = lMessageBuilder.ToString();

                return(lSaveResult);
            }

            lSaveResult.FailedValidation = false;
            TemplateConstraintMapper lMapper = new TemplateConstraintMapper(tdb);

            lMapper.MapViewModelToEntity(aUpdatedModel);

            tdb.SaveChanges();

            DB.Template            lUpdatedTemplate = tdb.Templates.Single(t => t.Id == aUpdatedModel.TemplateId);
            GreenTemplateViewModel lRefreshedModel  = lMapper.MapEntityToViewModel(lUpdatedTemplate);

            lSaveResult.ViewModel = lRefreshedModel;

            return(lSaveResult);
        }
Example #2
0
        public GreenTemplateViewModel GetGreenTemplate(int?templateId = null)
        {
            DB.Template lTemplate = tdb.Templates.DefaultIfEmpty(null).SingleOrDefault(t => t.Id == templateId);

            if (lTemplate == null)
            {
                return(new GreenTemplateViewModel());
            }
            else
            {
                TemplateConstraintMapper lMapper = new TemplateConstraintMapper(tdb);
                GreenTemplateViewModel   lModel  = lMapper.MapEntityToViewModel(lTemplate);

                return(lModel);
            }
        }