76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using Oqtane.Modules;
|
|
|
|
namespace SZUAbsolventenverein.Module.PremiumArea.Repository
|
|
{
|
|
public interface IPremiumAreaRepository
|
|
{
|
|
IEnumerable<Models.PremiumArea> GetPremiumAreas(int ModuleId);
|
|
Models.PremiumArea GetPremiumArea(int PremiumAreaId);
|
|
Models.PremiumArea GetPremiumArea(int PremiumAreaId, bool tracking);
|
|
Models.PremiumArea AddPremiumArea(Models.PremiumArea PremiumArea);
|
|
Models.PremiumArea UpdatePremiumArea(Models.PremiumArea PremiumArea);
|
|
void DeletePremiumArea(int PremiumAreaId);
|
|
}
|
|
|
|
public class PremiumAreaRepository : IPremiumAreaRepository, ITransientService
|
|
{
|
|
private readonly IDbContextFactory<PremiumAreaContext> _factory;
|
|
|
|
public PremiumAreaRepository(IDbContextFactory<PremiumAreaContext> factory)
|
|
{
|
|
_factory = factory;
|
|
}
|
|
|
|
public IEnumerable<Models.PremiumArea> GetPremiumAreas(int ModuleId)
|
|
{
|
|
using var db = _factory.CreateDbContext();
|
|
return db.PremiumArea.Where(item => item.ModuleId == ModuleId).ToList();
|
|
}
|
|
|
|
public Models.PremiumArea GetPremiumArea(int PremiumAreaId)
|
|
{
|
|
return GetPremiumArea(PremiumAreaId, true);
|
|
}
|
|
|
|
public Models.PremiumArea GetPremiumArea(int PremiumAreaId, bool tracking)
|
|
{
|
|
using var db = _factory.CreateDbContext();
|
|
if (tracking)
|
|
{
|
|
return db.PremiumArea.Find(PremiumAreaId);
|
|
}
|
|
else
|
|
{
|
|
return db.PremiumArea.AsNoTracking().FirstOrDefault(item => item.PremiumAreaId == PremiumAreaId);
|
|
}
|
|
}
|
|
|
|
public Models.PremiumArea AddPremiumArea(Models.PremiumArea PremiumArea)
|
|
{
|
|
using var db = _factory.CreateDbContext();
|
|
db.PremiumArea.Add(PremiumArea);
|
|
db.SaveChanges();
|
|
return PremiumArea;
|
|
}
|
|
|
|
public Models.PremiumArea UpdatePremiumArea(Models.PremiumArea PremiumArea)
|
|
{
|
|
using var db = _factory.CreateDbContext();
|
|
db.Entry(PremiumArea).State = EntityState.Modified;
|
|
db.SaveChanges();
|
|
return PremiumArea;
|
|
}
|
|
|
|
public void DeletePremiumArea(int PremiumAreaId)
|
|
{
|
|
using var db = _factory.CreateDbContext();
|
|
Models.PremiumArea PremiumArea = db.PremiumArea.Find(PremiumAreaId);
|
|
db.PremiumArea.Remove(PremiumArea);
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
}
|