Exemple #1
0
        public async Task <ActionResultResponse <string> > Insert(string tenantId, string productAttributeId,
                                                                  string creatorId, string creatorFullName, string creatorAvatar, 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 productAttributeValueId = Guid.NewGuid().ToString();
            var productAttributeValue   = new AttributeValue
            {
                Id               = productAttributeValueId,
                AttributeId      = productAttributeId,
                ConcurrencyStamp = productAttributeValueId,
                IsActive         = attributeValueMeta.IsActive,
                TenantId         = tenantId,
                CreatorId        = creatorId,
                CreatorFullName  = creatorFullName
            };

            var result = await _attributeValueRepository.Insert(productAttributeValue);

            if (result <= 0)
            {
                return(new ActionResultResponse <string>(result,
                                                         _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong)));
            }


            #region insert Translation.
            if (attributeValueMeta.Translations.Count > 0)
            {
                var resultInsertTranslation = await InsertTranslation();

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

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

            #region Local functions
            async Task <ActionResultResponse <string> > InsertTranslation()
            {
                var productAttributeValueTranslations = new List <AttributeValueTranslation>();

                foreach (var productAttributeValueTranslation in attributeValueMeta.Translations)
                {
                    //  Check name exists.
                    var isNameExists = await _attributeValueTranslationRepository.CheckExists(productAttributeValueId, tenantId, productAttributeId,
                                                                                              productAttributeValueTranslation.LanguageId, productAttributeValueTranslation.Name);

                    if (isNameExists)
                    {
                        await RollbackInsert();

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

                    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()
                    };

                    productAttributeValueTranslations.Add(productAttributeValueTranslationInsert);
                }

                var resultTranslation = await _attributeValueTranslationRepository.Inserts(productAttributeValueTranslations);

                if (resultTranslation > 0)
                {
                    return(new ActionResultResponse <string>(resultTranslation,
                                                             _warehouseResourceService.GetString("Add new product attribute value translation successful.")));
                }

                await RollbackInsertTranslation();
                await RollbackInsert();

                return(new ActionResultResponse <string>(-4,
                                                         _warehouseResourceService.GetString("Can not insert product attribute value translation. Please contact with administrator.")));
            }

            async Task RollbackInsert()
            {
                await _attributeValueRepository.ForceDelete(productAttributeValueId);
            }

            async Task RollbackInsertTranslation()
            {
                await _attributeValueTranslationRepository.ForceDelete(productAttributeValueId);
            }

            #endregion Local functions
        }