| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | using Api.Auth.Handlers; |
| | | 5 | | using Api.Auth.Utils; |
| | | 6 | | using Api.Database; |
| | | 7 | | using Api.Database.Utils; |
| | | 8 | | using Api.Utils; |
| | | 9 | | using Api.Works.Models; |
| | | 10 | | using Api.Works.Queries; |
| | | 11 | | using FluentValidation; |
| | | 12 | | using NodaTime; |
| | | 13 | | using Riok.Mapperly.Abstractions; |
| | | 14 | | using static Api.Utils.ValidatorUtils; |
| | | 15 | | |
| | | 16 | | namespace Api.Works.Mutations; |
| | | 17 | | |
| | | 18 | | [MutationType] |
| | | 19 | | public 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 | | |
| | | 46 | | public record AddWorkPayload(Queries.Work Work); |
| | | 47 | | |
| | | 48 | | public 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; } |
| | | 56 | | public List<Queries.WorkIdentifier> WorkIdentifiers { get; init; } = []; |
| | | 57 | | public required List<Guid> TagIds { get; init; } = []; |
| | | 58 | | public required List<Guid> AuthorIds { get; init; } = []; |
| | | 59 | | |
| | | 60 | | public class AddWorkInputValidator : AbstractValidator<AddWorkInput> |
| | | 61 | | { |
| | 1 | 62 | | public AddWorkInputValidator( |
| | 1 | 63 | | PGContext db, |
| | 1 | 64 | | ICurrentUserId userId, |
| | 1 | 65 | | IEFTransactionDIAccessorService tx |
| | 1 | 66 | | ) |
| | 1 | 67 | | { |
| | 1 | 68 | | RuleFor(w => w).BeginTransaction(tx); |
| | | 69 | | |
| | 1 | 70 | | RuleFor(w => w.TagIds).MustBeDistinct(nameof(TagIds)); |
| | 1 | 71 | | RuleFor(w => w.TagIds).IdsMustExist(db.WorkTags, userId.Id); |
| | | 72 | | |
| | 1 | 73 | | RuleFor(w => w.AuthorIds).MustBeDistinct(nameof(AuthorIds)); |
| | 1 | 74 | | RuleFor(w => w.AuthorIds).IdsMustExist(db.Authors, userId.Id); |
| | 1 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | [Mapper] |
| | | 80 | | public 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 | | } |