< Summary

Information
Class: Api.Works.Mutations.UpdateWorkPayload
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Works/Mutations/UpdateWorkMutation.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 128
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

/home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Works/Mutations/UpdateWorkMutation.cs

#LineLine coverage
 1// SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5using Api.Auth.Handlers;
 6using Api.Auth.Models;
 7using Api.Auth.Utils;
 8using Api.Database;
 9using Api.Database.Utils;
 10using Api.Utils;
 11using Api.Works.Models;
 12using Api.Works.Queries;
 13using FluentValidation;
 14using Microsoft.EntityFrameworkCore;
 15using NodaTime;
 16
 17namespace Api.Works.Mutations;
 18
 19[MutationType]
 20public static partial class UpdateWorkMutations
 21{
 22    [PermissionCheckAuthorize(UserPermissionBits.WorkRead | UserPermissionBits.WorkWrite)]
 23    [Error(typeof(DomainError))]
 24    public static async Task<UpdateWorkPayload> UpdateWorkAsync(
 25        [Service] PGContext db,
 26        [Service] IEFTransactionDIAccessorService txGetter,
 27        [Service] IValidator<UpdateWorkInput> validator,
 28        UpdateWorkInput input,
 29        CancellationToken ct
 30    )
 31    {
 32        await validator.ValidateOrThrowInputAsync(input, ct);
 33
 34        var tx = await txGetter.BeginOrGetTransactionAsync();
 35        var work = await db
 36            .Works.Include(w => w.WorkTag_Works)
 37            .Include(w => w.Work_Authors)
 38            .SingleAsync(w => w.Id == input.Id, cancellationToken: ct);
 39
 40        UpdateWorkInput.ApplyToWork(work, input);
 41        work.RowVersion += 1;
 42        work.MetadataUpdatedAt = Now();
 43
 44        await db.SaveChangesAsync(cancellationToken: ct);
 45
 46        await tx.CommitAsync(ct);
 47        return new UpdateWorkPayload(WorkMapper.ToDto(work));
 48    }
 49}
 50
 151public record UpdateWorkPayload(Queries.Work Work);
 52
 53public record UpdateWorkInput : IBasicEntityMetadata
 54{
 55    public static byte IdPostfix => Models.Work.IdPostfix;
 56
 57    public Guid Id { get; init; }
 58    public int RowVersion { get; init; }
 59
 60    public Optional<string> Title { get; init; }
 61    public Optional<string?> Description { get; init; }
 62
 63    public Optional<Instant?> WorkPublishedAt { get; init; }
 64    public Optional<Instant?> WorkUpdatedAt { get; init; }
 65    public Optional<List<Queries.WorkIdentifier>?> WorkIdentifiers { get; init; }
 66    public required Optional<List<Guid>?> TagIds { get; init; }
 67    public required Optional<List<Guid>?> AuthorIds { get; init; }
 68
 69    public class UpdateWorkInputValidator : AbstractValidator<UpdateWorkInput>
 70    {
 71        public UpdateWorkInputValidator(
 72            PGContext db,
 73            ICurrentUserId userId,
 74            IEFTransactionDIAccessorService tx
 75        )
 76        {
 77            RuleFor(w => w).BeginTransaction(tx);
 78
 79            RuleFor(w => w.Id).IdMustExist(db.Works, userId.Id);
 80
 81            RuleFor(w => w.RowVersion).RowVersionMustMatch(db.Works);
 82
 83            RuleFor(w => w.TagIds.Value!)
 84                .MustBeDistinct(nameof(TagIds))
 85                .WhenOptionalSet(w => w.TagIds);
 86            RuleFor(w => w.TagIds.Value!)
 87                .IdsMustExist(db.WorkTags, userId.Id)
 88                .WhenOptionalSet(w => w.TagIds);
 89
 90            RuleFor(w => w.AuthorIds.Value!)
 91                .MustBeDistinct(nameof(AuthorIds))
 92                .WhenOptionalSet(w => w.AuthorIds);
 93            RuleFor(w => w.AuthorIds.Value!)
 94                .IdsMustExist(db.Authors, userId.Id)
 95                .WhenOptionalSet(w => w.AuthorIds);
 96        }
 97    }
 98
 99    public static void ApplyToWork(Models.Work work, UpdateWorkInput input)
 100    {
 101        if (input.Title.HasValue)
 102            work.Title = input.Title.Value;
 103
 104        if (input.Description.HasValue)
 105            work.Description = input.Description.Value;
 106
 107        if (input.WorkPublishedAt.HasValue)
 108            work.WorkPublishedAt = input.WorkPublishedAt.Value?.InUtc();
 109
 110        if (input.WorkUpdatedAt.HasValue)
 111            work.WorkUpdatedAt = input.WorkUpdatedAt.Value?.InUtc();
 112
 113        if (input.WorkIdentifiers.HasValue)
 114            work.WorkIdentifiers =
 115                input.WorkIdentifiers.Value?.Select(WorkMapper.FromWorkIdDto).ToList()
 116                ?? [];
 117
 118        if (input.TagIds.HasValue)
 119            work.WorkTag_Works =
 120                input.TagIds.Value?.Select(t => new WorkTag_Work(work.Id, t)).ToList()
 121                ?? [];
 122
 123        if (input.AuthorIds.HasValue)
 124            work.Work_Authors =
 125                input.AuthorIds.Value?.Select(t => new Work_Author(work.Id, t)).ToList()
 126                ?? [];
 127    }
 128}

Methods/Properties

.ctor(Api.Works.Queries.Work)