Refactored project to module structure
This commit is contained in:
49
Moonlight.Api/Infrastructure/Database/DatabaseRepository.cs
Normal file
49
Moonlight.Api/Infrastructure/Database/DatabaseRepository.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Infrastructure.Database.Interfaces;
|
||||
|
||||
namespace Moonlight.Api.Infrastructure.Database;
|
||||
|
||||
public class DatabaseRepository<T> where T : class
|
||||
{
|
||||
private readonly DataContext DataContext;
|
||||
private readonly DbSet<T> Set;
|
||||
|
||||
public DatabaseRepository(DataContext dataContext)
|
||||
{
|
||||
DataContext = dataContext;
|
||||
Set = DataContext.Set<T>();
|
||||
}
|
||||
|
||||
public IQueryable<T> Query()
|
||||
{
|
||||
return Set;
|
||||
}
|
||||
|
||||
public async Task<T> AddAsync(T entity)
|
||||
{
|
||||
if (entity is IActionTimestamps actionTimestamps)
|
||||
{
|
||||
actionTimestamps.CreatedAt = DateTimeOffset.UtcNow;
|
||||
actionTimestamps.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
var final = Set.Add(entity);
|
||||
await DataContext.SaveChangesAsync();
|
||||
return final.Entity;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(T entity)
|
||||
{
|
||||
if (entity is IActionTimestamps actionTimestamps)
|
||||
actionTimestamps.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
Set.Update(entity);
|
||||
await DataContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(T entity)
|
||||
{
|
||||
Set.Remove(entity);
|
||||
await DataContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user