| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Core.Data.Repository; |
| | | 5 | | |
| | | 6 | | public sealed record InteractionChange(bool? Starred, bool? Liked, bool? Hidden); |
| | | 7 | | |
| | | 8 | | public 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> |
| | | 15 | | public interface IAssetInteractionWriter |
| | | 16 | | { |
| | | 17 | | Task<InteractionState?> ApplyAsync(Guid userId, Guid assetId, InteractionChange change, CancellationToken ct); |
| | | 18 | | } |
| | | 19 | | |
| | 24 | 20 | | public 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 |
| | 24 | 25 | | { |
| | | 26 | | try |
| | 24 | 27 | | { |
| | 24 | 28 | | return await TryApplyAsync(userId, assetId, change, ct); |
| | | 29 | | } |
| | 3 | 30 | | catch (DbUpdateException ex) when (ex.IsUniqueViolation(InteractionIndexNames.OnePerUserAndAsset)) |
| | 2 | 31 | | { |
| | 2 | 32 | | db.ChangeTracker.Clear(); |
| | 2 | 33 | | return await TryApplyAsync(userId, assetId, change, ct); |
| | | 34 | | } |
| | 22 | 35 | | } |
| | | 36 | | |
| | | 37 | | private async Task<InteractionState?> TryApplyAsync(Guid userId, Guid assetId, InteractionChange change, Cancellatio |
| | 26 | 38 | | { |
| | 26 | 39 | | var asset = await Query(db, userId, assetId).FirstOrDefaultAsync(ct); |
| | 26 | 40 | | if (asset is null) |
| | 4 | 41 | | return null; |
| | | 42 | | |
| | 22 | 43 | | var interaction = asset.Interaction; |
| | 22 | 44 | | if (interaction is null) |
| | 11 | 45 | | { |
| | 11 | 46 | | if (change is { Starred: null, Liked: null, Hidden: null }) |
| | 1 | 47 | | return new InteractionState(Starred: false, Liked: false, Hidden: false); |
| | | 48 | | |
| | 10 | 49 | | interaction = new AssetInteraction { UserId = userId, AssetId = assetId }; |
| | 10 | 50 | | db.Interactions.Add(interaction); |
| | 10 | 51 | | } |
| | | 52 | | |
| | 21 | 53 | | interaction.IsStarred = change.Starred ?? interaction.IsStarred; |
| | 21 | 54 | | interaction.IsLiked = change.Liked ?? interaction.IsLiked; |
| | 21 | 55 | | interaction.HiddenAt = change.Hidden switch |
| | 21 | 56 | | { |
| | 6 | 57 | | true => Instant.FromDateTimeOffset(time.GetUtcNow()), |
| | 3 | 58 | | false => null, |
| | 12 | 59 | | null => interaction.HiddenAt, |
| | 21 | 60 | | }; |
| | 21 | 61 | | await db.SaveChangesAsync(ct); |
| | 17 | 62 | | return new InteractionState(interaction.IsStarred, interaction.IsLiked, interaction.HiddenAt is not null); |
| | 22 | 63 | | } |
| | | 64 | | |
| | | 65 | | internal static IQueryable<VisibleAsset> Query(AnichronDbContext db, Guid userId, Guid assetId) |
| | 27 | 66 | | => db.MediaAssets.VisibleTo(userId) |
| | 27 | 67 | | .Where(a => a.Id == assetId) |
| | 27 | 68 | | .Select(a => new VisibleAsset(a.Interactions.FirstOrDefault(i => i.UserId == userId))); |
| | | 69 | | } |