Exemple #1
0
        private static void SeedCategoryImage(CatalogContext context, Guid categoryId, Guid mediaId)
        {
            if (!context.CategoryImages.Any(x => x.CategoryId == categoryId))
            {
                var categoryImage = new CategoryImage
                {
                    CategoryId = categoryId,
                    MediaId    = mediaId,
                    Order      = 0
                };

                context.CategoryImages.Add(categoryImage.FillCommonProperties());

                context.SaveChanges();
            }
        }
        public async Task <CategoryServiceModel> CreateAsync(CreateCategoryServiceModel model)
        {
            var category = new Category
            {
                Parentid = model.ParentId,
                IsLeaf   = true
            };

            var parentCategory = await this.context.Categories.FirstOrDefaultAsync(x => x.Id == model.ParentId && x.IsActive);

            category.Level = parentCategory.Level + 1;

            this.context.Categories.Add(category.FillCommonProperties());

            var categoryTranslation = new CategoryTranslation
            {
                CategoryId = category.Id,
                Name       = model.Name,
                Language   = model.Language
            };

            this.context.CategoryTranslations.Add(categoryTranslation.FillCommonProperties());

            foreach (var file in model.Files.OrEmptyIfNull())
            {
                var categoryImage = new CategoryImage
                {
                    CategoryId = category.Id,
                    MediaId    = file,
                };

                this.context.CategoryImages.Add(categoryImage.FillCommonProperties());
            }

            await this.context.SaveChangesAsync();

            return(await this.GetAsync(new GetCategoryServiceModel { Id = category.Id, Language = model.Language, OrganisationId = model.OrganisationId, Username = model.Username }));
        }
        public async Task <CategoryServiceModel> UpdateAsync(UpdateCategoryServiceModel model)
        {
            var category = await this.context.Categories.FirstOrDefaultAsync(x => x.Id == model.Id && x.IsActive);

            if (category == null)
            {
                throw new CustomException(this.productLocalizer.GetString("CategoryNotFound"), (int)HttpStatusCode.NotFound);
            }

            var parentCategory = await this.context.Categories.FirstOrDefaultAsync(x => x.Id == model.ParentId && x.IsActive);

            if (parentCategory == null)
            {
                throw new CustomException(this.productLocalizer.GetString("ParentCategoryNotFound"), (int)HttpStatusCode.NotFound);
            }

            category.Parentid = model.ParentId;
            category.Level    = parentCategory.Level + 1;
            category.IsLeaf   = !await this.context.Categories.AnyAsync(x => x.Parentid == category.Id && x.IsActive);

            category.LastModifiedDate = DateTime.UtcNow;

            var categoryTranslation = await this.context.CategoryTranslations.FirstOrDefaultAsync(x => x.CategoryId == model.Id && x.Language == model.Language && x.IsActive);

            if (categoryTranslation != null)
            {
                categoryTranslation.Name = model.Name;
            }
            else
            {
                var newCategoryTranslation = new CategoryTranslation
                {
                    CategoryId = category.Id,
                    Name       = model.Name
                };

                this.context.CategoryTranslations.Add(newCategoryTranslation.FillCommonProperties());
            }

            categoryTranslation.LastModifiedDate = DateTime.UtcNow;

            var categoryImages = this.context.CategoryImages.Where(x => x.CategoryId == model.Id);

            foreach (var categoryImage in categoryImages)
            {
                this.context.CategoryImages.Remove(categoryImage);
            }

            if (model.Files != null)
            {
                foreach (var file in model.Files)
                {
                    var categoryImage = new CategoryImage
                    {
                        CategoryId = model.Id.Value,
                        MediaId    = file
                    };

                    this.context.CategoryImages.Add(categoryImage.FillCommonProperties());
                }
            }

            await this.context.SaveChangesAsync();

            return(await this.GetAsync(new GetCategoryServiceModel { Id = category.Id, Language = model.Language, OrganisationId = model.OrganisationId, Username = model.Username }));
        }