Simple Database + Endpoints

This commit is contained in:
2025-12-07 12:07:10 +01:00
parent 35eee78efc
commit 3125d657dd
22 changed files with 595 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
@@ -7,11 +7,29 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="Models\Ingoing\Altersgruppen\AltersGruppeIngoing\**" />
<Compile Remove="Services\Interfaces\**" />
<Content Remove="Models\Ingoing\Altersgruppen\AltersGruppeIngoing\**" />
<Content Remove="Services\Interfaces\**" />
<EmbeddedResource Remove="Models\Ingoing\Altersgruppen\AltersGruppeIngoing\**" />
<EmbeddedResource Remove="Services\Interfaces\**" />
<None Remove="Models\Ingoing\Altersgruppen\AltersGruppeIngoing\**" />
<None Remove="Services\Interfaces\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="Services\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Services\Interfaces\**" />
<Content Remove="Services\Interfaces\**" />
<EmbeddedResource Remove="Services\Interfaces\**" />
<None Remove="Services\Interfaces\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\Ingoing\Altersgruppen\" />
<Folder Include="Services\" />
</ItemGroup>
</Project>

25
API/API.csproj.Backup.tmp Normal file
View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\Ingoing\" />
<Folder Include="Services\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,73 @@
using API.Models.Ingoing.Altersgruppen;
using API.Repository.AgeGroup;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers
{
[ApiController]
[Route("api/ageGroups/")]
public class AgeGroupController : ControllerBase
{
private IAgeGroupService _ageGroupService;
public AgeGroupController(IAgeGroupService ageGroupService)
{
_ageGroupService = ageGroupService;
}
[HttpGet()]
public async Task<IActionResult> GetAll()
{
var allAgeGroups = await _ageGroupService.GetAllAsync();
return Ok(allAgeGroups);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetOne([FromRoute] int id)
{
var group = await _ageGroupService.GetAsync(id);
if (group == null)
{
return NotFound();
}
return Ok(group);
}
[HttpPost()]
public async Task<IActionResult> Create([FromBody] AltersGruppeIngoing groupDto)
{
var group = await _ageGroupService.CreateAsync(groupDto.ToInternalFromIngoing());
return CreatedAtAction(nameof(GetOne), new { Id = group.Id }, group);
}
[HttpPut("{id}")]
public async Task<IActionResult> Update([FromRoute] int id, [FromBody] AltersGruppeIngoing groupDto)
{
var group = await _ageGroupService.UpdateAsync(id, groupDto.ToInternalFromIngoing());
if(group == null)
{
return NotFound();
}
return Ok(group);
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete([FromRoute] int Id)
{
var group = await _ageGroupService.DeleteAsync(Id);
if (group == null)
{
return NotFound();
}
return NoContent();
}
}
}

View File

@@ -0,0 +1,14 @@
using API.Models.Internal.Altersgruppen;
using Microsoft.EntityFrameworkCore;
namespace API.Database
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<AltersGruppe> Altersgruppen { get; set; }
}
}

View File

@@ -0,0 +1,47 @@
// <auto-generated />
using API.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace API.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20251206113128_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.11");
modelBuilder.Entity("API.Models.Internal.Altersgruppen.Altergruppe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("EndingAge")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("StartingAge")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Altersgruppen");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace API.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Altersgruppen",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
StartingAge = table.Column<string>(type: "TEXT", nullable: false),
EndingAge = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Altersgruppen", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Altersgruppen");
}
}
}

View File

@@ -0,0 +1,45 @@
// <auto-generated />
using API.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace API.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20251206220303_ChangeAgeFieldsToInt")]
partial class ChangeAgeFieldsToInt
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.11");
modelBuilder.Entity("API.Models.Internal.Altersgruppen.AltersGruppe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("EndingAge")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("StartingAge")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Altersgruppen");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,50 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace API.Migrations
{
/// <inheritdoc />
public partial class ChangeAgeFieldsToInt : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "StartingAge",
table: "Altersgruppen",
type: "INTEGER",
nullable: false,
oldClrType: typeof(string),
oldType: "TEXT");
migrationBuilder.AlterColumn<int>(
name: "EndingAge",
table: "Altersgruppen",
type: "INTEGER",
nullable: false,
oldClrType: typeof(string),
oldType: "TEXT");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "StartingAge",
table: "Altersgruppen",
type: "TEXT",
nullable: false,
oldClrType: typeof(int),
oldType: "INTEGER");
migrationBuilder.AlterColumn<string>(
name: "EndingAge",
table: "Altersgruppen",
type: "TEXT",
nullable: false,
oldClrType: typeof(int),
oldType: "INTEGER");
}
}
}

