Exemple #1
0
        public async Task <ActionResultResponse <string> > Update(string tenantId, string lastUpdateUserId, string lastUpdateFullName, string lastUpdateAvatar,
                                                                  string productAttributeId, AttributeMeta attributeMeta)
        {
            var info = await _attributeRepository.GetInfo(productAttributeId);

            if (info == null)
            {
                return(new ActionResultResponse <string>(-1, _warehouseResourceService.GetString("Product attribute does not exists.")));
            }

            if (info.TenantId != tenantId)
            {
                return(new ActionResultResponse <string>(-2,
                                                         _warehouseResourceService.GetString(ErrorMessage.NotHavePermission)));
            }

            if (info.ConcurrencyStamp != attributeMeta.ConcurrencyStamp)
            {
                return(new ActionResultResponse <string>(-3,
                                                         _warehouseResourceService.GetString(
                                                             "The product attribute already updated by other people. you are not allowed to edit the product attribute information.")));
            }

            // Todo Not allow update IsSelfContent if used by product.

            // Delete all attribute values when change IsSelfContent property.(IsSelfContent = true : delete )
            if (attributeMeta.IsSelfContent)
            {
                await _attributeValueTranslationRepository.DeleteByProductAttribute(productAttributeId);

                await _attributeValueRepository.DeleteByProductAttribute(productAttributeId);
            }

            info.IsActive           = attributeMeta.IsActive;
            info.IsRequire          = attributeMeta.IsRequire;
            info.IsMultiple         = attributeMeta.IsMultiple;
            info.IsSelfContent      = attributeMeta.IsSelfContent;
            info.ConcurrencyStamp   = Guid.NewGuid().ToString();
            info.LastUpdateTime     = DateTime.Now;
            info.LastUpdateUserId   = lastUpdateUserId;
            info.LastUpdateFullName = lastUpdateFullName;

            await _attributeRepository.Update(info);

            #region translation.
            if (attributeMeta.Translations.Count > 0)
            {
                var resultUpdateTranslation = await UpdateTranslation();

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

            return(new ActionResultResponse <string>(1, _warehouseResourceService.GetString("Update product attribute successful."),
                                                     string.Empty, info.ConcurrencyStamp));

            async Task <ActionResultResponse <string> > UpdateTranslation()
            {
                foreach (var productAttributeTranslation in attributeMeta.Translations)
                {
                    var isNameExists = await _attributeTranslationRepository.CheckExists(info.Id, tenantId,
                                                                                         productAttributeTranslation.LanguageId, productAttributeTranslation.Name);

                    if (isNameExists)
                    {
                        return(new ActionResultResponse <string>(-4, _warehouseResourceService.GetString("Product attribute name: \"{0}\" already exists.")));
                    }

                    var productAttributeTranslationInfo =
                        await _attributeTranslationRepository.GetInfo(info.Id, productAttributeTranslation.LanguageId);

                    if (productAttributeTranslationInfo != null)
                    {
                        productAttributeTranslationInfo.Name        = productAttributeTranslation.Name.Trim();
                        productAttributeTranslationInfo.Description = productAttributeTranslation.Description?.Trim();
                        productAttributeTranslationInfo.UnsignName  = productAttributeTranslation.Name?.StripVietnameseChars().ToUpper();

                        await _attributeTranslationRepository.Update(productAttributeTranslationInfo);
                    }
                    else
                    {
                        var productAttributeTranslationInsert = new AttributeTranslation
                        {
                            AttributeId = productAttributeId,
                            TenantId    = tenantId,
                            LanguageId  = productAttributeTranslation.LanguageId.Trim(),
                            Name        = productAttributeTranslation.Name.Trim(),
                            Description = productAttributeTranslation.Description?.Trim(),
                            UnsignName  = productAttributeTranslation.Name.StripVietnameseChars().ToUpper()
                        };

                        await _attributeTranslationRepository.Insert(productAttributeTranslationInsert);
                    }
                }
                return(new ActionResultResponse <string>(1,
                                                         _warehouseResourceService.GetString("Update product attribute translation successful."), string.Empty, info.ConcurrencyStamp));
            }
        }