Compare commits

...

2 Commits

Author SHA1 Message Date
ed9c6632c9
Create database and users table 2024-08-12 10:58:27 +02:00
b37d6a70e8
Create healthcheck 2024-08-12 10:18:49 +02:00
9 changed files with 56 additions and 46 deletions

1
API/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
database.sqlite3*

View File

@ -11,6 +11,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0-preview.6.24327.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0-preview.6.24327.4" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

9
API/AppDBContext.cs Normal file
View File

@ -0,0 +1,9 @@
using API.Models;
using Microsoft.EntityFrameworkCore;
namespace API;
public class AppDBContext(DbContextOptions<AppDBContext> options) : DbContext(options)
{
public DbSet<User> Users { get; set; }
}

View File

@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class HealthCheckController(AppDBContext context) : ControllerBase
{
[HttpGet(Name = "HealthCheck")]
public StatusCodeResult Get()
{
if (!context.Database.CanConnect())
{
return new StatusCodeResult(500);
}
return Ok();
}
}
}

View File

@ -1,33 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

11
API/Models/User.cs Normal file
View File

@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace API.Models;
public class User
{
public int Id { get; set; }
public string? Email { get; set; }
public string? Username { get; set; }
public string? Password { get; set; }
}

View File

@ -1,3 +1,5 @@
using Microsoft.EntityFrameworkCore;
namespace API
{
public class Program
@ -23,6 +25,11 @@ namespace API
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
IConfiguration Configuration = builder.Configuration;
var connectionString = Configuration.GetConnectionString("DefaultConnection") ?? Environment.GetEnvironmentVariable("DefaultConnection");
builder.Services.AddDbContext<AppDBContext>(options => options.UseSqlite(connectionString));
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@ -1,13 +0,0 @@
namespace API
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

View File

@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=database.sqlite3"
}
}