private static void SeedCategory(CatalogContext context, Guid id, int level, Guid?parentId, int order, bool isLeaf, string englishName, string polishName, string germanName) { if (!context.Categories.Any(x => x.Id == id)) { var category = new Category { Id = id, Level = level, Parentid = parentId, Order = order, IsLeaf = isLeaf }; var enCategoryTranslation = new CategoryTranslation { CategoryId = category.Id, Language = "en", Name = englishName }; var plCategoryTranslation = new CategoryTranslation { CategoryId = category.Id, Language = "pl", Name = polishName }; var deCategoryTranslation = new CategoryTranslation { CategoryId = category.Id, Language = "de", Name = germanName }; context.Categories.Add(category.FillCommonProperties()); context.CategoryTranslations.Add(enCategoryTranslation.FillCommonProperties()); context.CategoryTranslations.Add(plCategoryTranslation.FillCommonProperties()); context.CategoryTranslations.Add(deCategoryTranslation.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 })); }