adding PersonRepository
This commit is contained in:
1 parent
4ca2f43958
commit
e0d3e80262
2 files changed
+58
No files matched your search
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
using api.Domain;
|
||||||
|
|
||||||
|
namespace api.Repositories
|
||||||
|
{
|
||||||
|
public interface IPersonRepository
|
||||||
|
{
|
||||||
|
Task<IEnumerable<Person>> GetAllAsync();
|
||||||
|
Task<Person> GetByUsernameAsync(String username);
|
||||||
|
Task AddAsync(Person person);
|
||||||
|
Task UpdateAsync(Person person);
|
||||||
|
Task DeleteAsync(String username);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (person != null)
|
||||||
|
{
|
||||||
|
_context.Persons.Remove(person);
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user