Server now silently fails with no PostgreSQL
This commit is contained in:
@@ -18,28 +18,44 @@ public class NotesController : ControllerBase
|
|||||||
[HttpGet("{cardName}")]
|
[HttpGet("{cardName}")]
|
||||||
public async Task<ActionResult<CardNote>> GetNote(string cardName)
|
public async Task<ActionResult<CardNote>> GetNote(string cardName)
|
||||||
{
|
{
|
||||||
var note = await _context.CardNotes.FindAsync(cardName);
|
try
|
||||||
if (note == null)
|
|
||||||
{
|
{
|
||||||
|
var note = await _context.CardNotes.FindAsync(cardName);
|
||||||
|
if (note == null)
|
||||||
|
{
|
||||||
|
return Ok(new CardNote { CardName = cardName, Note = "" });
|
||||||
|
}
|
||||||
|
return Ok(note);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[WARNING] Could not retrieve note from database: {ex.Message}");
|
||||||
return Ok(new CardNote { CardName = cardName, Note = "" });
|
return Ok(new CardNote { CardName = cardName, Note = "" });
|
||||||
}
|
}
|
||||||
return Ok(note);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<CardNote>> UpdateNote(CardNote note)
|
public async Task<ActionResult<CardNote>> UpdateNote(CardNote note)
|
||||||
{
|
{
|
||||||
var existing = await _context.CardNotes.FindAsync(note.CardName);
|
try
|
||||||
if (existing == null)
|
|
||||||
{
|
{
|
||||||
_context.CardNotes.Add(note);
|
var existing = await _context.CardNotes.FindAsync(note.CardName);
|
||||||
|
if (existing == null)
|
||||||
|
{
|
||||||
|
_context.CardNotes.Add(note);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
existing.Note = note.Note;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
existing.Note = note.Note;
|
Console.WriteLine($"[WARNING] Could not save note to database: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
return Ok(note);
|
return Ok(note);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,30 @@ builder.Services.AddRazorPages();
|
|||||||
|
|
||||||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||||
options.UseNpgsql(connectionString));
|
{
|
||||||
|
if (!string.IsNullOrEmpty(connectionString))
|
||||||
|
{
|
||||||
|
options.UseNpgsql(connectionString);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Apply migrations
|
// Apply migrations
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
try
|
||||||
db.Database.Migrate();
|
{
|
||||||
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||||
|
if (!string.IsNullOrEmpty(connectionString))
|
||||||
|
{
|
||||||
|
db.Database.Migrate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[WARNING] Database migration failed: {ex.Message}. The application will continue without a database connection.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<OverrideHtmlAssetPlaceholders>false</OverrideHtmlAssetPlaceholders>
|
<OverrideHtmlAssetPlaceholders>false</OverrideHtmlAssetPlaceholders>
|
||||||
|
<StaticWebAssetsFingerprintingEnabled>false</StaticWebAssetsFingerprintingEnabled>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet"/>
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet"/>
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet"/>
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet"/>
|
||||||
<link href="/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
|
<link href="/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
|
||||||
<link href="/_content/Telerik.UI.for.Blazor/css/kendo-theme-default/all.css" rel="stylesheet"/>
|
<link href="/_content/Telerik.UI.for.Blazor/css/kendo-theme-bootstrap/all.css" rel="stylesheet"/>
|
||||||
<link href="/css/app.css" rel="stylesheet"/>
|
<link href="/css/app.css" rel="stylesheet"/>
|
||||||
<link href="/Web.styles.css" rel="stylesheet"/>
|
<link href="/Web.styles.css" rel="stylesheet"/>
|
||||||
<script src="/_content/Telerik.UI.for.Blazor/js/telerik-blazor.js" defer></script>
|
<script src="/_content/Telerik.UI.for.Blazor/js/telerik-blazor.js" defer></script>
|
||||||
|
|||||||
Reference in New Issue
Block a user