Simple Database + Endpoints
This commit is contained in:
68
API/Repository/AgeGroup/AgeGroupService.cs
Normal file
68
API/Repository/AgeGroup/AgeGroupService.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user