Esempio n. 1
0
        public IResult UpdateCategory(CategoryForUpdateDto categoryForUpdateDto)
        {
            var categoryToBeUpdated = _mapper.Map <CategoryTranslation>(categoryForUpdateDto);

            _categoryTranslationRepository.Update(categoryToBeUpdated);
            return(new SuccessResult(string.Format(Messages.SuccessfulUpdate, nameof(Category))));
        }
Esempio n. 2
0
        public async Task <ActionResultResponse> Update(string tenantId, int id, CategoryMeta categoryMeta)
        {
            var info = await _categoryRepository.GetInfo(tenantId, id);

            if (info == null)
            {
                return(new ActionResultResponse(-1, _resourceService.GetString("Category does not exists.")));
            }

            if (categoryMeta.ConcurrencyStamp != info.ConcurrencyStamp)
            {
                return(new ActionResultResponse(-2,
                                                _resourceService.GetString("This category already updated by another. You can not update this category.")));
            }

            var oldIdPath = info.IdPath;

            info.IsActive         = categoryMeta.IsActive;
            info.Order            = categoryMeta.Order;
            info.OrderPath        = categoryMeta.Order.ToString();
            info.ConcurrencyStamp = Guid.NewGuid().ToString();
            info.LastUpdate       = DateTime.Now;
            info.BannerImage      = categoryMeta.BannerImage;
            info.IsHomePage       = categoryMeta.IsHomePage;
            if (info.ParentId != categoryMeta.ParentId)
            {
                if (!categoryMeta.ParentId.HasValue)
                {
                    info.ParentId = null;
                    info.IdPath   = info.Id.ToString();

                    var order = await _categoryRepository.CountByParentId(null);

                    info.Order     = order;
                    info.OrderPath = order.ToString();
                }
                else
                {
                    if (categoryMeta.ParentId == info.Id)
                    {
                        return(new ActionResultResponse(-3, _resourceService.GetString("Parent category can not be it selft.")));
                    }


                    var parentInfo = await _categoryRepository.GetInfo(tenantId, categoryMeta.ParentId.Value);

                    if (parentInfo == null)
                    {
                        return(new ActionResultResponse(-3, _resourceService.GetString("Parent category does not exists.")));
                    }

                    info.ParentId = parentInfo.Id;
                    info.IdPath   = $"{parentInfo.IdPath}.{info.Id}";

                    var order = await _categoryRepository.CountByParentId(parentInfo.Id);

                    info.Order     = order;
                    info.OrderPath = $"{parentInfo.OrderPath}.{order}";
                }
            }

            await _categoryRepository.Update(info);

            await UpdateChildrenIdPath();

            var resultUpdateTranslation = await SaveCategoryTranslation();

            if (resultUpdateTranslation.Code <= 0)
            {
                return(resultUpdateTranslation);
            }

            return(new ActionResultResponse(1, _resourceService.GetString("Update categogry successful.")));

            #region Local functions
            async Task <ActionResultResponse> SaveCategoryTranslation()
            {
                var listNewCategoryTranslations = new List <CategoryTranslation>();

                foreach (var categoryTranslation in categoryMeta.CategoryTranslations)
                {
                    // Check name exists.
                    var isNameExists = await _categoryTranslationRepository.CheckExists(tenantId,
                                                                                        categoryTranslation.LanguageId, info.Id, categoryTranslation.Name);

                    if (isNameExists)
                    {
                        return(new ActionResultResponse <CategoryTranslation>(-4,
                                                                              _resourceService.GetString("Category name: \"{0}\" already exsits.", categoryTranslation.SeoLink)));
                    }

                    categoryTranslation.SeoLink = !string.IsNullOrEmpty(categoryTranslation.SeoLink)
                        ? categoryTranslation.SeoLink.Trim()
                        : categoryTranslation.Name.Trim().ToUrlString().ToLower();

                    // Check seoLink exists.
                    var isSeoLinkExists = await _categoryTranslationRepository.CheckSeoLinkExists(tenantId,
                                                                                                  categoryTranslation.LanguageId, info.Id, categoryTranslation.SeoLink);

                    if (isSeoLinkExists)
                    {
                        return(new ActionResultResponse <CategoryTranslation>(-5,
                                                                              _resourceService.GetString("Seolink: \"{0}\" already exsits.", categoryTranslation.SeoLink)));
                    }

                    var categoryTranslationInfo =
                        await _categoryTranslationRepository.GetInfo(info.Id, categoryTranslation.LanguageId);

                    // Add new category translation.
                    if (categoryTranslationInfo == null)
                    {
                        listNewCategoryTranslations.Add(new CategoryTranslation
                        {
                            CategoryId      = info.Id,
                            LanguageId      = categoryTranslation.LanguageId,
                            Name            = categoryTranslation.Name.Trim(),
                            MetaTitle       = categoryTranslation.MetaTitle?.Trim(),
                            Description     = categoryTranslation.Description?.Trim(),
                            MetaDescription = categoryTranslation.MetaDescription?.Trim(),
                            UnsignName      = categoryTranslation.Name.Trim().StripVietnameseChars().ToUpper(),
                            SeoLink         = categoryTranslation.SeoLink
                        });
                    }
                    else
                    {
                        categoryTranslationInfo.Name            = categoryTranslation.Name.Trim();
                        categoryTranslationInfo.MetaTitle       = categoryTranslation.MetaTitle?.Trim();
                        categoryTranslationInfo.Description     = categoryTranslation.Description?.Trim();
                        categoryTranslationInfo.MetaDescription = categoryTranslation.MetaDescription?.Trim();
                        categoryTranslationInfo.UnsignName      = categoryTranslation.Name.Trim().StripVietnameseChars().ToUpper();
                        categoryTranslationInfo.SeoLink         = categoryTranslation.SeoLink;
                        await _categoryTranslationRepository.Update(categoryTranslationInfo);
                    }
                }

                return(new ActionResultResponse(1));
            }

            async Task UpdateChildrenIdPath()
            {
                if (info.IdPath != oldIdPath)
                {
                    await _categoryRepository.UpdateChildrenIdPath(oldIdPath, info.IdPath);
                }
            }

            #endregion
        }