Пример #1
0
        public async Task <ActionResultResponse <string> > Update(string tenantId, string productAttributeId, string lastUpdateUserId,
                                                                  string lastUpdateFullName, string lastUpdateAvatar, string productAttributeValueId, AttributeValueMeta attributeValueMeta)
        {
            var infoProductAttribute = await _attributeRepository.GetInfo(productAttributeId);

            if (infoProductAttribute == null)
            {
                return(new ActionResultResponse <string>(-1, _warehouseResourceService.GetString("Product attribute does not exists.")));
            }
            // IsSelfContent = false : join voi bang value
            // IsSelfContent = true : tu nhap noi dung
            if (infoProductAttribute.IsSelfContent)
            {
                return(new ActionResultResponse <string>(-2, _warehouseResourceService.GetString("Product attribute is not self content.")));
            }

            var info = await _attributeValueRepository.GetInfo(productAttributeValueId);

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

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

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

            info.IsActive           = attributeValueMeta.IsActive;
            info.ConcurrencyStamp   = Guid.NewGuid().ToString();
            info.LastUpdateTime     = DateTime.Now;
            info.LastUpdateUserId   = lastUpdateUserId;
            info.LastUpdateFullName = lastUpdateFullName;

            await _attributeValueRepository.Update(info);

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

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

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

            async Task <ActionResultResponse <string> > UpdateTranslation()
            {
                foreach (var productAttributeValueTranslation in attributeValueMeta.Translations)
                {
                    var isNameExists = await _attributeValueTranslationRepository.CheckExists(info.Id, tenantId, productAttributeId,
                                                                                              productAttributeValueTranslation.LanguageId, productAttributeValueTranslation.Name);

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

                    var productAttributeValueTranslationInfo =
                        await _attributeValueTranslationRepository.GetInfo(info.Id, productAttributeValueTranslation.LanguageId);

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

                        await _attributeValueTranslationRepository.Update(productAttributeValueTranslationInfo);
                    }
                    else
                    {
                        var productAttributeValueTranslationInsert = new AttributeValueTranslation
                        {
                            AttributeValueId   = productAttributeValueId,
                            TenantId           = tenantId,
                            ProductAttributeId = productAttributeId,
                            LanguageId         = productAttributeValueTranslation.LanguageId.Trim(),
                            Name        = productAttributeValueTranslation.Name.Trim(),
                            Description = productAttributeValueTranslation.Description?.Trim(),
                            UnsignName  = productAttributeValueTranslation.Name.StripVietnameseChars().ToUpper()
                        };

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