69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|