< Summary

Class:MUNity.Services.UserNotificationService
Assembly:MUNity.Services
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityServices\Services\UserNotificationService.cs
Covered lines:0
Uncovered lines:80
Coverable lines:80
Total lines:129
Line coverage:0% (0 of 80)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)
Covered methods:0
Total methods:4
Method coverage:0% (0 of 4)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
GetLastFiveIntrestingNotifications()0%100%
GetCountOfUnreadNotifications()0%40%
GetTotalCountOfNotifications()0%40%
.ctor(...)100%10%

File(s)

C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityServices\Services\UserNotificationService.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Net.Http.Headers;
 5using System.Security.Claims;
 6using System.Text;
 7using System.Threading.Tasks;
 8using Microsoft.AspNetCore.Identity;
 9using Microsoft.EntityFrameworkCore;
 10using Microsoft.Extensions.Logging;
 11using MUNity.Database.Context;
 12using MUNity.Database.Models.User;
 13using MUNity.Schema.UserNotification;
 14
 15namespace 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)
 026        {
 027            if (claim == null)
 028            {
 029                _logger.LogWarning($"Getting the last five notifications has been called with an invalid claim (null).")
 030                return new List<UserNotificationItem>();
 31            }
 32
 033            var user = await _userManager.GetUserAsync(claim);
 034            if (user == null)
 035            {
 036                _logger.LogWarning($"Unable to find the user by the given claim {claim.Identity?.Name}");
 037                return new List<UserNotificationItem>();
 38            }
 39
 040            var result = _munityContext.UserNotifications
 041                .Where(n => n.User.UserName == claim.Identity.Name && n.IsRead == false)
 042                .Include(n => n.Category)
 043                .OrderByDescending(n => n.Timestamp)
 044                .Take(5)
 045                .Select(n => new UserNotificationItem()
 046                {
 047                    Title = n.Title,
 048                    Timestamp = n.Timestamp,
 049                    CategoryId = n.Category.UserNotificationCategoryId,
 050                    CategoryName = n.Category.CategoryName,
 051                    NotificationId = n.UserNotificationId,
 052                    Text = n.Text
 053                })
 054                .ToList();
 55
 056            if (result.Count < 5)
 057            {
 058                var readNotifications = _munityContext.UserNotifications
 059                    .Where(n => n.User.UserName == claim.Identity.Name && n.IsRead == true)
 060                    .Include(n => n.Category)
 061                    .OrderByDescending(n => n.Timestamp)
 062                    .Take(5 - result.Count)
 063                    .Select(n => new UserNotificationItem()
 064                    {
 065                        Title = n.Title,
 066                        Timestamp = n.Timestamp,
 067                        CategoryId = n.Category.UserNotificationCategoryId,
 068                        CategoryName = n.Category.CategoryName,
 069                        NotificationId = n.UserNotificationId,
 070                        Text = n.Text
 071                    })
 072                    .ToList();
 73
 074                result.AddRange(readNotifications);
 075            }
 76
 077            if (result.Count > 0)
 078                result = result.OrderByDescending(n => n.Timestamp).ToList();
 79
 080            return result;
 081        }
 82
 83        public async Task<int> GetCountOfUnreadNotifications(ClaimsPrincipal claim)
 084        {
 085            if (claim == null)
 086            {
 087                _logger.LogWarning($"GetCountOfUnreadNotifications was called with a claim null");
 088                return 0;
 89            }
 90
 091            var user = await _userManager.GetUserAsync(claim);
 092            if (user == null)
 093            {
 094                _logger.LogWarning("GetCountOfUnreadNotifications the given user was not found");
 095                return 0;
 96            }
 97
 098            return _munityContext.UserNotifications.Count(n =>
 099                n.User.UserName == claim.Identity.Name && n.IsRead == false);
 0100        }
 101
 102        public async Task<int> GetTotalCountOfNotifications(ClaimsPrincipal claim)
 0103        {
 0104            if (claim == null)
 0105            {
 0106                _logger.LogWarning($"GetTotalCountOfNotifications was called with a claim null");
 0107                return 0;
 108            }
 109
 0110            var user = await _userManager.GetUserAsync(claim);
 0111            if (user == null)
 0112            {
 0113                _logger.LogWarning("GetTotalCountOfNotifications the given user was not found");
 0114                return 0;
 115            }
 116
 0117            return _munityContext.UserNotifications.Count(n =>
 0118                n.User.UserName == claim.Identity.Name);
 0119        }
 120
 121
 0122        public UserNotificationService(UserManager<MunityUser> userManager, MunityContext munityContext, ILogger<UserNot
 0123        {
 0124            _userManager = userManager;
 0125            _munityContext = munityContext;
 0126            _logger = logger;
 0127        }
 128    }
 129}