| | | 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 Microsoft.EntityFrameworkCore; |
| | | 16 | | using NodaTime; |
| | | 17 | | using Npgsql; |
| | | 18 | | |
| | | 19 | | namespace Api.Works.Mutations; |
| | | 20 | | |
| | | 21 | | [MutationType] |
| | | 22 | | public static partial class UpdateTagMutations |
| | | 23 | | { |
| | | 24 | | [PermissionCheckAuthorize(UserPermissionBits.TagRead | UserPermissionBits.TagWrite)] |
| | | 25 | | [Error(typeof(DomainError))] |
| | | 26 | | public static async Task<UpdateTagPayload> UpdateTagAsync( |
| | | 27 | | [Service] PGContext db, |
| | | 28 | | [Service] IEFTransactionDIAccessorService txGetter, |
| | | 29 | | [Service] IValidator<UpdateTagInput> validator, |
| | | 30 | | UpdateTagInput input, |
| | | 31 | | CancellationToken ct |
| | | 32 | | ) |
| | | 33 | | { |
| | | 34 | | await validator.ValidateOrThrowInputAsync(input, ct); |
| | | 35 | | |
| | | 36 | | var tx = await txGetter.BeginOrGetTransactionAsync(); |
| | | 37 | | var tag = await db.WorkTags.SingleAsync( |
| | | 38 | | t => t.Id == input.Id, |
| | | 39 | | cancellationToken: ct |
| | | 40 | | ); |
| | | 41 | | |
| | | 42 | | UpdateTagInput.ApplyToTag(tag, input); |
| | | 43 | | tag.RowVersion += 1; |
| | | 44 | | tag.MetadataUpdatedAt = Now(); |
| | | 45 | | |
| | | 46 | | await db.SaveChangesOrThrowAsync( |
| | | 47 | | PostgresErrorCodes.UniqueViolation, |
| | | 48 | | ErrorCodes.TAG_ALREADY_EXISTS, |
| | | 49 | | "A tag with the same namespace and name already exists", |
| | | 50 | | ct |
| | | 51 | | ); |
| | | 52 | | |
| | | 53 | | await tx.CommitAsync(ct); |
| | | 54 | | return new UpdateTagPayload(TagMapper.ToDto(tag)); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public record UpdateTagPayload(Queries.Tag Tag); |
| | | 59 | | |
| | | 60 | | public record UpdateTagInput : IBasicEntityMetadata |
| | | 61 | | { |
| | 0 | 62 | | public static byte IdPostfix => Models.WorkTag.IdPostfix; |
| | | 63 | | |
| | | 64 | | public Guid Id { get; init; } |
| | | 65 | | public int RowVersion { get; init; } |
| | | 66 | | |
| | | 67 | | public required Optional<string[]> TagNamespace { get; init; } |
| | | 68 | | public required Optional<string> TagName { get; init; } |
| | | 69 | | |
| | | 70 | | public class UpdateTagInputValidator : AbstractValidator<UpdateTagInput> |
| | | 71 | | { |
| | 1 | 72 | | public UpdateTagInputValidator( |
| | 1 | 73 | | PGContext db, |
| | 1 | 74 | | ICurrentUserId userId, |
| | 1 | 75 | | IEFTransactionDIAccessorService tx |
| | 1 | 76 | | ) |
| | 1 | 77 | | { |
| | 1 | 78 | | RuleFor(w => w).BeginTransaction(tx); |
| | | 79 | | |
| | 1 | 80 | | RuleFor(w => w.Id).IdMustExist(db.WorkTags, userId.Id); |
| | | 81 | | |
| | 1 | 82 | | RuleFor(w => w.RowVersion).RowVersionMustMatch(db.WorkTags); |
| | | 83 | | |
| | 1 | 84 | | RuleFor(w => w) |
| | 1 | 85 | | .MustAsync( |
| | 1 | 86 | | async (input, ct) => |
| | 1 | 87 | | { |
| | 1 | 88 | | var existing = await db |
| | 1 | 89 | | .WorkTags.Where(t => |
| | 1 | 90 | | t.OwnerId == userId.Id && t.Id == input.Id |
| | 1 | 91 | | ) |
| | 1 | 92 | | .Select(t => new { t.TagNamespace, t.TagName }) |
| | 1 | 93 | | .FirstOrDefaultAsync(ct); |
| | 1 | 94 | | |
| | 1 | 95 | | if (existing is null) |
| | 1 | 96 | | return true; |
| | 1 | 97 | | |
| | 1 | 98 | | var tagNamespace = input.TagNamespace.HasValue |
| | 1 | 99 | | ? input.TagNamespace.Value ?? [] |
| | 1 | 100 | | : existing.TagNamespace; |
| | 1 | 101 | | var tagName = input.TagName.HasValue |
| | 1 | 102 | | ? input.TagName.Value |
| | 1 | 103 | | : existing.TagName; |
| | 1 | 104 | | |
| | 1 | 105 | | return !await db.WorkTags.AnyAsync( |
| | 1 | 106 | | other => |
| | 1 | 107 | | other.OwnerId == userId.Id |
| | 1 | 108 | | && other.Id != input.Id |
| | 1 | 109 | | && other.TagNamespace == tagNamespace |
| | 1 | 110 | | && other.TagName == tagName, |
| | 1 | 111 | | ct |
| | 1 | 112 | | ); |
| | 1 | 113 | | } |
| | 1 | 114 | | ) |
| | 1 | 115 | | .WithMessage("A tag with the same namespace and name already exists") |
| | 1 | 116 | | .WithErrorCode(ErrorCodes.TAG_ALREADY_EXISTS); |
| | 1 | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | public static void ApplyToTag(Models.WorkTag tag, UpdateTagInput input) |
| | 1 | 121 | | { |
| | 1 | 122 | | if (input.TagNamespace.HasValue) |
| | 1 | 123 | | tag.TagNamespace = input.TagNamespace.Value ?? []; |
| | | 124 | | |
| | 1 | 125 | | if (input.TagName.HasValue) |
| | 1 | 126 | | tag.TagName = input.TagName.Value; |
| | 1 | 127 | | } |
| | | 128 | | } |
| | | 129 | | // Mostly Ai Generated - End |