Create database and users table

This commit is contained in:
Reimar 2024-08-12 10:58:27 +02:00
parent 037ca9d824
commit bbf442e00c
7 changed files with 43 additions and 2 deletions

1
API/.gitignore vendored Normal file
View File

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

View File

@ -11,6 +11,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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.EntityFrameworkCore.Sqlite" Version="9.0.0-preview.6.24327.4" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />

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

@ -4,11 +4,17 @@ namespace API.Controllers
{ {
[ApiController] [ApiController]
[Route("[controller]")] [Route("[controller]")]
public class HealthCheckController : ControllerBase public class HealthCheckController(AppDBContext context) : ControllerBase
{ {
[HttpGet(Name = "HealthCheck")] [HttpGet(Name = "HealthCheck")]
public void Get() public StatusCodeResult Get()
{ {
if (!context.Database.CanConnect())
{
return new StatusCodeResult(500);
}
return Ok();
} }
} }
} }

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 namespace API
{ {
public class Program public class Program
@ -23,6 +25,11 @@ namespace API
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); 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(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

View File

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