Ejemplo n.º 1
0
        public async Task <ActionResultResponse <string> > Insert(string tenantId, CustomerResourceMeta customerResourceMeta)
        {
            //if (!CustomerResourceMeta.CustomerResourceTranslations.Any())
            //    return new ActionResultResponse(-1, _sharedResourceService.GetString("Please enter at least one language."));

            var CustomerResourceTranslations = new List <CustomerResourceTranslation>();
            var CustomerResourceId           = Guid.NewGuid().ToString();

            // Insert new Customer source.
            var resultInsertCustomerResource = await _customerResourceRepository.Insert(new CustomerResource
            {
                Id = CustomerResourceId,
                ConcurrencyStamp = CustomerResourceId,
                IsActive         = customerResourceMeta.IsActive,
                CreateTime       = DateTime.Now,
                IsDelete         = false,
                TenantId         = tenantId,
                Order            = customerResourceMeta.Order
            });

            if (resultInsertCustomerResource <= 0)
            {
                return(new ActionResultResponse <string>(resultInsertCustomerResource, _sharedResourceService.GetString("Something went wrong. Please contact with administrator.")));
            }

            foreach (var customerResourceTranslation in customerResourceMeta.CustomerResourceTranslations)
            {
                // Check name exists.
                var isNameExists = await _customerResourceTranslationRepository.CheckExists(CustomerResourceId,
                                                                                            customerResourceTranslation.LanguageId, customerResourceTranslation.Name);

                if (isNameExists)
                {
                    await RollbackInsert();

                    return(new ActionResultResponse <string>(-2, _customerResourceService.GetString("Customer resource name: \"{0}\" already exists.", customerResourceTranslation.Name)));
                }

                var customerResourceTranslationInsert = new CustomerResourceTranslation()
                {
                    CustomerResourceId = CustomerResourceId,

                    LanguageId  = customerResourceTranslation.LanguageId.Trim(),
                    Name        = customerResourceTranslation.Name.Trim(),
                    Description = customerResourceTranslation.Description?.Trim(),
                    UnsignName  = customerResourceTranslation.Name.StripVietnameseChars().ToUpper()
                };

                CustomerResourceTranslations.Add(customerResourceTranslationInsert);
            }

            // Insert Customer source translations.
            var resultInsertTranslation = await _customerResourceTranslationRepository.Inserts(CustomerResourceTranslations);

            if (resultInsertTranslation > 0)
            {
                return(new ActionResultResponse <string>(resultInsertCustomerResource, _customerResourceService.GetString("Add new Customer resource successful."), string.Empty, CustomerResourceId));
            }

            await RollbackInsert();

            return(new ActionResultResponse <string>(-3, _customerResourceService.GetString("Can not insert new Customer resource. Please contact with administrator.")));

            async Task RollbackInsert()
            {
                await _customerResourceRepository.ForceDelete(CustomerResourceId);

                await _customerResourceTranslationRepository.ForceDeleteByCustomerResourceId(CustomerResourceId);
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResultResponse> Update(string tenantId, string id, CustomerResourceMeta CustomerResourceMeta)
        {
            //if (!CustomerResourceMeta.CustomerResourceTranslations.Any())
            //    return new ActionResultResponse(-1, _sharedResourceService.GetString("Please enter at least one language."));

            var info = await _customerResourceRepository.GetInfo(id);

            if (info == null)
            {
                return(new ActionResultResponse(-2, _customerResourceService.GetString("Customer resource does not exists.")));
            }

            if (info.TenantId != tenantId)
            {
                return(new ActionResultResponse(-3, _sharedResourceService.GetString("You do not have permission to to this action.")));
            }

            if (info.ConcurrencyStamp != CustomerResourceMeta.ConcurrencyStamp)
            {
                return(new ActionResultResponse(-4, _customerResourceService.GetString("The Customer resource already updated by other people. You can not update this Customer source.")));
            }

            info.Order            = CustomerResourceMeta.Order;
            info.IsActive         = CustomerResourceMeta.IsActive;
            info.ConcurrencyStamp = Guid.NewGuid().ToString();
            info.LastUpdate       = DateTime.Now;

            foreach (var customerResourceTranslation in CustomerResourceMeta.CustomerResourceTranslations)
            {
                var isNameExists = await _customerResourceTranslationRepository.CheckExists(info.Id,
                                                                                            customerResourceTranslation.LanguageId, customerResourceTranslation.Name);

                if (isNameExists)
                {
                    return(new ActionResultResponse(-5, _customerResourceService.GetString("Customer resource name: \"{0}\" already exists.", customerResourceTranslation.Name)));
                }


                var customerResourceTranslationInfo =
                    await _customerResourceTranslationRepository.GetInfo(info.Id, customerResourceTranslation.LanguageId);

                if (customerResourceTranslationInfo != null)
                {
                    customerResourceTranslationInfo.Name        = customerResourceTranslation.Name.Trim();
                    customerResourceTranslationInfo.Description = customerResourceTranslation.Description?.Trim();
                    customerResourceTranslationInfo.UnsignName  = customerResourceTranslation.Name.StripVietnameseChars().ToUpper();
                    await _customerResourceTranslationRepository.Update(customerResourceTranslationInfo);
                }
                else
                {
                    var customerResourceTranslationInsert = new CustomerResourceTranslation()
                    {
                        CustomerResourceId = id,
                        LanguageId         = customerResourceTranslation.LanguageId.Trim(),
                        Name        = customerResourceTranslation.Name.Trim(),
                        Description = customerResourceTranslation.Description?.Trim(),
                        UnsignName  = customerResourceTranslation.Name.StripVietnameseChars().ToUpper()
                    };
                    await _customerResourceTranslationRepository.Insert(customerResourceTranslationInsert);
                }
            }
            await _customerResourceRepository.Update(info);

            return(new ActionResultResponse(1, _customerResourceService.GetString("Update Customer resource successful.")));
        }