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

@@ -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;
}
}