| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | // Mostly Ai Generated - Start |
| | | 6 | | using Api.Auth.Handlers; |
| | | 7 | | using Api.Auth.Models; |
| | | 8 | | using Api.Auth.Utils; |
| | | 9 | | using Api.Database; |
| | | 10 | | using Api.Database.Utils; |
| | | 11 | | using Api.Utils; |
| | | 12 | | using Api.Works.Models; |
| | | 13 | | using Api.Works.Queries; |
| | | 14 | | using FluentValidation; |
| | | 15 | | using NodaTime; |
| | | 16 | | using Riok.Mapperly.Abstractions; |
| | | 17 | | |
| | | 18 | | namespace Api.Works.Mutations; |
| | | 19 | | |
| | | 20 | | [MutationType] |
| | | 21 | | public static partial class AddAuthorMutations |
| | | 22 | | { |
| | | 23 | | [PermissionCheckAuthorize(UserPermissionBits.AuthorWrite)] |
| | | 24 | | public static async Task<AddAuthorPayload> AddAuthorAsync( |
| | | 25 | | [Service] PGContext db, |
| | | 26 | | [Service] ICurrentUserId userId, |
| | | 27 | | [Service] IEFTransactionDIAccessorService txGetter, |
| | | 28 | | AddAuthorInput input, |
| | | 29 | | CancellationToken ct |
| | | 30 | | ) |
| | | 31 | | { |
| | | 32 | | var tx = await txGetter.BeginOrGetTransactionAsync(); |
| | | 33 | | |
| | | 34 | | var author = AddAuthorInputMapper.CreateFromDto(input, userId.Id!.Value, Now()); |
| | | 35 | | |
| | | 36 | | await db.AddAsync(author, cancellationToken: ct); |
| | | 37 | | await db.SaveChangesAsync(cancellationToken: ct); |
| | | 38 | | |
| | | 39 | | await tx.CommitAsync(ct); |
| | | 40 | | return new AddAuthorPayload(AuthorMapper.ToDto(author)); |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public record AddAuthorPayload(Queries.Author Author); |
| | | 45 | | |
| | | 46 | | public record AddAuthorInput |
| | | 47 | | { |
| | | 48 | | public string? FirstName { get; init; } |
| | | 49 | | |
| | | 50 | | public string? LastName { get; init; } |
| | | 51 | | |
| | | 52 | | public required string DisplayName { get; init; } |
| | | 53 | | |
| | | 54 | | public required List<string> PenNames { get; init; } = []; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | [Mapper] |
| | | 58 | | public static partial class AddAuthorInputMapper |
| | | 59 | | { |
| | | 60 | | [MapperIgnoreTarget(nameof(Models.Author.RowVersion))] |
| | | 61 | | private static partial Models.Author CreateFromDtoInternal( |
| | | 62 | | AddAuthorInput w, |
| | | 63 | | Guid id, |
| | | 64 | | Instant metadataAddedAt, |
| | | 65 | | Instant metadataUpdatedAt |
| | | 66 | | ); |
| | | 67 | | |
| | | 68 | | public static Models.Author CreateFromDto(AddAuthorInput w, Guid ownerId, Instant now) |
| | 1 | 69 | | { |
| | 1 | 70 | | var id = Guid.CreateVersion7().WithPostfix(Models.Author.IdPostfix); |
| | 1 | 71 | | var author = CreateFromDtoInternal(w, id, now, now); |
| | 1 | 72 | | author.OwnerId = ownerId; |
| | | 73 | | |
| | 1 | 74 | | return author; |
| | 1 | 75 | | } |
| | | 76 | | |
| | | 77 | | [UserMapping(Default = true)] |
| | | 78 | | public static ZonedDateTime FromInstantToZonedDateTime(Instant i) => |
| | 0 | 79 | | MapperUtils.FromInstantToZonedDateTime(i); |
| | | 80 | | } |
| | | 81 | | // Mostly Ai Generated - End |