| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev> |
| | | 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 | | using NodaTime; |
| | | 16 | | using Npgsql; |
| | | 17 | | using Riok.Mapperly.Abstractions; |
| | | 18 | | |
| | | 19 | | namespace Api.Works.Mutations; |
| | | 20 | | |
| | | 21 | | [MutationType] |
| | | 22 | | public static partial class AddTagMutations |
| | | 23 | | { |
| | | 24 | | [PermissionCheckAuthorize(UserPermissionBits.TagWrite)] |
| | | 25 | | [Error(typeof(DomainError))] |
| | | 26 | | public static async Task<AddTagPayload> AddTagAsync( |
| | | 27 | | [Service] PGContext db, |
| | | 28 | | [Service] ICurrentUserId userId, |
| | | 29 | | [Service] IEFTransactionDIAccessorService txGetter, |
| | | 30 | | [Service] IValidator<AddTagInput> validator, |
| | | 31 | | AddTagInput input, |
| | | 32 | | CancellationToken ct |
| | | 33 | | ) |
| | | 34 | | { |
| | | 35 | | await validator.ValidateOrThrowInputAsync(input, ct); |
| | | 36 | | |
| | | 37 | | var tx = await txGetter.BeginOrGetTransactionAsync(); |
| | | 38 | | |
| | | 39 | | var tag = AddTagInputMapper.CreateFromDto(input, userId.Id!.Value, Now()); |
| | | 40 | | |
| | | 41 | | await db.AddAsync(tag, cancellationToken: ct); |
| | | 42 | | await db.SaveChangesOrThrowAsync( |
| | | 43 | | PostgresErrorCodes.UniqueViolation, |
| | | 44 | | ErrorCodes.TAG_ALREADY_EXISTS, |
| | | 45 | | "A tag with the same namespace and name already exists", |
| | | 46 | | ct |
| | | 47 | | ); |
| | | 48 | | |
| | | 49 | | await tx.CommitAsync(ct); |
| | | 50 | | return new AddTagPayload(TagMapper.ToDto(tag)); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public record AddTagPayload(Queries.Tag Tag); |
| | | 55 | | |
| | | 56 | | public record AddTagInput |
| | | 57 | | { |
| | | 58 | | public required string[] TagNamespace { get; init; } |
| | | 59 | | |
| | | 60 | | public required string TagName { get; init; } |
| | | 61 | | |
| | | 62 | | public class AddTagInputValidator : AbstractValidator<AddTagInput> |
| | | 63 | | { |
| | | 64 | | public AddTagInputValidator( |
| | | 65 | | PGContext db, |
| | | 66 | | ICurrentUserId userId, |
| | | 67 | | IEFTransactionDIAccessorService tx |
| | | 68 | | ) |
| | | 69 | | { |
| | | 70 | | RuleFor(t => t).BeginTransaction(tx); |
| | | 71 | | |
| | | 72 | | RuleFor(t => t) |
| | | 73 | | .MustAsync( |
| | | 74 | | async (input, ct) => |
| | | 75 | | !await db.WorkTags.AnyAsync( |
| | | 76 | | existing => |
| | | 77 | | existing.OwnerId == userId.Id |
| | | 78 | | && existing.TagNamespace == input.TagNamespace |
| | | 79 | | && existing.TagName == input.TagName, |
| | | 80 | | ct |
| | | 81 | | ) |
| | | 82 | | ) |
| | | 83 | | .WithMessage("A tag with the same namespace and name already exists") |
| | | 84 | | .WithErrorCode(ErrorCodes.TAG_ALREADY_EXISTS); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | [Mapper] |
| | | 90 | | public static partial class AddTagInputMapper |
| | | 91 | | { |
| | | 92 | | [MapperIgnoreTarget(nameof(Models.WorkTag.RowVersion))] |
| | | 93 | | [MapperIgnoreTarget(nameof(Models.WorkTag.WorkTagWorks))] |
| | | 94 | | [MapperIgnoreTarget(nameof(Models.WorkTag.Works))] |
| | | 95 | | [MapperIgnoreTarget(nameof(Models.WorkTag.Owner))] |
| | | 96 | | private static partial Models.WorkTag CreateFromDtoInternal( |
| | | 97 | | AddTagInput w, |
| | | 98 | | Guid id, |
| | | 99 | | Instant metadataAddedAt, |
| | | 100 | | Instant metadataUpdatedAt |
| | | 101 | | ); |
| | | 102 | | |
| | | 103 | | public static Models.WorkTag CreateFromDto(AddTagInput w, Guid ownerId, Instant now) |
| | 1 | 104 | | { |
| | 1 | 105 | | var id = Guid.CreateVersion7().WithPostfix(Models.WorkTag.IdPostfix); |
| | 1 | 106 | | var tag = CreateFromDtoInternal(w, id, now, now); |
| | 1 | 107 | | tag.OwnerId = ownerId; |
| | | 108 | | |
| | 1 | 109 | | return tag; |
| | 1 | 110 | | } |
| | | 111 | | |
| | | 112 | | [UserMapping(Default = true)] |
| | | 113 | | public static ZonedDateTime FromInstantToZonedDateTime(Instant i) => |
| | 0 | 114 | | MapperUtils.FromInstantToZonedDateTime(i); |
| | | 115 | | } |
| | | 116 | | // Mostly Ai Generated - End |