Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ app.db
app.db-shm
app.db-wal
.mono
logs/

# Node.js dependencies
node_modules/
Expand Down
7 changes: 1 addition & 6 deletions src/RazorPagesProject/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ public async virtual Task AddMessageAsync(Message message)

public async virtual Task DeleteAllMessagesAsync()
{
foreach (var message in Messages)
{
Messages.Remove(message);
}

await SaveChangesAsync();
await Messages.ExecuteDeleteAsync();
}

public async virtual Task DeleteMessageAsync(int id)
Expand Down
22 changes: 20 additions & 2 deletions src/RazorPagesProject/Pages/GitHubProfile.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

namespace RazorPagesProject.Pages;

public class GitHubProfileModel(IGitHubClient client, IStringLocalizer<GitHubProfileModel> localizer) : PageModel
public class GitHubProfileModel(IGitHubClient client, IStringLocalizer<GitHubProfileModel> localizer, ILogger<GitHubProfileModel> logger) : PageModel
{
private readonly IStringLocalizer<GitHubProfileModel> _localizer = localizer;
private readonly ILogger<GitHubProfileModel> _logger = logger;

public class InputModel
{
Expand All @@ -29,7 +30,24 @@ public async Task<IActionResult> OnGetAsync([FromRoute] string userName)
{
if (userName != null)
{
GitHubUser = await Client.GetUserAsync(userName);
try
{
GitHubUser = await Client.GetUserAsync(userName);
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to fetch GitHub profile for user '{UserName}'. Status: {StatusCode}",
userName, ex.StatusCode);
ModelState.AddModelError(string.Empty, _localizer["ErrorFetchingProfile"]);
return Page();
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected error occurred while fetching GitHub profile for user '{UserName}'",
userName);
ModelState.AddModelError(string.Empty, _localizer["UnexpectedError"]);
return Page();
}
}

return Page();
Expand Down
16 changes: 13 additions & 3 deletions src/RazorPagesProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
using RazorPagesProject.Data;
using RazorPagesProject.Services;
using System.Globalization;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File("logs/app-{Date}.txt", rollingInterval: RollingInterval.Day));


// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(connectionString));
Expand Down Expand Up @@ -44,7 +53,8 @@
{
client.BaseAddress = new Uri("https://api.github.com");
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Yolo", "0.1.0"));
});
})
.AddStandardResilienceHandler();

builder.Services.AddScoped<IQuoteService, QuoteService>();

Expand All @@ -60,11 +70,11 @@
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRequestLocalization();
Expand Down
6 changes: 3 additions & 3 deletions src/RazorPagesProject/RazorPagesProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
</ItemGroup>

<!-- The following target is a workaround for https://github.com/dotnet/aspnetcore/issues/59625 -->
<Target
Name="Workaround_ResolveBuildCompressedStaticWebAssetsConfiguration"
BeforeTargets="ResolveBuildCompressedStaticWebAssetsConfiguration">
<Target Name="Workaround_ResolveBuildCompressedStaticWebAssetsConfiguration" BeforeTargets="ResolveBuildCompressedStaticWebAssetsConfiguration">
<!-- Remove any duplicate items from StaticWebAssets -->

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/RazorPagesProject/Resources/Pages/GitHubProfile.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@
<data name="Language" xml:space="preserve">
<value>Language</value>
</data>
<data name="ErrorFetchingProfile" xml:space="preserve">
<value>Unable to fetch the GitHub profile. Please check the username and try again.</value>
</data>
<data name="UnexpectedError" xml:space="preserve">
<value>An unexpected error occurred. Please try again later.</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/RazorPagesProject/Resources/Pages/GitHubProfile.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@
<data name="Language" xml:space="preserve">
<value>言語</value>
</data>
<data name="ErrorFetchingProfile" xml:space="preserve">
<value>GitHubプロフィールを取得できませんでした。ユーザー名を確認して、もう一度お試しください。</value>
</data>
<data name="UnexpectedError" xml:space="preserve">
<value>予期しないエラーが発生しました。後でもう一度お試しください。</value>
</data>
</root>
Loading