< Summary

Information
Class: Anichron.Core.Data.Repository.EfAssetInteractionWriter
Assembly: Anichron.Core
File(s): /home/runner/work/anichron/anichron/src/Anichron.Core/Data/Repository/AssetInteractionWriter.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ApplyAsync()100%11100%
TryApplyAsync()100%2020100%
Query(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Core/Data/Repository/AssetInteractionWriter.cs

#LineLine coverage
 1using Anichron.Core.Domain;
 2using Microsoft.EntityFrameworkCore;
 3
 4namespace Anichron.Core.Data.Repository;
 5
 6public sealed record InteractionChange(bool? Starred, bool? Liked, bool? Hidden);
 7
 8public sealed record InteractionState(bool Starred, bool Liked, bool Hidden);
 9
 10/// <summary>
 11/// Applies a change to the caller's own interaction with an asset they can see; a <c>null</c> flag keeps its
 12/// stored value, a hide stamps the current time even on a hidden asset, and the row is created on the first
 13/// change. Returns <c>null</c> when the asset is not visible to the caller.
 14/// </summary>
 15public interface IAssetInteractionWriter
 16{
 17    Task<InteractionState?> ApplyAsync(Guid userId, Guid assetId, InteractionChange change, CancellationToken ct);
 18}
 19
 2420public sealed class EfAssetInteractionWriter(AnichronDbContext db, TimeProvider time) : IAssetInteractionWriter
 21{
 22    internal sealed record VisibleAsset(AssetInteraction? Interaction);
 23
 24    public async Task<InteractionState?> ApplyAsync(Guid userId, Guid assetId, InteractionChange change, CancellationTok
 2425    {
 26        try
 2427        {
 2428            return await TryApplyAsync(userId, assetId, change, ct);
 29        }
 330        catch (DbUpdateException ex) when (ex.IsUniqueViolation(InteractionIndexNames.OnePerUserAndAsset))
 231        {
 232            db.ChangeTracker.Clear();
 233            return await TryApplyAsync(userId, assetId, change, ct);
 34        }
 2235    }
 36
 37    private async Task<InteractionState?> TryApplyAsync(Guid userId, Guid assetId, InteractionChange change, Cancellatio
 2638    {
 2639        var asset = await Query(db, userId, assetId).FirstOrDefaultAsync(ct);
 2640        if (asset is null)
 441            return null;
 42
 2243        var interaction = asset.Interaction;
 2244        if (interaction is null)
 1145        {
 1146            if (change is { Starred: null, Liked: null, Hidden: null })
 147                return new InteractionState(Starred: false, Liked: false, Hidden: false);
 48
 1049            interaction = new AssetInteraction { UserId = userId, AssetId = assetId };
 1050            db.Interactions.Add(interaction);
 1051        }
 52
 2153        interaction.IsStarred = change.Starred ?? interaction.IsStarred;
 2154        interaction.IsLiked = change.Liked ?? interaction.IsLiked;
 2155        interaction.HiddenAt = change.Hidden switch
 2156        {
 657            true => Instant.FromDateTimeOffset(time.GetUtcNow()),
 358            false => null,
 1259            null => interaction.HiddenAt,
 2160        };
 2161        await db.SaveChangesAsync(ct);
 1762        return new InteractionState(interaction.IsStarred, interaction.IsLiked, interaction.HiddenAt is not null);
 2263    }
 64
 65    internal static IQueryable<VisibleAsset> Query(AnichronDbContext db, Guid userId, Guid assetId)
 2766        => db.MediaAssets.VisibleTo(userId)
 2767            .Where(a => a.Id == assetId)
 2768            .Select(a => new VisibleAsset(a.Interactions.FirstOrDefault(i => i.UserId == userId)));
 69}