1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-24 04:42:56 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Database/Factories/AbstractUpsertHelper.cs

31 lines
610 B
C#

using Microsoft.EntityFrameworkCore;
namespace Phantom.Server.Database.Factories;
public abstract class AbstractUpsertHelper<T> where T : class {
private protected readonly ApplicationDbContext Ctx;
internal AbstractUpsertHelper(ApplicationDbContext ctx) {
this.Ctx = ctx;
}
private protected abstract DbSet<T> Set { get; }
private protected abstract T Construct(Guid guid);
public T Fetch(Guid guid) {
DbSet<T> set = Set;
T? entity = set.Find(guid);
if (entity == null) {
entity = Construct(guid);
set.Add(entity);
}
else {
set.Update(entity);
}
return entity;
}
}