Refactored AgeGroup namespace and services, added RegistrationKey functionality.

This commit is contained in:
2026-01-25 21:30:05 +01:00
parent 91dd8d1603
commit ce26a30693
12 changed files with 173 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
using API.Models.Ingoing.Altersgruppen;
using API.Repository.AgeGroup;
using API.Models.Internal.Altersgruppen;
using API.Repository.AgeGroupRepo;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers

View File

@@ -1,10 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using API.Models.Internal.User;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[ApiController]
[Route("api/account")]
public class AuthController : ControllerBase
public class AuthController(UserManager<User> userManager, SignInManager<User> signInManager, IEmailSender emailSender)
: ControllerBase
{
private readonly UserManager<User> _userManager = userManager;
private readonly SignInManager<User> _signInManager = signInManager;
private readonly IEmailSender _emailSender = emailSender;
}

View File

@@ -0,0 +1,21 @@
using API.Repository.RegistrationKeyRepo;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[ApiController]
[Route("api/registrationKey")]
public class RegistrationKeyController : ControllerBase
{
private IRegistrationKeyService _keyService;
private readonly RoleManager<IdentityRole> _roleManager;
public RegistrationKeyController(IRegistrationKeyService keyService, RoleManager<IdentityRole> roleManager)
{
_keyService = keyService;
_roleManager = roleManager;
}
}

View File

@@ -12,6 +12,7 @@ namespace API.Database
}
public DbSet<AltersGruppe> Altersgruppen { get; set; }
public DbSet<RegistrationKey> RegistrationKeys { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

View File

@@ -1,28 +0,0 @@
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

@@ -9,4 +9,27 @@ namespace API.Models.Internal.Altersgruppen
public int StartingAge { get; set; }
public int EndingAge { get; set; }
}
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,24 @@
namespace API.Models.Internal.User;
public class RegistrationKey
{
public string Id { get; set; } = Ulid.NewUlid().ToString();
public string? LinkedRole { get; set; }
public DateTime Created { get; set; } = DateTime.UtcNow;
}
public class RegistrationKeyIngoing
{
public string? LinkedRole { get; set; }
}
public static class RegistrationKeyMapper
{
public static RegistrationKey ToInternalFromIngoing(this RegistrationKeyIngoing key)
{
return new RegistrationKey
{
LinkedRole = key.LinkedRole,
};
}
}

View File

@@ -1,6 +1,6 @@
using API.Database;
using API.Models.Internal.User;
using API.Repository.AgeGroup;
using API.Repository.AgeGroupRepo;
using API.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
@@ -27,7 +27,7 @@ builder.Services.AddIdentityCore<User>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
}).AddEntityFrameworkStores<ApplicationDbContext>().AddRoles<IdentityRole>().AddDefaultTokenProviders();
// Database

View File

@@ -2,7 +2,7 @@
using API.Models.Internal.Altersgruppen;
using Microsoft.EntityFrameworkCore;
namespace API.Repository.AgeGroup
namespace API.Repository.AgeGroupRepo
{
public class AgeGroupService : IAgeGroupService
{

View File

@@ -1,6 +1,6 @@
using API.Models.Internal.Altersgruppen;
namespace API.Repository.AgeGroup
namespace API.Repository.AgeGroupRepo
{
public interface IAgeGroupService
{

View File

@@ -0,0 +1,13 @@
using API.Models.Internal.User;
namespace API.Repository.RegistrationKeyRepo;
public interface IRegistrationKeyService
{
public Task<List<RegistrationKey>> GetAllAsync();
public Task<RegistrationKey?> GetAsync(string id);
public Task<RegistrationKey> CreateAsync(RegistrationKeyIngoing key);
public Task<RegistrationKey?> DeleteAsync(string id);
public Task<int> DeleteOldRegistrationKeysAsync(int x);
}

View File

@@ -0,0 +1,76 @@
using API.Database;
using API.Models.Internal.User;
using Microsoft.EntityFrameworkCore;
namespace API.Repository.RegistrationKeyRepo;
public class RegistrationKeyService : IRegistrationKeyService
{
private ApplicationDbContext _context;
public RegistrationKeyService(ApplicationDbContext context)
{
_context = context;
}
public async Task<List<RegistrationKey>> GetAllAsync()
{
var allKeys = await _context.RegistrationKeys.ToListAsync();
return allKeys;
}
public async Task<RegistrationKey?> GetAsync(string id)
{
return await _context.RegistrationKeys.FindAsync(id);
}
public async Task<RegistrationKey> CreateAsync(RegistrationKeyIngoing key)
{
var internalKey = key.ToInternalFromIngoing();
await _context.RegistrationKeys.AddAsync(internalKey);
await _context.SaveChangesAsync();
return internalKey;
}
public async Task<RegistrationKey?> DeleteAsync(string id)
{
var key = await _context.RegistrationKeys.FirstOrDefaultAsync(x => x.Id == id);
if (key == null)
{
return null;
}
_context.RegistrationKeys.Remove(key);
await _context.SaveChangesAsync();
return key;
}
public async Task<int> DeleteOldRegistrationKeysAsync(int x)
{
if (x <= 0)
{
return 0;
}
var cutoff = DateTime.UtcNow.AddDays(-x);
var oldKeys = await _context.RegistrationKeys
.Where(k => k.Created < cutoff)
.ToListAsync();
if (oldKeys.Count == 0)
{
return 0;
}
_context.RegistrationKeys.RemoveRange(oldKeys);
await _context.SaveChangesAsync();
return oldKeys.Count;
}
}