Server now silently fails with no PostgreSQL
This commit is contained in:
@@ -18,28 +18,44 @@ public class NotesController : ControllerBase
|
||||
[HttpGet("{cardName}")]
|
||||
public async Task<ActionResult<CardNote>> GetNote(string cardName)
|
||||
{
|
||||
var note = await _context.CardNotes.FindAsync(cardName);
|
||||
if (note == null)
|
||||
try
|
||||
{
|
||||
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(note);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<CardNote>> UpdateNote(CardNote note)
|
||||
{
|
||||
var existing = await _context.CardNotes.FindAsync(note.CardName);
|
||||
if (existing == null)
|
||||
try
|
||||
{
|
||||
_context.CardNotes.Add(note);
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.Note = note.Note;
|
||||
}
|
||||
var existing = await _context.CardNotes.FindAsync(note.CardName);
|
||||
if (existing == null)
|
||||
{
|
||||
_context.CardNotes.Add(note);
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.Note = note.Note;
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[WARNING] Could not save note to database: {ex.Message}");
|
||||
}
|
||||
|
||||
return Ok(note);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,15 +10,30 @@ builder.Services.AddRazorPages();
|
||||
|
||||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
options.UseNpgsql(connectionString));
|
||||
{
|
||||
if (!string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
options.UseNpgsql(connectionString);
|
||||
}
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Apply migrations
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
db.Database.Migrate();
|
||||
try
|
||||
{
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user