| | | 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.Queries; |
| | | 13 | | using FluentValidation; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | |
| | | 16 | | namespace Api.Works.Mutations; |
| | | 17 | | |
| | | 18 | | [MutationType] |
| | | 19 | | public static partial class UpdateAuthorMutations |
| | | 20 | | { |
| | | 21 | | [PermissionCheckAuthorize( |
| | | 22 | | UserPermissionBits.AuthorRead | UserPermissionBits.AuthorWrite |
| | | 23 | | )] |
| | | 24 | | [Error(typeof(DomainError))] |
| | | 25 | | public static async Task<UpdateAuthorPayload> UpdateAuthorAsync( |
| | | 26 | | [Service] PGContext db, |
| | | 27 | | [Service] IEFTransactionDIAccessorService txGetter, |
| | | 28 | | [Service] IValidator<UpdateAuthorInput> validator, |
| | | 29 | | UpdateAuthorInput input, |
| | | 30 | | CancellationToken ct |
| | | 31 | | ) |
| | | 32 | | { |
| | | 33 | | await validator.ValidateOrThrowInputAsync(input, ct); |
| | | 34 | | |
| | | 35 | | var tx = await txGetter.BeginOrGetTransactionAsync(); |
| | | 36 | | var author = await db.Authors.SingleAsync( |
| | | 37 | | a => a.Id == input.Id, |
| | | 38 | | cancellationToken: ct |
| | | 39 | | ); |
| | | 40 | | |
| | | 41 | | UpdateAuthorInput.ApplyToAuthor(author, input); |
| | | 42 | | author.RowVersion += 1; |
| | | 43 | | author.MetadataUpdatedAt = Now(); |
| | | 44 | | |
| | | 45 | | await db.SaveChangesAsync(cancellationToken: ct); |
| | | 46 | | |
| | | 47 | | await tx.CommitAsync(ct); |
| | | 48 | | return new UpdateAuthorPayload(AuthorMapper.ToDto(author)); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public record UpdateAuthorPayload(Queries.Author Author); |
| | | 53 | | |
| | | 54 | | public record UpdateAuthorInput : IBasicEntityMetadata |
| | | 55 | | { |
| | | 56 | | public static byte IdPostfix => Models.Author.IdPostfix; |
| | | 57 | | |
| | | 58 | | public Guid Id { get; init; } |
| | | 59 | | public int RowVersion { get; init; } |
| | | 60 | | |
| | | 61 | | public Optional<string?> FirstName { get; init; } |
| | | 62 | | public Optional<string?> LastName { get; init; } |
| | | 63 | | public required Optional<string> DisplayName { get; init; } |
| | | 64 | | public required Optional<List<string>?> PenNames { get; init; } |
| | | 65 | | |
| | | 66 | | public class UpdateAuthorInputValidator : AbstractValidator<UpdateAuthorInput> |
| | | 67 | | { |
| | 1 | 68 | | public UpdateAuthorInputValidator( |
| | 1 | 69 | | PGContext db, |
| | 1 | 70 | | ICurrentUserId userId, |
| | 1 | 71 | | IEFTransactionDIAccessorService tx |
| | 1 | 72 | | ) |
| | 1 | 73 | | { |
| | 1 | 74 | | RuleFor(a => a).BeginTransaction(tx); |
| | | 75 | | |
| | 1 | 76 | | RuleFor(w => w.Id).IdMustExist(db.Authors, userId.Id); |
| | | 77 | | |
| | 1 | 78 | | RuleFor(w => w.RowVersion).RowVersionMustMatch(db.Authors); |
| | 1 | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public static void ApplyToAuthor(Models.Author author, UpdateAuthorInput input) |
| | | 83 | | { |
| | | 84 | | if (input.FirstName.HasValue) |
| | | 85 | | author.FirstName = input.FirstName.Value; |
| | | 86 | | |
| | | 87 | | if (input.LastName.HasValue) |
| | | 88 | | author.LastName = input.LastName.Value; |
| | | 89 | | |
| | | 90 | | if (input.DisplayName.HasValue) |
| | | 91 | | author.DisplayName = input.DisplayName.Value; |
| | | 92 | | |
| | | 93 | | if (input.PenNames.HasValue) |
| | | 94 | | author.PenNames = input.PenNames.Value ?? []; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | // Mostly Ai Generated - End |