From 95a751c505ff98b2d211d121f42dc60891b1fb38 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Wed, 10 Jun 2020 18:55:31 -0500 Subject: [PATCH] Displaying last 5 notifications in Layout --- .../HostedServices/NotificationDbSaver.cs | 29 ++++++++++++-- BTCPayServer/Views/Shared/_Layout.cshtml | 40 +++++++++++++++---- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/BTCPayServer/HostedServices/NotificationDbSaver.cs b/BTCPayServer/HostedServices/NotificationDbSaver.cs index 39e12dc69..0077c17c5 100644 --- a/BTCPayServer/HostedServices/NotificationDbSaver.cs +++ b/BTCPayServer/HostedServices/NotificationDbSaver.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using BTCPayServer.Data; using BTCPayServer.Events; using BTCPayServer.Events.Notifications; +using BTCPayServer.Models.NotificationViewModels; using Microsoft.AspNetCore.Identity; namespace BTCPayServer.HostedServices @@ -64,15 +65,37 @@ namespace BTCPayServer.HostedServices _db = db; } - public int GetNotificationCount(ClaimsPrincipal user) + public NotificationSummaryViewModel GetSummaryNotifications(ClaimsPrincipal user) { + var resp = new NotificationSummaryViewModel(); var claimWithId = user.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier); // TODO: Soft caching in order not to pound database too much - var count = _db.Notifications + resp.UnseenCount = _db.Notifications .Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen) .Count(); - return count; + + if (resp.UnseenCount > 0) + { + resp.Last5 = _db.Notifications + .Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen) + .OrderByDescending(a => a.Created) + .Take(5) + .Select(a => a.ViewModel()) + .ToList(); + } + else + { + resp.Last5 = new List(); + } + + return resp; } } + + public class NotificationSummaryViewModel + { + public int UnseenCount { get; set; } + public List Last5 { get; set; } + } } diff --git a/BTCPayServer/Views/Shared/_Layout.cshtml b/BTCPayServer/Views/Shared/_Layout.cshtml index 792ed5a9a..9a0dc7b29 100644 --- a/BTCPayServer/Views/Shared/_Layout.cshtml +++ b/BTCPayServer/Views/Shared/_Layout.cshtml @@ -79,14 +79,38 @@ - + + var notificationModel = notificationManager.GetSummaryNotifications(User); + @if (notificationModel.UnseenCount > 0) + { + + } + else + { + + } +