Files
probearbeit/api/Repositories/PersonRepository.cs
T
2026-08-31 13:54:35 +02:00

42 lines
1.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using api.Data;
using api.Domain;
using api.Repositories;
public class PersonRepository(AppDbContext context) : IPersonRepository
{
private readonly AppDbContext _context = context;
public async Task<IEnumerable<Person>> GetAllAsync()
{
return await _context.Persons.ToListAsync();
}
public async Task AddAsync(Person person)
{
await _context.Persons.AddAsync(person);
await _context.SaveChangesAsync();
}
public async Task<Person> GetByUsernameAsync(string username)
{
return await _context.Persons.FindAsync(username) ?? throw new KeyNotFoundException("User does not exist");
}
public async Task UpdateAsync(Person person)
{
_context.Persons.Update(person);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(string username)
{
Person person = await _context.Persons.FindAsync(username) ?? throw new KeyNotFoundException("User does not exist");;
_context.Persons.Remove(person);
await _context.SaveChangesAsync();
}
}