示例#1
0
 public async Task <int> Insert(Attribute Attribute)
 {
     _attributeRepository.Create(Attribute);
     return(await Context.SaveChangesAsync());
 }
示例#2
0
 public async Task <int> Update(Attribute Attribute)
 {
     return(await Context.SaveChangesAsync());
 }
示例#3
0
        public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId, string creatorFullName, string creatorAvatar,
                                                                  AttributeMeta attributeMeta)
        {
            var productAttributeId = Guid.NewGuid().ToString();
            var productAttribute   = new Attribute
            {
                Id = productAttributeId,
                ConcurrencyStamp = productAttributeId,
                IsActive         = attributeMeta.IsActive,
                IsRequire        = attributeMeta.IsRequire,
                IsMultiple       = attributeMeta.IsMultiple,
                IsSelfContent    = attributeMeta.IsSelfContent,
                TenantId         = tenantId,
                CreatorId        = creatorId,
                CreatorFullName  = creatorFullName,
                CreatorAvatar    = creatorAvatar
            };

            var result = await _attributeRepository.Insert(productAttribute);

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


            #region insert Translation.
            if (attributeMeta.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 successful."),
                                                     string.Empty, productAttributeId));

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

                foreach (var productAttributeTranslation in attributeMeta.Translations)
                {
                    //  Check name exists.
                    var isNameExists = await _attributeTranslationRepository.CheckExists(productAttributeId, tenantId,
                                                                                         productAttributeTranslation.LanguageId, productAttributeTranslation.Name);

                    if (isNameExists)
                    {
                        await RollbackInsert();

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

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

                    productAttributeTranslations.Add(productAttributeTranslationInsert);
                }

                var resultTranslation = await _attributeTranslationRepository.Inserts(productAttributeTranslations);

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

                await RollbackInsertTranslation();
                await RollbackInsert();

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

            async Task RollbackInsert()
            {
                await _attributeRepository.ForceDelete(productAttributeId);
            }

            async Task RollbackInsertTranslation()
            {
                await _attributeTranslationRepository.ForceDelete(productAttributeId);
            }

            #endregion Local functions
        }