create aspnet api with postgresql
This version added a compose application using: * ASP.Net API Core * EntityFramework Core * Postgresql Signed-off-by: Eduardo Silva <eduardo.lour.silva@gmail.com>
This commit is contained in:
committed by
Eduardo Silva
parent
bc95525543
commit
4eef6ec88c
@@ -0,0 +1,36 @@
|
||||
using aspnet_api_postgresql.Infrastructure.Database;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace aspnet_api_postgresql.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class HealthCheckController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<HealthCheckController> _logger;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
public HealthCheckController(ApplicationDbContext dbContext, ILogger<HealthCheckController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public string Ping()
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Database.OpenConnection();
|
||||
_dbContext.Database.ExecuteSqlRaw("SELECT 1");
|
||||
_dbContext.Database.CloseConnection();
|
||||
return "OK";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error connecting to database");
|
||||
return "Error connecting to database";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user