public async Task <IManagerActionResultModel <Category> > UpdateProductCategoryTranslation(ITranslationCategoryModel model) { var result = new ManagerActionResultModel <Category>(); Category entity = await this.GetByIdAsync(model.Id); if (entity == null) { result.Succeeded = false; result.Errors.Add(new ErrorResultModel(string.Empty, "Category not found!")); } if (model.Language == this.appSettings.DefaultLanguage) { return(await UpdateProductCategoryAsync(model)); } else { CategoryTranslation translation = entity.Translations.Where(t => t.Language == model.Language).FirstOrDefault(); if (translation == null) { translation = new CategoryTranslation() { Category = entity, CategoryId = entity.Id, Language = model.Language }; entity.Translations.Add(translation); } translation.Name = model.Name; await this.categoryRepo.SaveAsync(); } return(result); }
private void Create(Domain.Assignment Assignment, String LangCode) { if (String.IsNullOrEmpty(LangCode)) { LangCode = TicketService.LangMultiCODE; } if (Assignment.AssignedCategory != null) { if (Assignment.AssignedCategory.Translations != null) { CategoryTranslation trans = Assignment.AssignedCategory.Translations.Where(t => t.LanguageCode == LangCode).FirstOrDefault(); if (trans == null) { trans = Assignment.AssignedCategory.Translations.Where(t => t.LanguageCode == TicketService.LangMultiCODE).FirstOrDefault(); } if (trans == null) { CurrentCategoryName = Assignment.AssignedCategory.Name; } else { CurrentCategoryName = trans.Name; } } } else { CurrentCategoryName = ""; } if (Assignment.Type == Enums.AssignmentType.Category) { CurrentUserName = ""; IsManager = false; } else { IsManager = (Assignment.Type == Enums.AssignmentType.Manager); if (Assignment.AssignedTo != null) { if (Assignment.AssignedTo.Person != null) { CurrentUserName = Assignment.AssignedTo.Person.SurnameAndName; } else { CurrentUserName = Assignment.AssignedTo.Sname + " " + Assignment.AssignedTo.Name; } } } if (Assignment.CreatedOn != null) { CreatedOn = (DateTime)Assignment.CreatedOn; } }
public async Task <IManagerActionResultModel <Category> > UpdateProductCategoryAsync(IUpdateCategoryModel model) { var result = new ManagerActionResultModel <Category>(); try { Category entity = await this.GetByIdAsync(model.Id); if (entity == null) { result.Succeeded = false; result.Errors.Add(new ErrorResultModel(string.Empty, "Category not found!")); } CategoryTranslation translation = entity.Translations.Where(t => t.Language == this.appSettings.DefaultLanguage).FirstOrDefault(); entity.Name = model.Name; translation.Name = model.Name; await this.categoryRepo.SaveAsync(); result.Succeeded = true; result.Model = entity; } catch (Exception ex) { result.Errors.Add(new ErrorResultModel(ex)); if (ex.InnerException != null) { result.Errors.Add(new ErrorResultModel(ex.InnerException)); } } return(result); }
private async Task <IDataResult <int> > CheckCategoryExist(ITranslationEntity enVariant) { CategoryTranslation category = await _categoryTranslationRepository.FirstOrDefaultAsync(p => p.IsMainTranslation && p.CategoryName == enVariant.Value); return(new DataResult <int>() { Success = category != null, Data = category?.CategoryId ?? 0 }); }
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 <ApiResult <bool> > Update(CategoryUpdateRequest request) { CategoryViewModel categoryVD = await GetById(request.Id, request.LanguageId); if (categoryVD == null) { return(new ApiErrorResult <bool>()); } //check categorytranslation CategoryTranslation categoryTranslation = _context.CategoryTranslations.Where(x => x.CategoryId == request.Id && x.LanguageId == request.LanguageId).FirstOrDefault(); if (categoryTranslation == null) { return(new ApiErrorResult <bool>()); } //update Category var category = new Category() { Id = categoryVD.Id, IsShowOnHome = categoryVD.IsShowOnHome, ParentId = categoryVD.ParentId, SortOrder = categoryVD.SortOrder, Status = categoryVD.Status }; //update CategoryTranslation categoryTranslation.Name = request.Name; categoryTranslation.SeoAlias = request.SeoAlias; categoryTranslation.SeoDescription = request.SeoDescription; categoryTranslation.SeoTitle = request.SeoTitle; _context.Categories.Update(category); _context.CategoryTranslations.Update(categoryTranslation); await _context.SaveChangesAsync(); return(new ApiSuccessResult <bool>()); }
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 JsonResult Save(CategoryViewModel model) { var currentUser = GetAuthenticatedUser(); try { if (model.translations.Count != Language.GetList().Count()) { return(Error("ترجمه های وارد شده صحیح نیست.")); } foreach (var item in model.translations) { if (string.IsNullOrEmpty(item.title)) { return(Error(string.Format("وارد کردن عنوان در زبان {0} اجباری است.", Language.GetList().Single(x => x.Id == item.languageId).PersianTitle))); } } if (model.id != null && model.id > 0) { if (model.id == model.parentId) { return(Error("دسته بندی نمی تواند با خودش رابطه داشته باشد.")); } var entity = _context.Category.Single(x => x.StatusId != CategoryStatus.Deleted.Id && x.CategoryTypeId == CategoryType.Product.Id && x.Id == model.id); if (Request.Files.Count > 0) { var file = Request.Files[0]; var contentTypes = new List <string>() { "image/x-png", "image/gif", "image/jpeg" }; //if (!contentTypes.Contains(file.ContentType)) //{ // return Error("پسوند فایل انتخاب شده معتبر نیست."); //} if (!string.IsNullOrEmpty(entity.FileId)) { //AsefianFileContextHelper.DeleteFilePermanently(entity.FileId, entity.FileName); } var fileEntity = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp(), "admin", "category"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else if (string.IsNullOrEmpty(model.fileId) && !string.IsNullOrEmpty(entity.FileId)) { if (!string.IsNullOrEmpty(entity.FileId)) { //AsefianFileContextHelper.DeleteFilePermanently(entity.FileId, entity.FileName); } } entity.ParentId = model.parentId; entity.Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(); entity.Order = model.order; entity.FileId = model.fileId; entity.FileName = model.fileName; entity.StatusId = model.statusId; entity.ModifyUserId = currentUser.id; entity.ModifyDate = GetDatetime(); entity.ModifyIp = GetCurrentIp(); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); } else { entityItem = new CategoryTranslation() { CategoryId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian() }; _context.CategoryTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success("اطلاعات دسته بندی با موفقیت ویرایش شد.")); } else { if (Request.Files.Count > 0) { var file = Request.Files[0]; var contentTypes = new List <string>() { "image/x-png", "image/gif", "image/jpeg" }; if (!contentTypes.Contains(file.ContentType)) { return(Error("پسوند فایل انتخاب شده معتبر نیست.")); } var fileEntity = AsefianFileContextHelper.Save(file, currentUser.id, GetCurrentIp(), "admin", "category"); model.fileId = fileEntity.id; model.fileName = fileEntity.fileName; } else { model.fileId = null; model.fileName = null; } var entity = new Category() { ParentId = model.parentId, Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), Order = model.order, FileId = model.fileId, FileName = model.fileName, StatusId = model.statusId, CategoryTypeId = CategoryType.Product.Id, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.Category.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Title = item.title.ToStandardPersian(); entityItem.Description = item.description?.ToStandardPersian(); } else { entityItem = new CategoryTranslation() { CategoryId = entity.Id, LanguageId = item.languageId, Title = item.title.ToStandardPersian(), Description = item.description?.ToStandardPersian() }; _context.CategoryTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success(string.Format("دسته بندی \"{0}\" با موفقیت ایجاد شد.", entity.Sku))); } } catch (Exception ex) { return(ServerError(ex)); } }
public async Task <RequestResultWithData <CategoryTranslation> > CreateTranslationAsync(CategoryTranslation categoryTranslation) { if (ModelState.IsValid) { try { var creationResult = await _repositoryTranslation.AddAsync(categoryTranslation); return(new RequestResultWithData <CategoryTranslation>(creationResult.Item1, creationResult.Item2)); } catch (Exception ex) { return(new RequestResultWithData <CategoryTranslation>(false, ex.ToString())); } } else { var errorList = (from item in ModelState where item.Value.Errors.Any() select item.Value.Errors[0].ErrorMessage).ToList(); return(new RequestResultWithData <CategoryTranslation>(false, JsonConvert.SerializeObject(errorList))); } }
public async Task <RequestResult> UpdateTranslationAsync([FromRoute] Guid categoryId, [FromRoute] Guid translationId, CategoryTranslation translation) { if (ModelState.IsValid) { var result = await _repositoryTranslation.UpdateAsync(translation); return(new RequestResult(result, !result ? "ERROR" : "")); } else { var errorList = (from item in ModelState where item.Value.Errors.Any() select item.Value.Errors[0].ErrorMessage).ToList(); return(new RequestResultWithData <Category>(false, JsonConvert.SerializeObject(errorList))); } }
public async Task <int> Update(CategoryTranslation categoryTranslation) { return(await Context.SaveChangesAsync()); }
public async Task <int> Insert(CategoryTranslation categoryTranslation) { _categoryTranslationRepository.Create(categoryTranslation); return(await Context.SaveChangesAsync()); }
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 })); }