View File

@@ -0,0 +1,42 @@
// <auto-generated />
using API.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace API.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.11");
modelBuilder.Entity("API.Models.Internal.Altersgruppen.AltersGruppe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("EndingAge")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("StartingAge")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Altersgruppen");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,28 @@
using API.Models.Internal.Altersgruppen;
using System.ComponentModel.DataAnnotations;
namespace API.Models.Ingoing.Altersgruppen
{
public class AltersGruppeIngoing
{
[Required]
public string Name { get; set; }
[Required]
public int StartingAge { get; set; }
[Required]
public int EndingAge { get; set; }
}
public static class AltersgruppeMapper
{
public static AltersGruppe ToInternalFromIngoing(this AltersGruppeIngoing group)
{
return new AltersGruppe
{
Name = group.Name,
StartingAge = group.StartingAge,
EndingAge = group.EndingAge,
};
}
}
}

View File

@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;
namespace API.Models.Internal.Altersgruppen
{
public class AltersGruppe
{
public int Id { get; set; }
public string Name { get; set; }
public int StartingAge { get; set; }
public int EndingAge { get; set; }
}
}

View File

@@ -1,12 +1,29 @@
using API.Database;
using API.Repository.AgeGroup;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Database
var postgreConnection = builder.Configuration.GetConnectionString("PostgresConnection");
if (!string.IsNullOrEmpty(postgreConnection))
{
// Nutze PostgresSQL
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(postgreConnection));
}
else
{
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite("Data Source=app.db"));
}
builder.Services.AddScoped<IAgeGroupService, AgeGroupService>();
// Add Database Services
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -16,6 +33,12 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
using(var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
app.UseHttpsRedirection();
app.UseAuthorization();

View File

@@ -0,0 +1,68 @@
using API.Database;
using API.Models.Internal.Altersgruppen;
using Microsoft.EntityFrameworkCore;
namespace API.Repository.AgeGroup
{
public class AgeGroupService : IAgeGroupService
{
private ApplicationDbContext _context;
public AgeGroupService(ApplicationDbContext context)
{
_context = context;
}
public async Task<AltersGruppe> CreateAsync(AltersGruppe altersGruppe)
{
await _context.Altersgruppen.AddAsync(altersGruppe);
await _context.SaveChangesAsync();
return altersGruppe;
}
public async Task<AltersGruppe?> DeleteAsync(int id)
{
var group = await _context.Altersgruppen.FirstOrDefaultAsync(x => x.Id == id);
if (group == null)
{
return null;
}
_context.Altersgruppen.Remove(group);
_context.SaveChanges();
return group;
}
public async Task<List<AltersGruppe>> GetAllAsync()
{
var allGroups = await _context.Altersgruppen.ToListAsync();
return allGroups;
}
public async Task<AltersGruppe?> GetAsync(int id)
{
return await _context.Altersgruppen.FindAsync(id);
}
public async Task<AltersGruppe?> UpdateAsync(int id, AltersGruppe altersGruppe)
{
var existingGroup = await _context.Altersgruppen.FirstOrDefaultAsync(x => x.Id == id);
if (existingGroup == null)
{
return null;
}
existingGroup.Name = altersGruppe.Name;
existingGroup.StartingAge = altersGruppe.StartingAge;
existingGroup.EndingAge = altersGruppe.EndingAge;
await _context.SaveChangesAsync();
return existingGroup;
}
}
}

View File

@@ -0,0 +1,14 @@
using API.Models.Internal.Altersgruppen;
namespace API.Repository.AgeGroup
{
public interface IAgeGroupService
{
public Task<List<AltersGruppe>> GetAllAsync();
public Task<AltersGruppe?> GetAsync(int id);
public Task<AltersGruppe> CreateAsync(AltersGruppe altersGruppe);
public Task<AltersGruppe?> DeleteAsync(int id);
public Task<AltersGruppe?> UpdateAsync(int id, AltersGruppe altersGruppe);
}
}

BIN
API/app.db-shm Normal file

Binary file not shown.

BIN
API/app.db-wal Normal file

Binary file not shown.