Saving notifications to database and loading them from there

This commit is contained in:
rockstardev
2020-05-28 21:54:26 -05:00
parent 9206f0cd2e
commit 4bc0fd98ca
2 changed files with 27 additions and 14 deletions

View File

@@ -4,11 +4,13 @@ using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Events.Notifications; using BTCPayServer.Events.Notifications;
using BTCPayServer.Filters; using BTCPayServer.Filters;
using BTCPayServer.HostedServices; using BTCPayServer.HostedServices;
using BTCPayServer.Models.NotificationViewModels; using BTCPayServer.Models.NotificationViewModels;
using BTCPayServer.Security; using BTCPayServer.Security;
using Google;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -18,10 +20,12 @@ namespace BTCPayServer.Controllers
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public class NotificationsController : Controller public class NotificationsController : Controller
{ {
private readonly ApplicationDbContext _db;
private readonly EventAggregator _eventAggregator; private readonly EventAggregator _eventAggregator;
public NotificationsController(EventAggregator eventAggregator) public NotificationsController(ApplicationDbContext db, EventAggregator eventAggregator)
{ {
_db = db;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
} }
@@ -35,13 +39,14 @@ namespace BTCPayServer.Controllers
var userId = claimWithId.Value; var userId = claimWithId.Value;
var model = new IndexViewModel() var model = new IndexViewModel()
{ {
Items = NotificationDbSaver.Notif Items = _db.Notifications
.OrderByDescending(a => a.Created)
.Skip(skip).Take(count) .Skip(skip).Take(count)
.Where(a => a.ApplicationUserId == userId) .Where(a => a.ApplicationUserId == userId)
.Select(a => a.ViewModel()) .Select(a => a.ViewModel())
.ToList() .ToList()
}; };
model.Items = model.Items.OrderByDescending(a => a.Created).ToList();
return View(model); return View(model);
} }

View File

@@ -13,15 +13,17 @@ namespace BTCPayServer.HostedServices
{ {
public class NotificationDbSaver : EventHostedServiceBase public class NotificationDbSaver : EventHostedServiceBase
{ {
private readonly UserManager<ApplicationUser> _userManager; private readonly UserManager<ApplicationUser> _UserManager;
private readonly ApplicationDbContextFactory _ContextFactory;
public NotificationDbSaver(UserManager<ApplicationUser> userManager, public NotificationDbSaver(UserManager<ApplicationUser> userManager,
EventAggregator eventAggregator) : base(eventAggregator) { ApplicationDbContextFactory contextFactory,
_userManager = userManager; EventAggregator eventAggregator) : base(eventAggregator)
{
_UserManager = userManager;
_ContextFactory = contextFactory;
} }
public static List<NotificationData> Notif = new List<NotificationData>();
protected override void SubscribeToEvents() protected override void SubscribeToEvents()
{ {
Subscribe<NewVersionNotification>(); Subscribe<NewVersionNotification>();
@@ -34,13 +36,19 @@ namespace BTCPayServer.HostedServices
{ {
var data = (evt as NewVersionNotification).ToData(); var data = (evt as NewVersionNotification).ToData();
var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin); var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);
using (var db = _ContextFactory.CreateContext())
{
foreach (var admin in admins) foreach (var admin in admins)
{ {
data.Id = Guid.NewGuid().ToString(); data.Id = Guid.NewGuid().ToString();
data.ApplicationUserId = admin.Id; data.ApplicationUserId = admin.Id;
Notif.Add(data); db.Notifications.Add(data);
}
await db.SaveChangesAsync();
} }
} }
} }