< Summary - Syki

Information
Class: Syki.Back.Features.Notifications.GetNotifications.GetNotificationsService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Notifications/GetNotifications/GetNotificationsService.cs
Tag: 97_27801654829
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 60
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Get()0%2040%

File(s)

/home/runner/work/syki/syki/Back/Features/Notifications/GetNotifications/GetNotificationsService.cs

#LineLine coverage
 1using Dapper;
 2using System.Text.Json;
 3
 4namespace Syki.Back.Features.Notifications.GetNotifications;
 5
 06public class GetNotificationsService(SykiDbContext ctx) : ISykiService
 7{
 8    public async Task<OneOf<GetNotificationsOut, SykiError>> Get(GetNotificationsIn data)
 9    {
 010        var connection = await ctx.GetOpenConnectionAsync();
 11
 012        var unreadFilter = data.UnreadOnly ? "AND un.viewed_at IS NULL" : "";
 13
 014        var sql = $@"
 015            SELECT
 016                n.id,
 017                n.notification_type,
 018                n.title,
 019                n.description,
 020                n.created_at,
 021                un.viewed_at,
 022                n.metadata::text AS metadata,
 023                COUNT(*) OVER() AS total_rows
 024            FROM
 025                syki.user_notifications un
 026            JOIN
 027                syki.notifications n ON n.id = un.notification_id
 028            WHERE
 029                un.user_id = @UserId
 030                {unreadFilter}
 031            ORDER BY n.created_at DESC
 032            LIMIT @PageSize
 033            OFFSET @Offset
 034        ";
 35
 036        var offset = (data.Page - 1) * data.PageSize;
 037        var parameters = new { UserId = ctx.RequestUser.Id, data.PageSize, Offset = offset };
 38
 039        var rows = (await connection.QueryAsync<NotificationRow>(sql, parameters)).ToList();
 40
 041        var items = rows.Select(r => new GetNotificationsItemOut
 042        {
 043            Id = r.Id,
 044            NotificationType = (NotificationType)r.NotificationType,
 045            Title = r.Title,
 046            Description = r.Description,
 047            CreatedAt = r.CreatedAt,
 048            ViewedAt = r.ViewedAt,
 049            Metadata = r.Metadata != null ? JsonDocument.Parse(r.Metadata) : null,
 050        }).ToList();
 51
 052        return new GetNotificationsOut
 053        {
 054            Total = rows.FirstOrDefault()?.TotalRows ?? 0,
 055            Page = data.Page,
 056            PageSize = data.PageSize,
 057            Items = items,
 058        };
 059    }
 60}