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 CreateAsync(AltersGruppe altersGruppe) { await _context.Altersgruppen.AddAsync(altersGruppe); await _context.SaveChangesAsync(); return altersGruppe; } public async Task 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> GetAllAsync() { var allGroups = await _context.Altersgruppen.ToListAsync(); return allGroups; } public async Task GetAsync(int id) { return await _context.Altersgruppen.FindAsync(id); } public async Task 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; } } }