< Summary - Estud

Information
Class: Estud.Back.Features.Notifications.GetNotifications.GetNotificationsService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Notifications/GetNotifications/GetNotificationsService.cs
Tag: 114_29044117136
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 60
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Get()100%44100%

File(s)

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

#LineLine coverage
 1using Dapper;
 2using System.Text.Json;
 3
 4namespace Estud.Back.Features.Notifications.GetNotifications;
 5
 66public class GetNotificationsService(EstudDbContext ctx) : IEstudService
 7{
 8    public async Task<OneOf<GetNotificationsOut, EstudError>> Get(GetNotificationsIn data)
 9    {
 610        var connection = await ctx.GetOpenConnectionAsync();
 11
 612        var unreadFilter = data.UnreadOnly ? "AND un.viewed_at IS NULL" : "";
 13
 614        var sql = $@"
 615            SELECT
 616                n.id,
 617                n.notification_type,
 618                n.title,
 619                n.description,
 620                n.created_at,
 621                un.viewed_at,
 622                n.metadata::text AS metadata,
 623                COUNT(*) OVER() AS total_rows
 624            FROM
 625                estud.user_notifications un
 626            JOIN
 627                estud.notifications n ON n.id = un.notification_id
 628            WHERE
 629                un.user_id = @UserId
 630                {unreadFilter}
 631            ORDER BY n.created_at DESC
 632            LIMIT @PageSize
 633            OFFSET @Offset
 634        ";
 35
 636        var offset = (data.Page - 1) * data.PageSize;
 637        var parameters = new { UserId = ctx.RequestUser.Id, data.PageSize, Offset = offset };
 38
 639        var rows = (await connection.QueryAsync<NotificationRow>(sql, parameters)).ToList();
 40
 841        var items = rows.Select(r => new GetNotificationsItemOut
 842        {
 843            Id = r.Id,
 844            NotificationType = (NotificationType)r.NotificationType,
 845            Title = r.Title,
 846            Description = r.Description,
 847            CreatedAt = r.CreatedAt,
 848            ViewedAt = r.ViewedAt,
 849            Metadata = r.Metadata != null ? JsonDocument.Parse(r.Metadata) : null,
 850        }).ToList();
 51
 652        return new GetNotificationsOut
 653        {
 654            Total = rows.FirstOrDefault()?.TotalRows ?? 0,
 655            Page = data.Page,
 656            PageSize = data.PageSize,
 657            Items = items,
 658        };
 659    }
 60}