2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
|
|
|
|
|
using LANCommander.Server.Models;
|
2023-08-25 19:25:28 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2023-04-20 00:24:46 -05:00
|
|
|
|
public abstract class BaseDatabaseService<T> : BaseService where T : BaseModel
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
public DatabaseContext Context { get; set; }
|
|
|
|
|
|
public HttpContext HttpContext { get; set; }
|
|
|
|
|
|
|
2024-01-04 18:23:54 -06:00
|
|
|
|
public BaseDatabaseService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor) : base()
|
|
|
|
|
|
{
|
2023-01-09 01:05:40 -06:00
|
|
|
|
Context = dbContext;
|
|
|
|
|
|
HttpContext = httpContextAccessor.HttpContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-25 19:25:28 -05:00
|
|
|
|
public virtual async Task<ICollection<T>> Get()
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2023-08-25 19:25:28 -05:00
|
|
|
|
return await Get(x => true).ToListAsync();
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-09 01:35:08 -06:00
|
|
|
|
public virtual async Task<T> Get(Guid id)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
using (var repo = new Repository<T>(Context, HttpContext))
|
|
|
|
|
|
{
|
|
|
|
|
|
return await repo.Find(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-09 01:35:08 -06:00
|
|
|
|
public virtual IQueryable<T> Get(Expression<Func<T, bool>> predicate)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
using (var repo = new Repository<T>(Context, HttpContext))
|
|
|
|
|
|
{
|
|
|
|
|
|
return repo.Get(predicate);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-09 01:35:08 -06:00
|
|
|
|
public virtual bool Exists(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Get(id) != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual async Task<T> Add(T entity)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
using (var repo = new Repository<T>(Context, HttpContext))
|
|
|
|
|
|
{
|
|
|
|
|
|
entity = await repo.Add(entity);
|
|
|
|
|
|
await repo.SaveChanges();
|
|
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-10 17:17:54 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Adds an entity to the database if it does exist as dictated by the predicate
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="predicate">Qualifier expressoin</param>
|
|
|
|
|
|
/// <param name="entity">Entity to add</param>
|
|
|
|
|
|
/// <returns>Newly created or existing entity</returns>
|
2024-03-29 17:18:18 -05:00
|
|
|
|
public virtual async Task<ExistingEntityResult<T>> AddMissing(Expression<Func<T, bool>> predicate, T entity)
|
2023-01-10 17:17:54 -06:00
|
|
|
|
{
|
|
|
|
|
|
using (var repo = new Repository<T>(Context, HttpContext))
|
|
|
|
|
|
{
|
|
|
|
|
|
var existing = repo.Get(predicate).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (existing == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
entity = await repo.Add(entity);
|
|
|
|
|
|
|
|
|
|
|
|
await repo.SaveChanges();
|
|
|
|
|
|
|
2024-03-29 17:18:18 -05:00
|
|
|
|
return new ExistingEntityResult<T>
|
|
|
|
|
|
{
|
|
|
|
|
|
Value = entity,
|
|
|
|
|
|
Existing = false
|
|
|
|
|
|
};
|
2023-01-10 17:17:54 -06:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-03-29 17:18:18 -05:00
|
|
|
|
return new ExistingEntityResult<T>
|
|
|
|
|
|
{
|
|
|
|
|
|
Value = existing,
|
|
|
|
|
|
Existing = true
|
|
|
|
|
|
};
|
2023-01-10 17:17:54 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-09 01:35:08 -06:00
|
|
|
|
public virtual async Task<T> Update(T entity)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
using (var repo = new Repository<T>(Context, HttpContext))
|
|
|
|
|
|
{
|
2023-01-14 15:10:41 -06:00
|
|
|
|
var existing = await repo.Find(entity.Id);
|
2023-01-10 18:58:44 -06:00
|
|
|
|
|
2023-01-14 15:10:41 -06:00
|
|
|
|
Context.Entry(existing).CurrentValues.SetValues(entity);
|
|
|
|
|
|
|
|
|
|
|
|
entity = repo.Update(existing);
|
2023-01-09 01:05:40 -06:00
|
|
|
|
await repo.SaveChanges();
|
|
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-09 01:35:08 -06:00
|
|
|
|
public virtual async Task Delete(T entity)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
using (var repo = new Repository<T>(Context, HttpContext))
|
|
|
|
|
|
{
|
|
|
|
|
|
repo.Delete(entity);
|
|
|
|
|
|
await repo.SaveChanges();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|