Refactored AgeGroup namespace and services, added RegistrationKey functionality.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
21
API/Controllers/RegistrationKeyController.cs
Normal file
21
API/Controllers/RegistrationKeyController.cs
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
API/Models/Internal/User/RegistrationKey.cs
Normal file
24
API/Models/Internal/User/RegistrationKey.cs
Normal 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using API.Models.Internal.Altersgruppen;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Repository.AgeGroup
|
||||
namespace API.Repository.AgeGroupRepo
|
||||
{
|
||||
public class AgeGroupService : IAgeGroupService
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using API.Models.Internal.Altersgruppen;
|
||||
|
||||
namespace API.Repository.AgeGroup
|
||||
namespace API.Repository.AgeGroupRepo
|
||||
{
|
||||
public interface IAgeGroupService
|
||||
{
|
||||
@@ -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);
|
||||
}
|
||||
76
API/Repository/RegistrationKeyRepo/RegistrationKeyService.cs
Normal file
76
API/Repository/RegistrationKeyRepo/RegistrationKeyService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user