33using MicroCommerce . ApiService . Domain . Entities ;
44using MicroCommerce . ApiService . Features . DomainEvents ;
55using MicroCommerce . ApiService . Infrastructure ;
6+ using MicroCommerce . ApiService . Services ;
67using Microsoft . EntityFrameworkCore ;
78using Microsoft . EntityFrameworkCore . Infrastructure ;
89using Microsoft . EntityFrameworkCore . Storage ;
@@ -24,8 +25,10 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
2425 using var scope = serviceProvider . CreateScope ( ) ;
2526 var context = scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
2627 var publishEndpoint = scope . ServiceProvider . GetRequiredService < IPublishEndpoint > ( ) ;
28+ var fileService = scope . ServiceProvider . GetRequiredService < IFileService > ( ) ;
29+ var environment = scope . ServiceProvider . GetRequiredService < IHostEnvironment > ( ) ;
2730
28- await InitializeDatabaseAsync ( context , publishEndpoint , cancellationToken ) ;
31+ await InitializeDatabaseAsync ( context , publishEndpoint , fileService , environment , cancellationToken ) ;
2932 }
3033 catch ( Exception ex )
3134 {
@@ -34,20 +37,20 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
3437 }
3538 }
3639
37- public async Task InitializeDatabaseAsync ( ApplicationDbContext context , IPublishEndpoint publishEndpoint , CancellationToken cancellationToken = default )
40+ public async Task InitializeDatabaseAsync ( ApplicationDbContext context , IPublishEndpoint publishEndpoint , IFileService fileService , IHostEnvironment environment , CancellationToken cancellationToken = default )
3841 {
3942 var sw = Stopwatch . StartNew ( ) ;
4043
4144 var strategy = context . Database . CreateExecutionStrategy ( ) ;
4245 await strategy . ExecuteAsync ( context . Database . MigrateAsync , cancellationToken ) ;
4346
44- await SeedDataAsync ( context , cancellationToken ) ;
47+ await SeedDataAsync ( context , fileService , environment , cancellationToken ) ;
4548 await IndexData ( context , publishEndpoint , cancellationToken ) ;
4649
4750 logger . LogInformation ( "Database initialization completed after {ElapsedMilliseconds}ms" , sw . ElapsedMilliseconds ) ;
4851 }
4952
50- private static async Task SeedDataAsync ( ApplicationDbContext context , CancellationToken cancellationToken )
53+ private static async Task SeedDataAsync ( ApplicationDbContext context , IFileService fileService , IHostEnvironment environment , CancellationToken cancellationToken )
5154 {
5255 if ( ! context . Products . Any ( ) )
5356 {
@@ -69,7 +72,29 @@ static List<Product> GetPreconfiguredItems()
6972 ] ;
7073 }
7174
72- await context . Products . AddRangeAsync ( GetPreconfiguredItems ( ) , cancellationToken ) ;
75+ var products = GetPreconfiguredItems ( ) ;
76+
77+ var tasks = new List < Task > ( ) ;
78+ foreach ( var product in products )
79+ {
80+ var filePath = Path . Combine ( environment . ContentRootPath , "Resources/Images" , product . ImageUrl ) ;
81+ if ( ! File . Exists ( filePath ) )
82+ {
83+ continue ;
84+ }
85+
86+ var uploadTask = Task . Run ( async ( ) =>
87+ {
88+ await using var stream = File . OpenRead ( filePath ) ;
89+ await fileService . UploadFileAsync ( product . ImageUrl , stream , cancellationToken ) ;
90+ } , cancellationToken ) ;
91+
92+ tasks . Add ( uploadTask ) ;
93+ }
94+
95+ await Task . WhenAll ( tasks ) ;
96+
97+ await context . Products . AddRangeAsync ( products , cancellationToken ) ;
7398 }
7499
75100 await context . SaveChangesAsync ( cancellationToken ) ;
0 commit comments