Skip to content

Commit 823590a

Browse files
Merge pull request #41 from myvas/update-received-view
Show short version
2 parents 7acc383 + 43e83de commit 823590a

11 files changed

+353
-185
lines changed

.github/workflows/deploy.yml

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
- name: Save the SSH private key to a file
5151
run: |
5252
if [ ! -f ~/.ssh/id_rsa ]; then
53+
echo "Save the SSH private key to a file"
5354
mkdir -p ~/.ssh
5455
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
5556
chmod 600 ~/.ssh/id_rsa
@@ -58,6 +59,7 @@ jobs:
5859
- name: Add remote server to known_hosts
5960
run: |
6061
if ! grep -q "${{ env.SSH_HOST }}" ~/.ssh/known_hosts; then
62+
echo "Add remote server to known_hosts"
6163
ssh-keyscan ${{ env.SSH_HOST }} >> ~/.ssh/known_hosts
6264
fi
6365

src/MvcDemo/Data/DemoDbInitializer.cs

-59
This file was deleted.

src/MvcDemo/Data/DemoDesignTimeDbContextFactory.cs

-16
This file was deleted.

src/MvcDemo/Data/HostDatabaseExtensions.cs

-52
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Identity;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace Demo.Data;
10+
11+
public static class WebApplicationDatabaseExtensions
12+
{
13+
public static WebApplication MigrateDatabase(this WebApplication app)
14+
{
15+
using (var scope = app.Services.CreateScope())
16+
{
17+
var services = scope.ServiceProvider;
18+
19+
try
20+
{
21+
var db = services.GetRequiredService<DemoDbContext>();
22+
db.Database.Migrate();
23+
}
24+
catch (Exception ex)
25+
{
26+
var logger = services.GetRequiredService<ILogger<Program>>();
27+
logger.LogError(ex, "An error occurred while migrating the database.");
28+
}
29+
}
30+
31+
return app;
32+
}
33+
34+
35+
public static WebApplication SeedDatabase(this WebApplication app, string adminUserName, string adminInitPassword)
36+
{
37+
using (var scope = app.Services.CreateScope())
38+
{
39+
var services = scope.ServiceProvider;
40+
41+
try
42+
{
43+
Task.Run(async () =>
44+
{
45+
var userManager = services.GetRequiredService<UserManager<IdentityUser>>();
46+
await EnsureAdminUser(userManager, adminUserName, adminInitPassword);
47+
});
48+
}
49+
catch (Exception ex)
50+
{
51+
var logger = services.GetRequiredService<ILogger<Program>>();
52+
logger.LogError(ex, "An error occurred while seeding the database.");
53+
}
54+
}
55+
56+
return app;
57+
}
58+
59+
private static async Task EnsureAdminUser(UserManager<IdentityUser> userManager, string adminUserName, string adminEmail)
60+
{
61+
var adminInitPassword = adminEmail;
62+
63+
var user = await userManager.FindByNameAsync(adminUserName);
64+
if (user == null)
65+
{
66+
user = new IdentityUser()
67+
{
68+
UserName = adminUserName,
69+
Email = adminEmail,
70+
EmailConfirmed = true,
71+
};
72+
var result = await userManager.CreateAsync(user, adminInitPassword);
73+
if (!result.Succeeded)
74+
{
75+
throw new Exception(GetErrorMessage(result));
76+
}
77+
}
78+
}
79+
80+
private static string GetErrorMessage(IdentityResult identityResult)
81+
{
82+
var result = "";
83+
84+
foreach (var error in identityResult.Errors)
85+
{
86+
result += $"[{error.Code}]{error.Description}" + Environment.NewLine;
87+
}
88+
return result;
89+
}
90+
}

src/MvcDemo/Demo.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<PackageReference Include="Myvas.AspNetCore.Authentication.WeixinOpen" Version="9.0.*" />
2828
<PackageReference Include="Myvas.AspNetCore.TencentSms" Version="9.0.*" />
2929
<PackageReference Include="Myvas.AspNetCore.ViewDivert" Version="9.0.*" />
30-
<PackageReference Include="Myvas.AspNetCore.Weixin" Version="9.0.0-rc.5" />
30+
<PackageReference Include="Myvas.AspNetCore.Weixin" Version="9.0.0-rc.6" />
3131
<PackageReference Include="Serilog.AspNetCore" Version="9.0.*" />
3232
</ItemGroup>
3333

src/MvcDemo/HostExtensions.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Demo;
1212

1313
public static class HostExtensions
1414
{
15-
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
15+
public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder)
1616
{
1717
var Configuration = builder.Configuration;
1818

@@ -91,6 +91,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
9191
{
9292
o.Configuration = Configuration.GetConnectionString("RedisConnection");
9393
})
94+
.AddJsapiTicketRedisCacheProvider()
9495
.AddWeixinSite(o =>
9596
{
9697
o.Debug = true; // for this demo for debugging
@@ -105,7 +106,7 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
105106
})
106107
.AddWeixinEventSink<DemoEventSink>();
107108

108-
return builder.Build();
109+
return builder;
109110
}
110111

111112
public static WebApplication ConfigurePipeline(this WebApplication app)

src/MvcDemo/Migrations/20250323190719_V2.Designer.cs renamed to src/MvcDemo/Migrations/20250325070817_V3.Designer.cs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)