Commit 21257fa6 authored by gdlcf88's avatar gdlcf88

Data seeder for default Store and ProductType, close #15, #16

parent 954f812e
using System.Threading.Tasks;
using Volo.Abp.Data;
namespace EasyAbp.EShop.Products.ProductTypes
{
public interface IProductTypeDataSeeder
{
Task SeedAsync(DataSeedContext context);
}
}
\ No newline at end of file
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;
namespace EasyAbp.EShop.Products.ProductTypes
{
public class ProductTypeDataSeeder : IProductTypeDataSeeder, ITransientDependency
{
private readonly IGuidGenerator _guidGenerator;
private readonly IProductTypeRepository _productTypeRepository;
public ProductTypeDataSeeder(
IGuidGenerator guidGenerator,
IProductTypeRepository productTypeRepository)
{
_guidGenerator = guidGenerator;
_productTypeRepository = productTypeRepository;
}
public async Task SeedAsync(DataSeedContext context)
{
if (await _productTypeRepository.GetCountAsync() == 0)
{
await _productTypeRepository.InsertAsync(new ProductType(_guidGenerator.Create(), "Default", "Default",
null, MultiTenancySides.Both), true);
}
}
}
}
\ No newline at end of file
using System.Threading.Tasks;
using EasyAbp.EShop.Products.ProductTypes;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Uow;
namespace EasyAbp.EShop.Products
{
public class ProductsDataSeedContributor : IDataSeedContributor, ITransientDependency
{
private readonly IProductTypeDataSeeder _productTypeDataSeeder;
public ProductsDataSeedContributor(IProductTypeDataSeeder productTypeDataSeeder)
{
_productTypeDataSeeder = productTypeDataSeeder;
}
[UnitOfWork(true)]
public async Task SeedAsync(DataSeedContext context)
{
await _productTypeDataSeeder.SeedAsync(context);
}
}
}
\ No newline at end of file
...@@ -9,6 +9,8 @@ namespace EasyAbp.EShop.Stores.Settings ...@@ -9,6 +9,8 @@ namespace EasyAbp.EShop.Stores.Settings
/* Define module settings here. /* Define module settings here.
* Use names from StoresSettings class. * Use names from StoresSettings class.
*/ */
context.Add(new SettingDefinition(StoresSettings.DefaultProductTypeDisplayName, "My store"));
} }
} }
} }
\ No newline at end of file
...@@ -7,5 +7,7 @@ ...@@ -7,5 +7,7 @@
/* Add constants for setting names. Example: /* Add constants for setting names. Example:
* public const string MySettingName = GroupName + ".MySettingName"; * public const string MySettingName = GroupName + ".MySettingName";
*/ */
public const string DefaultProductTypeDisplayName = GroupName + ".DefaultProductTypeDisplayName";
} }
} }
\ No newline at end of file
using System.Threading.Tasks;
using Volo.Abp.Data;
namespace EasyAbp.EShop.Stores.Stores
{
public interface IStoreDataSeeder
{
Task SeedAsync(DataSeedContext context);
}
}
\ No newline at end of file
...@@ -18,8 +18,10 @@ namespace EasyAbp.EShop.Stores.Stores ...@@ -18,8 +18,10 @@ namespace EasyAbp.EShop.Stores.Stores
public Store( public Store(
Guid id, Guid id,
Guid? tenantId,
[NotNull] string name) : base(id) [NotNull] string name) : base(id)
{ {
TenantId = tenantId;
Name = name; Name = name;
} }
} }
......
using System.Threading.Tasks;
using EasyAbp.EShop.Stores.Settings;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Settings;
namespace EasyAbp.EShop.Stores.Stores
{
public class StoreDataSeeder : IStoreDataSeeder, ITransientDependency
{
private readonly IGuidGenerator _guidGenerator;
private readonly ICurrentTenant _currentTenant;
private readonly ISettingProvider _settingProvider;
private readonly IStoreRepository _storeRepository;
public StoreDataSeeder(
IGuidGenerator guidGenerator,
ICurrentTenant currentTenant,
ISettingProvider settingProvider,
IStoreRepository storeRepository)
{
_guidGenerator = guidGenerator;
_currentTenant = currentTenant;
_settingProvider = settingProvider;
_storeRepository = storeRepository;
}
public async Task SeedAsync(DataSeedContext context)
{
using (_currentTenant.Change(context.TenantId))
{
if (await _storeRepository.GetCountAsync() == 0)
{
await _storeRepository.InsertAsync(
new Store(_guidGenerator.Create(), _currentTenant.Id,
await _settingProvider.GetOrNullAsync(StoresSettings.DefaultProductTypeDisplayName)), true);
}
}
}
}
}
\ No newline at end of file
using System.Threading.Tasks;
using EasyAbp.EShop.Stores.Stores;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Uow;
namespace EasyAbp.EShop.Stores
{
public class StoresDataSeedContributor : IDataSeedContributor, ITransientDependency
{
private readonly IStoreDataSeeder _storeDataSeeder;
public StoresDataSeedContributor(IStoreDataSeeder storeDataSeeder)
{
_storeDataSeeder = storeDataSeeder;
}
[UnitOfWork(true)]
public async Task SeedAsync(DataSeedContext context)
{
await _storeDataSeeder.SeedAsync(context);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment