| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Net.Http.Headers; |
| | 5 | | using System.Security.Claims; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Microsoft.AspNetCore.Identity; |
| | 9 | | using Microsoft.EntityFrameworkCore; |
| | 10 | | using Microsoft.Extensions.Logging; |
| | 11 | | using MUNity.Database.Context; |
| | 12 | | using MUNity.Database.Models.User; |
| | 13 | | using MUNity.Schema.UserNotification; |
| | 14 | |
|
| | 15 | | namespace MUNity.Services |
| | 16 | | { |
| | 17 | | public class UserNotificationService |
| | 18 | | { |
| | 19 | | private ILogger<UserNotificationService> _logger; |
| | 20 | |
|
| | 21 | | private UserManager<MunityUser> _userManager; |
| | 22 | |
|
| | 23 | | private MunityContext _munityContext; |
| | 24 | |
|
| | 25 | | public async Task<List<UserNotificationItem>> GetLastFiveIntrestingNotifications(ClaimsPrincipal claim) |
| 0 | 26 | | { |
| 0 | 27 | | if (claim == null) |
| 0 | 28 | | { |
| 0 | 29 | | _logger.LogWarning($"Getting the last five notifications has been called with an invalid claim (null).") |
| 0 | 30 | | return new List<UserNotificationItem>(); |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | var user = await _userManager.GetUserAsync(claim); |
| 0 | 34 | | if (user == null) |
| 0 | 35 | | { |
| 0 | 36 | | _logger.LogWarning($"Unable to find the user by the given claim {claim.Identity?.Name}"); |
| 0 | 37 | | return new List<UserNotificationItem>(); |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | var result = _munityContext.UserNotifications |
| 0 | 41 | | .Where(n => n.User.UserName == claim.Identity.Name && n.IsRead == false) |
| 0 | 42 | | .Include(n => n.Category) |
| 0 | 43 | | .OrderByDescending(n => n.Timestamp) |
| 0 | 44 | | .Take(5) |
| 0 | 45 | | .Select(n => new UserNotificationItem() |
| 0 | 46 | | { |
| 0 | 47 | | Title = n.Title, |
| 0 | 48 | | Timestamp = n.Timestamp, |
| 0 | 49 | | CategoryId = n.Category.UserNotificationCategoryId, |
| 0 | 50 | | CategoryName = n.Category.CategoryName, |
| 0 | 51 | | NotificationId = n.UserNotificationId, |
| 0 | 52 | | Text = n.Text |
| 0 | 53 | | }) |
| 0 | 54 | | .ToList(); |
| | 55 | |
|
| 0 | 56 | | if (result.Count < 5) |
| 0 | 57 | | { |
| 0 | 58 | | var readNotifications = _munityContext.UserNotifications |
| 0 | 59 | | .Where(n => n.User.UserName == claim.Identity.Name && n.IsRead == true) |
| 0 | 60 | | .Include(n => n.Category) |
| 0 | 61 | | .OrderByDescending(n => n.Timestamp) |
| 0 | 62 | | .Take(5 - result.Count) |
| 0 | 63 | | .Select(n => new UserNotificationItem() |
| 0 | 64 | | { |
| 0 | 65 | | Title = n.Title, |
| 0 | 66 | | Timestamp = n.Timestamp, |
| 0 | 67 | | CategoryId = n.Category.UserNotificationCategoryId, |
| 0 | 68 | | CategoryName = n.Category.CategoryName, |
| 0 | 69 | | NotificationId = n.UserNotificationId, |
| 0 | 70 | | Text = n.Text |
| 0 | 71 | | }) |
| 0 | 72 | | .ToList(); |
| | 73 | |
|
| 0 | 74 | | result.AddRange(readNotifications); |
| 0 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | if (result.Count > 0) |
| 0 | 78 | | result = result.OrderByDescending(n => n.Timestamp).ToList(); |
| | 79 | |
|
| 0 | 80 | | return result; |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public async Task<int> GetCountOfUnreadNotifications(ClaimsPrincipal claim) |
| 0 | 84 | | { |
| 0 | 85 | | if (claim == null) |
| 0 | 86 | | { |
| 0 | 87 | | _logger.LogWarning($"GetCountOfUnreadNotifications was called with a claim null"); |
| 0 | 88 | | return 0; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | var user = await _userManager.GetUserAsync(claim); |
| 0 | 92 | | if (user == null) |
| 0 | 93 | | { |
| 0 | 94 | | _logger.LogWarning("GetCountOfUnreadNotifications the given user was not found"); |
| 0 | 95 | | return 0; |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | return _munityContext.UserNotifications.Count(n => |
| 0 | 99 | | n.User.UserName == claim.Identity.Name && n.IsRead == false); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public async Task<int> GetTotalCountOfNotifications(ClaimsPrincipal claim) |
| 0 | 103 | | { |
| 0 | 104 | | if (claim == null) |
| 0 | 105 | | { |
| 0 | 106 | | _logger.LogWarning($"GetTotalCountOfNotifications was called with a claim null"); |
| 0 | 107 | | return 0; |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | var user = await _userManager.GetUserAsync(claim); |
| 0 | 111 | | if (user == null) |
| 0 | 112 | | { |
| 0 | 113 | | _logger.LogWarning("GetTotalCountOfNotifications the given user was not found"); |
| 0 | 114 | | return 0; |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | return _munityContext.UserNotifications.Count(n => |
| 0 | 118 | | n.User.UserName == claim.Identity.Name); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | |
|
| 0 | 122 | | public UserNotificationService(UserManager<MunityUser> userManager, MunityContext munityContext, ILogger<UserNot |
| 0 | 123 | | { |
| 0 | 124 | | _userManager = userManager; |
| 0 | 125 | | _munityContext = munityContext; |
| 0 | 126 | | _logger = logger; |
| 0 | 127 | | } |
| | 128 | | } |
| | 129 | | } |