public async Task DeleteCategories(List <Guid> categoriesToBeDeleted) { if (categoriesToBeDeleted.IsNotNullOrEmpty()) { _categoryRepository.RemoveAll(DomainObjectSpecifications.IdIn <Category>(categoriesToBeDeleted)); } }
public async Task <IEnumerable <CategoryDto> > GetArticleCategories() { var ids = Enumerable.Select(_dbContext.Set <Article>(), x => x.CategoryId).Distinct().ToList(); if (ids.Count == 0) { return(Array.Empty <CategoryDto>()); } var entities = _categoryRepository.FindAll(DomainObjectSpecifications.IdIn <Category>(ids)).ToList(); return(entities.To <List <CategoryDto> >()); }
public async Task CreateOrUpdateProduct(ProductDto dto) { var entity = dto.To <Product>(); _productRepository.AddOrUpdate(entity); var tagEntities = new List <Tag>(); if (dto.Tags.IsNotNullOrEmpty()) { var tags = dto.Tags.Split(new char[] { ',' }); foreach (var tag in tags) { var tagEntity = _tagRepository.Find(CmsSpecifications.TagsWithNameAndType(tag, TagType.Public)); if (tagEntity == null) { tagEntity = new Tag() { Id = Guid.NewGuid(), TagType = TagType.Public, Name = tag }; _tagRepository.Add(tagEntity); tagEntities.Add(tagEntity); } } } var associatedTags = _productTagRepository.FindAll(ErpSpecifications.TagsWithProductId(dto.Id)).ToList(); var tagsToBeDeleted = associatedTags.Where(x => tagEntities.Any(tag => tag.Id == x.TargetId) == false).Select(x => x.Id).ToList(); var tagsNeedToBeAdded = tagEntities.Where(x => associatedTags.Any(a => a.TargetId == x.Id) == false).ToList(); if (tagsToBeDeleted.Any()) { _productTagRepository.RemoveAll(DomainObjectSpecifications.IdIn <ProductTag>(tagsToBeDeleted)); } if (tagsNeedToBeAdded.Any()) { foreach (var tagId in tagsNeedToBeAdded) { var association = new ProductTag { Id = Guid.NewGuid(), SourceId = dto.Id, TargetId = tagId.Id }; _productTagRepository.Add(association); } } //Product Images var images = dto.Images?.Select(x => Guid.Parse(x.Id)).ToList() ?? new List <Guid>(); var associatedImages = _productImageRepository.FindAll(ErpSpecifications.ImageWithProductId(dto.Id)).ToList(); var imagesToBeDeleted = associatedImages.Where(x => images.Any(image => image == x.TargetId) == false).Select(x => x.Id).ToList(); var imagesToBeAdded = images.Where(x => associatedImages.Any(a => a.TargetId == x) == false).ToList(); if (imagesToBeDeleted.Any()) { _productImageRepository.RemoveAll(DomainObjectSpecifications.IdIn <ProductImage>(imagesToBeDeleted)); } if (imagesToBeAdded.Any()) { foreach (var item in imagesToBeAdded) { var image = dto.Images.First(x => x.Id == item.ToString()); var attachement = new Attachment { Id = item, Name = image.Name, RbsInfo = image.Url }; _attachmentRepository.AddOrUpdate(attachement); var association = new ProductImage { Id = Guid.NewGuid(), SourceId = dto.Id, TargetId = item }; _productImageRepository.Add(association); } } _repositoryContext.Commit(); }
public async Task CreateOrUpdateArticle(ArticleDto dto) { var entity = dto.To <Article>(); _articleRepository.AddOrUpdate(entity); var tagEntities = new List <Tag>(); if (dto.Tags.IsNotNullOrEmpty()) { var tags = dto.Tags.Split(new char[] { ',' }); foreach (var tag in tags) { var tagEntity = _tagRepository.Find(CmsSpecifications.TagsWithNameAndType(tag, TagType.Public)); if (tagEntity == null) { tagEntity = new Tag() { Id = Guid.NewGuid(), TagType = TagType.Public, Name = tag }; _tagRepository.Add(tagEntity); } tagEntities.Add(tagEntity); } } var associatedTags = _articleTagRepository.FindAll(CmsSpecifications.TagsWithArticleId(dto.Id)).Select(x => x.TargetId).ToList(); var tagsToBeDeleted = associatedTags.Where(x => tagEntities.Any(tag => tag.Id == x) == false); var tagsNeedToBeAdded = tagEntities.Where(x => associatedTags.Contains(x.Id) == false); if (tagsToBeDeleted.Any()) { _articleTagRepository.RemoveAll(DomainObjectSpecifications.IdIn <ArticleTag>(tagsToBeDeleted.ToList())); } if (tagsNeedToBeAdded.Any()) { foreach (var tagId in tagsNeedToBeAdded) { var association = new ArticleTag { Id = Guid.NewGuid() }; association.SourceId = dto.Id; association.TargetId = tagId.Id; _articleTagRepository.Add(association); } } if (dto.IsPublished) { _articleSEORepository.AddOrUpdate(new ArticleSEO { Id = dto.Id, Description = dto.Summary, Image = dto.Cover, Keywords = dto.Tags, Locale = dto.ContentLanguage, PageName = dto.PageName, Title = dto.Title, }); } else { if (_articleSEORepository.Exists(CmsSpecifications.ArticleSEOWithId(entity.Id))) { _articleSEORepository.Remove(new ArticleSEO { Id = entity.Id }); } } _articleRepository.Context.Commit(); }