< Summary

Information
Class: Api.Works.Mutations.AddWorkInput
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Works/Mutations/AddWorkMutation.cs
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 107
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%
.ctor(...)100%11100%

File(s)

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

#LineLine coverage
 1// SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4using Api.Auth.Handlers;
 5using Api.Auth.Utils;
 6using Api.Database;
 7using Api.Database.Utils;
 8using Api.Utils;
 9using Api.Works.Models;
 10using Api.Works.Queries;
 11using FluentValidation;
 12using NodaTime;
 13using Riok.Mapperly.Abstractions;
 14using static Api.Utils.ValidatorUtils;
 15
 16namespace Api.Works.Mutations;
 17
 18[MutationType]
 19public static partial class AddWorkMutations
 20{
 21    [PermissionCheckAuthorize(Auth.Models.UserPermissionBits.WorkWrite)]
 22    [Error(typeof(DomainError))]
 23    public static async Task<AddWorkPayload> AddWorkAsync(
 24        [Service] PGContext db,
 25        [Service] ICurrentUserId userId,
 26        [Service] IEFTransactionDIAccessorService txGetter,
 27        [Service] IValidator<AddWorkInput> validator,
 28        AddWorkInput input,
 29        CancellationToken ct
 30    )
 31    {
 32        await validator.ValidateOrThrowInputAsync(input, ct);
 33
 34        var tx = await txGetter.BeginOrGetTransactionAsync();
 35
 36        var work = AddWorkInputMapper.CreateFromDto(input, userId.Id!.Value, Now());
 37
 38        await db.AddAsync(work, cancellationToken: ct);
 39        await db.SaveChangesAsync(cancellationToken: ct);
 40
 41        await tx.CommitAsync(ct);
 42        return new AddWorkPayload(WorkMapper.ToDto(work));
 43    }
 44}
 45
 46public record AddWorkPayload(Queries.Work Work);
 47
 48public record AddWorkInput
 49{
 50    public required string Title { get; init; }
 51
 52    public string? Description { get; init; }
 53
 54    public Instant? WorkPublishedAt { get; init; }
 55    public Instant? WorkUpdatedAt { get; init; }
 156    public List<Queries.WorkIdentifier> WorkIdentifiers { get; init; } = [];
 157    public required List<Guid> TagIds { get; init; } = [];
 158    public required List<Guid> AuthorIds { get; init; } = [];
 59
 60    public class AddWorkInputValidator : AbstractValidator<AddWorkInput>
 61    {
 162        public AddWorkInputValidator(
 163            PGContext db,
 164            ICurrentUserId userId,
 165            IEFTransactionDIAccessorService tx
 166        )
 167        {
 168            RuleFor(w => w).BeginTransaction(tx);
 69
 170            RuleFor(w => w.TagIds).MustBeDistinct(nameof(TagIds));
 171            RuleFor(w => w.TagIds).IdsMustExist(db.WorkTags, userId.Id);
 72
 173            RuleFor(w => w.AuthorIds).MustBeDistinct(nameof(AuthorIds));
 174            RuleFor(w => w.AuthorIds).IdsMustExist(db.Authors, userId.Id);
 175        }
 76    }
 77}
 78
 79[Mapper]
 80public static partial class AddWorkInputMapper
 81{
 82    [MapperIgnoreTarget(nameof(Models.Work.RowVersion))]
 83    [MapperIgnoreSource(nameof(AddWorkInput.TagIds))]
 84    [MapperIgnoreSource(nameof(AddWorkInput.AuthorIds))]
 85    private static partial Models.Work CreateFromDtoInternal(
 86        AddWorkInput w,
 87        Guid id,
 88        Instant metadataAddedAt,
 89        Instant metadataUpdatedAt
 90    );
 91
 92    public static Models.Work CreateFromDto(AddWorkInput w, Guid ownerId, Instant now)
 93    {
 94        var id = Guid.CreateVersion7().WithPostfix(Models.Work.IdPostfix);
 95        var work = CreateFromDtoInternal(w, id, now, now);
 96        work.OwnerId = ownerId;
 97
 98        work.WorkTag_Works = [.. w.TagIds.Select(tId => new WorkTag_Work(id, tId))];
 99        work.Work_Authors = [.. w.AuthorIds.Select(aId => new Work_Author(id, aId))];
 100
 101        return work;
 102    }
 103
 104    [UserMapping(Default = true)]
 105    public static ZonedDateTime FromInstantToZonedDateTime(Instant i) =>
 106        MapperUtils.FromInstantToZonedDateTime(i);
 107}