New: DB-Migration / Repositories und Models für den Premiumbereich
This commit is contained in:
60
Server/Repository/UserPremiumRepository.cs
Normal file
60
Server/Repository/UserPremiumRepository.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Oqtane.Modules;
|
||||
using SZUAbsolventenverein.Module.PremiumArea.Models;
|
||||
|
||||
namespace SZUAbsolventenverein.Module.PremiumArea.Repository
|
||||
{
|
||||
public interface IUserPremiumRepository
|
||||
{
|
||||
UserPremium GetUserPremium(int UserId);
|
||||
UserPremium SaveUserPremium(UserPremium UserPremium);
|
||||
void AddPremiumEvent(PremiumEvent premiumEvent);
|
||||
IEnumerable<PremiumEvent> GetPremiumEvents(int UserId);
|
||||
}
|
||||
|
||||
public class UserPremiumRepository : IUserPremiumRepository, ITransientService
|
||||
{
|
||||
private readonly IDbContextFactory<PremiumAreaContext> _factory;
|
||||
|
||||
public UserPremiumRepository(IDbContextFactory<PremiumAreaContext> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public UserPremium GetUserPremium(int UserId)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
return db.UserPremium.FirstOrDefault(item => item.UserId == UserId);
|
||||
}
|
||||
|
||||
public UserPremium SaveUserPremium(UserPremium UserPremium)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
if (UserPremium.Id > 0)
|
||||
{
|
||||
db.Entry(UserPremium).State = EntityState.Modified;
|
||||
}
|
||||
else
|
||||
{
|
||||
db.UserPremium.Add(UserPremium);
|
||||
}
|
||||
db.SaveChanges();
|
||||
return UserPremium;
|
||||
}
|
||||
|
||||
public void AddPremiumEvent(PremiumEvent premiumEvent)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
db.PremiumEvent.Add(premiumEvent);
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public IEnumerable<PremiumEvent> GetPremiumEvents(int UserId)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
return db.PremiumEvent.Where(item => item.UserId == UserId).OrderByDescending(x => x.CreatedOn).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user