Skip to content
Merged
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
16 changes: 10 additions & 6 deletions src/Notifications.API/Database/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ namespace Notifications.Database;
internal static class Repository
{
// Read
public static async Task<IResult> DbAvailable(AppDbContext db) =>
await db.Database.CanConnectAsync()
? TypedResults.Ok("OK")
: TypedResults.InternalServerError("Database not available.");

public static Task<List<Notification>> GetCurrentNotificationsAsync(AppDbContext db) =>
db.Notifications
public static async Task<List<Notification>> GetCurrentNotificationsAsync(AppDbContext db) =>
await db.Notifications
.Where(n => n.Active && n.DisplayStart < DateTime.Now && DateTime.Now < n.DisplayEnd)
.OrderBy(n => n.DisplayStart).ThenBy(n => n.DisplayEnd).ToListAsync();

public static Task<List<Notification>> GetFutureNotificationsAsync(AppDbContext db) =>
db.Notifications
public static async Task<List<Notification>> GetFutureNotificationsAsync(AppDbContext db) =>
await db.Notifications
.Where(n => n.Active && DateTime.Now < n.DisplayStart)
.OrderBy(n => n.DisplayStart).ThenBy(n => n.DisplayEnd).ToListAsync();

public static Task<List<Notification>> GetAllNotifications(AppDbContext db) =>
db.Notifications
public static async Task<List<Notification>> GetAllNotifications(AppDbContext db) =>
await db.Notifications
.OrderBy(n => n.DisplayStart).ThenBy(n => n.DisplayEnd).ToListAsync();

// Write
Expand Down
3 changes: 2 additions & 1 deletion src/Notifications.API/Platform/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ internal static class Endpoints
public static void MapEndpoints(this IEndpointRouteBuilder app)
{
// Status endpoints
app.MapGet("/health", () => Results.Ok("OK"));
app.MapGet("/", () => Results.Ok());
app.MapGet("/health", Repository.DbAvailable);
app.MapGet("/version", () => Results.Ok(new { AppSettings.Version }));

// Public endpoints
Expand Down
2 changes: 1 addition & 1 deletion src/Notifications.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
var app = builder.Build();
app.UseRaygun();
app.MapEndpoints();
await app.BuildDatabaseAsync();

await app.BuildDatabaseAsync();
await app.RunAsync();
4 changes: 4 additions & 0 deletions src/Notifications.API/REST-Samples/successes.http
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
@ApiKey = client-api-key-1
@ApiKey2 = client-api-key-2

### Connect to API
GET {{HostAddress}}
Accept: application/json

### Check health.
GET {{HostAddress}}/health
Accept: application/json
Expand Down
6 changes: 3 additions & 3 deletions tests/Notifications.API.Tests/AddNotificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public async Task Setup()
}

[TearDown]
public void TearDown()
public async Task TearDown()
{
_dbContext.Database.EnsureDeleted();
_dbContext.Dispose();
await _dbContext.Database.EnsureDeletedAsync();
await _dbContext.DisposeAsync();
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions tests/Notifications.API.Tests/DeactivateNotificationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public async Task Setup()
}

[TearDown]
public void TearDown()
public async Task TearDown()
{
_dbContext.Database.EnsureDeleted();
_dbContext.Dispose();
await _dbContext.Database.EnsureDeletedAsync();
await _dbContext.DisposeAsync();
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions tests/Notifications.API.Tests/GetNotificationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public async Task Setup()
}

[TearDown]
public void TearDown()
public async Task TearDown()
{
_dbContext.Database.EnsureDeleted();
_dbContext.Dispose();
await _dbContext.Database.EnsureDeletedAsync();
await _dbContext.DisposeAsync();
}

[Test]
Expand Down
39 changes: 39 additions & 0 deletions tests/Notifications.API.Tests/HealthCheckTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Notifications.Database;

namespace Notifications.API.Tests;

[TestFixture]
public class HealthCheckTests
{
private AppDbContext _dbContext;

[SetUp]
public async Task Setup()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(databaseName: "RepositoryTest")
.Options;

_dbContext = new AppDbContext(options);
await _dbContext.Database.EnsureCreatedAsync();
}

[TearDown]
public async Task TearDown()
{
await _dbContext.Database.EnsureDeletedAsync();
await _dbContext.DisposeAsync();
}

[Test]
public async Task DbAvailable_IfAvailable()
{
// Act
var results = await Repository.DbAvailable(_dbContext);

// Assert
results.Should().BeOfType<Ok<string>>();
}
}
Loading