mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 14:22:40 +01:00
Saving notifications to database and loading them from there
This commit is contained in:
parent
9206f0cd2e
commit
4bc0fd98ca
2 changed files with 27 additions and 14 deletions
|
@ -4,11 +4,13 @@ using System.Linq;
|
|||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Data;
|
||||
using BTCPayServer.Events.Notifications;
|
||||
using BTCPayServer.Filters;
|
||||
using BTCPayServer.HostedServices;
|
||||
using BTCPayServer.Models.NotificationViewModels;
|
||||
using BTCPayServer.Security;
|
||||
using Google;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
@ -18,10 +20,12 @@ namespace BTCPayServer.Controllers
|
|||
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
|
||||
public class NotificationsController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly EventAggregator _eventAggregator;
|
||||
|
||||
public NotificationsController(EventAggregator eventAggregator)
|
||||
public NotificationsController(ApplicationDbContext db, EventAggregator eventAggregator)
|
||||
{
|
||||
_db = db;
|
||||
_eventAggregator = eventAggregator;
|
||||
}
|
||||
|
||||
|
@ -35,13 +39,14 @@ namespace BTCPayServer.Controllers
|
|||
var userId = claimWithId.Value;
|
||||
var model = new IndexViewModel()
|
||||
{
|
||||
Items = NotificationDbSaver.Notif
|
||||
Items = _db.Notifications
|
||||
.OrderByDescending(a => a.Created)
|
||||
.Skip(skip).Take(count)
|
||||
.Where(a => a.ApplicationUserId == userId)
|
||||
.Select(a => a.ViewModel())
|
||||
.ToList()
|
||||
};
|
||||
model.Items = model.Items.OrderByDescending(a => a.Created).ToList();
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,15 +13,17 @@ namespace BTCPayServer.HostedServices
|
|||
{
|
||||
public class NotificationDbSaver : EventHostedServiceBase
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly UserManager<ApplicationUser> _UserManager;
|
||||
private readonly ApplicationDbContextFactory _ContextFactory;
|
||||
|
||||
public NotificationDbSaver(UserManager<ApplicationUser> userManager,
|
||||
EventAggregator eventAggregator) : base(eventAggregator) {
|
||||
_userManager = userManager;
|
||||
ApplicationDbContextFactory contextFactory,
|
||||
EventAggregator eventAggregator) : base(eventAggregator)
|
||||
{
|
||||
_UserManager = userManager;
|
||||
_ContextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public static List<NotificationData> Notif = new List<NotificationData>();
|
||||
|
||||
protected override void SubscribeToEvents()
|
||||
{
|
||||
Subscribe<NewVersionNotification>();
|
||||
|
@ -34,13 +36,19 @@ namespace BTCPayServer.HostedServices
|
|||
{
|
||||
var data = (evt as NewVersionNotification).ToData();
|
||||
|
||||
var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
||||
foreach (var admin in admins)
|
||||
{
|
||||
data.Id = Guid.NewGuid().ToString();
|
||||
data.ApplicationUserId = admin.Id;
|
||||
var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
||||
|
||||
Notif.Add(data);
|
||||
using (var db = _ContextFactory.CreateContext())
|
||||
{
|
||||
foreach (var admin in admins)
|
||||
{
|
||||
data.Id = Guid.NewGuid().ToString();
|
||||
data.ApplicationUserId = admin.Id;
|
||||
|
||||
db.Notifications.Add(data);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue