Skip to content

Commit 4820041

Browse files
fixed path for audio and image
1 parent 5bdd823 commit 4820041

File tree

1 file changed

+72
-74
lines changed

1 file changed

+72
-74
lines changed

Streetcode/Streetcode.WebApi/Extensions/SeedingLocalExtension.cs

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -37,81 +37,50 @@ public static async Task SeedDataAsync(this WebApplication app)
3737
var blobOptions = app.Services.GetRequiredService<IOptions<BlobEnvironmentVariables>>();
3838
string blobPath = app.Configuration.GetValue<string>("Blob:BlobStorePath");
3939
var blobService = new BlobService(blobOptions);
40+
string initialDataImagePath = Path.GetFullPath(Path.Combine("..", "Streetcode.XIntegrationTest", "TestData", "InitialData", "images.json"));
41+
string initialDataAudioPath = Path.GetFullPath(Path.Combine("..", "Streetcode.XIntegrationTest", "TestData", "InitialData", "audios.json"));
4042

41-
// Uncomment this part for image and audio seeding
42-
// const string initialDataImagePath = "../Streetcode.XIntegrationTest/TestData/InitialData/images.json";
43-
// const string initialDataAudioPath = "../Streetcode.XIntegrationTest/TestData/InitialData/audios.json";
44-
// if (!dbContext.Images.Any())
45-
// {
46-
// string imageJson = File.ReadAllText(initialDataImagePath, Encoding.UTF8);
47-
// var imgfromJson = JsonConvert.DeserializeObject<List<Image>>(imageJson);
48-
//
49-
// if (imgfromJson != null)
50-
// {
51-
// foreach (var img in imgfromJson)
52-
// {
53-
// string filePath = Path.Combine(blobPath, img.BlobName!);
54-
// if (!File.Exists(filePath))
55-
// {
56-
// blobService.SaveFileInStorageBase64(img.Base64!, img.BlobName!.Split('.')[0], img.BlobName.Split('.')[1]);
57-
// }
58-
// }
59-
//
60-
// dbContext.Images.AddRange(imgfromJson);
61-
// }
62-
//
63-
// await dbContext.SaveChangesAsync();
64-
//
65-
// if (!dbContext.ImageDetailses.Any())
66-
// {
67-
// dbContext.ImageDetailses.AddRange(new[]
68-
// {
69-
// new ImageDetails
70-
// {
71-
// ImageId = 1,
72-
// Alt = "0"
73-
// },
74-
// new ImageDetails
75-
// {
76-
// ImageId = 2,
77-
// Alt = "1"
78-
// },
79-
// new ImageDetails
80-
// {
81-
// ImageId = 3,
82-
// Alt = "1"
83-
// },
84-
// new ImageDetails
85-
// {
86-
// ImageId = 4,
87-
// Alt = "0"
88-
// }
89-
// });
90-
// await dbContext.SaveChangesAsync();
91-
// }
92-
// }
93-
//
94-
// if (!dbContext.Audios.Any())
95-
// {
96-
// string audiosJson = await File.ReadAllTextAsync(initialDataAudioPath, Encoding.UTF8);
97-
// var audiosfromJson = JsonConvert.DeserializeObject<List<Audio>>(audiosJson);
98-
//
99-
// if (audiosfromJson != null)
100-
// {
101-
// foreach (var audio in audiosfromJson)
102-
// {
103-
// string filePath = Path.Combine(blobPath, audio.BlobName!);
104-
// if (!File.Exists(filePath))
105-
// {
106-
// blobService.SaveFileInStorageBase64(audio.Base64!, audio.BlobName!.Split('.')[0], audio.BlobName.Split('.')[1]);
107-
// }
108-
// }
109-
//
110-
// dbContext.Audios.AddRange(audiosfromJson);
111-
// }
112-
//
113-
// await dbContext.SaveChangesAsync();
114-
// }
43+
if (File.Exists(initialDataImagePath))
44+
{
45+
if (!dbContext.Images.Any())
46+
{
47+
string imageJson = File.ReadAllText(initialDataImagePath, Encoding.UTF8);
48+
var imgfromJson = JsonConvert.DeserializeObject<List<Image>>(imageJson);
49+
50+
if (imgfromJson != null)
51+
{
52+
foreach (var img in imgfromJson)
53+
{
54+
string filePath = Path.Combine(blobPath, img.BlobName!);
55+
if (!File.Exists(filePath))
56+
{
57+
blobService.SaveFileInStorageBase64(img.Base64!, img.BlobName!.Split('.')[0], img.BlobName.Split('.')[1]);
58+
}
59+
}
60+
61+
dbContext.Images.AddRange(imgfromJson);
62+
}
63+
64+
await dbContext.SaveChangesAsync();
65+
66+
if (!dbContext.ImageDetailses.Any())
67+
{
68+
dbContext.ImageDetailses.AddRange(new[]
69+
{
70+
new ImageDetails { ImageId = 1, Alt = "0" },
71+
new ImageDetails { ImageId = 2, Alt = "1" },
72+
new ImageDetails { ImageId = 3, Alt = "1" },
73+
new ImageDetails { ImageId = 4, Alt = "0" }
74+
});
75+
76+
await dbContext.SaveChangesAsync();
77+
}
78+
}
79+
}
80+
else
81+
{
82+
Console.WriteLine($"Could not find the file at path: {initialDataImagePath}");
83+
}
11584

11685
if (!dbContext.Expertises.Any())
11786
{
@@ -175,6 +144,35 @@ public static async Task SeedDataAsync(this WebApplication app)
175144
await dbContext.SaveChangesAsync();
176145
}
177146

147+
if (File.Exists(initialDataAudioPath))
148+
{
149+
if (!dbContext.Audios.Any())
150+
{
151+
string audiosJson = await File.ReadAllTextAsync(initialDataAudioPath, Encoding.UTF8);
152+
var audiosfromJson = JsonConvert.DeserializeObject<List<Audio>>(audiosJson);
153+
154+
if (audiosfromJson != null)
155+
{
156+
foreach (var audio in audiosfromJson)
157+
{
158+
string filePath = Path.Combine(blobPath, audio.BlobName!);
159+
if (!File.Exists(filePath))
160+
{
161+
blobService.SaveFileInStorageBase64(audio.Base64!, audio.BlobName!.Split('.')[0], audio.BlobName.Split('.')[1]);
162+
}
163+
}
164+
165+
dbContext.Audios.AddRange(audiosfromJson);
166+
}
167+
168+
await dbContext.SaveChangesAsync();
169+
}
170+
}
171+
else
172+
{
173+
Console.WriteLine($"Could not find the file at path: {initialDataAudioPath}");
174+
}
175+
178176
if (!dbContext.Responses.Any())
179177
{
180178
dbContext.Responses.AddRange(

0 commit comments

Comments
 (0)