Ejemplo n.º 1
0
        private bool CanCreateLanguageVariation(Response viewModel, Concept newConcept)
        {
            var group = _conceptRepository.GetByGroupId(newConcept.GroupId);

            if (group == null || group.Count == 0)
            {
                viewModel.Errors.TryAddModelError("groupId", "GroupId did not have a valid value");
                return(false);
            }

            var metas = _metaRepository.GetByRangeOfIds(newConcept.MetaIds);
            var metasWithoutLanguage = metas
                                       .Where(x => !x.Category.TypeGroup.Name.ToLower().Equals("language"))
                                       .Select(x => x.LanguageVariation)
                                       .ToList();
            var newMetaLanguage = metas.FirstOrDefault(x => x.Category.TypeGroup.Name.ToLower().Equals("language"));

            // Validate metas
            foreach (var concept in group)
            {
                var groupMeta = _metaRepository.GetByRangeOfIds(concept.MetaIds);
                var metasForConceptWithoutLanguage = groupMeta
                                                     .Where(x => !x.Category.TypeGroup.Name.ToLower().Equals("language"))
                                                     .Select(x => x.LanguageVariation)
                                                     .ToList();
                var groupLanguage = groupMeta.FirstOrDefault(x => x.Category.TypeGroup.Name.ToLower().Equals("language"));

                if (groupLanguage != null && newMetaLanguage != null &&
                    groupLanguage.LanguageVariation.Equals(newMetaLanguage.LanguageVariation))
                {
                    viewModel.Errors.TryAddModelError("metaIds", "Cannot create a concept with the same language");
                    return(false);
                }

                var commonMetas = metasForConceptWithoutLanguage.Intersect(metasWithoutLanguage).ToList();
                if (metasForConceptWithoutLanguage.Count != metasWithoutLanguage.Count || commonMetas.Count != metasWithoutLanguage.Count)
                {
                    viewModel.Errors.TryAddModelError("metaIds", "Did not contain similar meta as rest of the concept group");
                    return(false);
                }
            }
            return(true);
        }