From bbf442e00cf3f494b984ef48eecc47de92b2da71 Mon Sep 17 00:00:00 2001 From: Reimar Date: Mon, 12 Aug 2024 10:58:27 +0200 Subject: [PATCH] Create database and users table --- API/.gitignore | 1 + API/API.csproj | 4 ++++ API/AppDBContext.cs | 9 +++++++++ API/Controllers/HealthCheckController.cs | 10 ++++++++-- API/Models/User.cs | 11 +++++++++++ API/Program.cs | 7 +++++++ API/appsettings.Development.json | 3 +++ 7 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 API/.gitignore create mode 100644 API/AppDBContext.cs create mode 100644 API/Models/User.cs diff --git a/API/.gitignore b/API/.gitignore new file mode 100644 index 0000000..b9f9133 --- /dev/null +++ b/API/.gitignore @@ -0,0 +1 @@ +database.sqlite3* \ No newline at end of file diff --git a/API/API.csproj b/API/API.csproj index 7e107e7..271a4f5 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -11,6 +11,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/API/AppDBContext.cs b/API/AppDBContext.cs new file mode 100644 index 0000000..bdeb5cf --- /dev/null +++ b/API/AppDBContext.cs @@ -0,0 +1,9 @@ +using API.Models; +using Microsoft.EntityFrameworkCore; + +namespace API; + +public class AppDBContext(DbContextOptions options) : DbContext(options) +{ + public DbSet Users { get; set; } +} diff --git a/API/Controllers/HealthCheckController.cs b/API/Controllers/HealthCheckController.cs index 4d08a42..c2943d9 100644 --- a/API/Controllers/HealthCheckController.cs +++ b/API/Controllers/HealthCheckController.cs @@ -4,11 +4,17 @@ namespace API.Controllers { [ApiController] [Route("[controller]")] - public class HealthCheckController : ControllerBase + public class HealthCheckController(AppDBContext context) : ControllerBase { [HttpGet(Name = "HealthCheck")] - public void Get() + public StatusCodeResult Get() { + if (!context.Database.CanConnect()) + { + return new StatusCodeResult(500); + } + + return Ok(); } } } diff --git a/API/Models/User.cs b/API/Models/User.cs new file mode 100644 index 0000000..5b6180c --- /dev/null +++ b/API/Models/User.cs @@ -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; } +} \ No newline at end of file diff --git a/API/Program.cs b/API/Program.cs index 6ffa8ba..c75bd9c 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -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(options => options.UseSqlite(connectionString)); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/API/appsettings.Development.json b/API/appsettings.Development.json index 0c208ae..f274806 100644 --- a/API/appsettings.Development.json +++ b/API/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "DefaultConnection": "Data Source=database.sqlite3" } }