diff --git a/API/Controllers/AgeGroupController.cs b/API/Controllers/AgeGroupController.cs index 8280bf1..2d9241f 100644 --- a/API/Controllers/AgeGroupController.cs +++ b/API/Controllers/AgeGroupController.cs @@ -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 diff --git a/API/Controllers/AuthController.cs b/API/Controllers/AuthController.cs index fea2dea..e0458ec 100644 --- a/API/Controllers/AuthController.cs +++ b/API/Controllers/AuthController.cs @@ -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 userManager, SignInManager signInManager, IEmailSender emailSender) + : ControllerBase { - + private readonly UserManager _userManager = userManager; + private readonly SignInManager _signInManager = signInManager; + private readonly IEmailSender _emailSender = emailSender; } diff --git a/API/Controllers/RegistrationKeyController.cs b/API/Controllers/RegistrationKeyController.cs new file mode 100644 index 0000000..330a5c3 --- /dev/null +++ b/API/Controllers/RegistrationKeyController.cs @@ -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 _roleManager; + + public RegistrationKeyController(IRegistrationKeyService keyService, RoleManager roleManager) + { + _keyService = keyService; + _roleManager = roleManager; + } + + +} \ No newline at end of file diff --git a/API/Database/ApplicationDbContext.cs b/API/Database/ApplicationDbContext.cs index 029f21e..aaedc61 100644 --- a/API/Database/ApplicationDbContext.cs +++ b/API/Database/ApplicationDbContext.cs @@ -12,6 +12,7 @@ namespace API.Database } public DbSet Altersgruppen { get; set; } + public DbSet RegistrationKeys { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/API/Models/Ingoing/Altersgruppen/AltersGruppeIngoing.cs b/API/Models/Ingoing/Altersgruppen/AltersGruppeIngoing.cs deleted file mode 100644 index d382c45..0000000 --- a/API/Models/Ingoing/Altersgruppen/AltersGruppeIngoing.cs +++ /dev/null @@ -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, - }; - } - } -} diff --git a/API/Models/Internal/Altersgruppen/AltersGruppe.cs b/API/Models/Internal/Altersgruppen/AltersGruppe.cs index ad480fc..28b1db5 100644 --- a/API/Models/Internal/Altersgruppen/AltersGruppe.cs +++ b/API/Models/Internal/Altersgruppen/AltersGruppe.cs @@ -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, + }; + } + } } diff --git a/API/Models/Internal/User/RegistrationKey.cs b/API/Models/Internal/User/RegistrationKey.cs new file mode 100644 index 0000000..1a6c690 --- /dev/null +++ b/API/Models/Internal/User/RegistrationKey.cs @@ -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, + }; + } +} \ No newline at end of file diff --git a/API/Program.cs b/API/Program.cs index f15c0a9..0403ead 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -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(options => { options.SignIn.RequireConfirmedAccount = true; options.User.RequireUniqueEmail = true; -}).AddEntityFrameworkStores().AddDefaultTokenProviders(); +}).AddEntityFrameworkStores().AddRoles().AddDefaultTokenProviders(); // Database diff --git a/API/Repository/AgeGroup/AgeGroupService.cs b/API/Repository/AgeGroupRepo/AgeGroupService.cs similarity index 97% rename from API/Repository/AgeGroup/AgeGroupService.cs rename to API/Repository/AgeGroupRepo/AgeGroupService.cs index 3b41cd6..d66249a 100644 --- a/API/Repository/AgeGroup/AgeGroupService.cs +++ b/API/Repository/AgeGroupRepo/AgeGroupService.cs @@ -2,7 +2,7 @@ using API.Models.Internal.Altersgruppen; using Microsoft.EntityFrameworkCore; -namespace API.Repository.AgeGroup +namespace API.Repository.AgeGroupRepo { public class AgeGroupService : IAgeGroupService { diff --git a/API/Repository/AgeGroup/IAgeGroupService.cs b/API/Repository/AgeGroupRepo/IAgeGroupService.cs similarity index 91% rename from API/Repository/AgeGroup/IAgeGroupService.cs rename to API/Repository/AgeGroupRepo/IAgeGroupService.cs index 9da50d3..2095cac 100644 --- a/API/Repository/AgeGroup/IAgeGroupService.cs +++ b/API/Repository/AgeGroupRepo/IAgeGroupService.cs @@ -1,6 +1,6 @@ using API.Models.Internal.Altersgruppen; -namespace API.Repository.AgeGroup +namespace API.Repository.AgeGroupRepo { public interface IAgeGroupService { diff --git a/API/Repository/RegistrationKeyRepo/IRegistrationKeyService.cs b/API/Repository/RegistrationKeyRepo/IRegistrationKeyService.cs new file mode 100644 index 0000000..630f33c --- /dev/null +++ b/API/Repository/RegistrationKeyRepo/IRegistrationKeyService.cs @@ -0,0 +1,13 @@ +using API.Models.Internal.User; + +namespace API.Repository.RegistrationKeyRepo; + +public interface IRegistrationKeyService +{ + public Task> GetAllAsync(); + public Task GetAsync(string id); + public Task CreateAsync(RegistrationKeyIngoing key); + public Task DeleteAsync(string id); + + public Task DeleteOldRegistrationKeysAsync(int x); +} \ No newline at end of file diff --git a/API/Repository/RegistrationKeyRepo/RegistrationKeyService.cs b/API/Repository/RegistrationKeyRepo/RegistrationKeyService.cs new file mode 100644 index 0000000..6babbf5 --- /dev/null +++ b/API/Repository/RegistrationKeyRepo/RegistrationKeyService.cs @@ -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> GetAllAsync() + { + var allKeys = await _context.RegistrationKeys.ToListAsync(); + + return allKeys; + } + + public async Task GetAsync(string id) + { + return await _context.RegistrationKeys.FindAsync(id); + } + + public async Task CreateAsync(RegistrationKeyIngoing key) + { + var internalKey = key.ToInternalFromIngoing(); + + await _context.RegistrationKeys.AddAsync(internalKey); + await _context.SaveChangesAsync(); + + return internalKey; + } + + public async Task 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 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; + } +} \ No newline at end of file