| | | 1 | | using RoleRadar.Application.Abstractions; |
| | | 2 | | using RoleRadar.Domain.Common; |
| | | 3 | | using RoleRadar.Domain.Tracking; |
| | | 4 | | |
| | | 5 | | namespace RoleRadar.Application.Tracking.Queries; |
| | | 6 | | |
| | | 7 | | public sealed record GetContactQuery(Guid Id, string? OwnerId = null) : IQuery<ContactDto?>; |
| | | 8 | | |
| | | 9 | | public sealed record ContactDto( |
| | | 10 | | Guid Id, |
| | | 11 | | string Name, |
| | | 12 | | string? Role, |
| | | 13 | | string? CompanyAffiliation, |
| | | 14 | | Guid? CompanyId, |
| | | 15 | | string Relationship, |
| | | 16 | | string? Notes, |
| | | 17 | | bool IsRedacted, |
| | | 18 | | Guid VersionToken, |
| | | 19 | | DateTime CreatedAt, |
| | | 20 | | DateTime UpdatedAt); |
| | | 21 | | |
| | | 22 | | public sealed class GetContactQueryHandler : IQueryHandler<GetContactQuery, ContactDto?> |
| | | 23 | | { |
| | | 24 | | private readonly ITrackingTransactionContext _context; |
| | | 25 | | |
| | 4 | 26 | | public GetContactQueryHandler(ITrackingTransactionContext context) |
| | | 27 | | { |
| | 4 | 28 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 4 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<Result<ContactDto?>> HandleAsync(GetContactQuery query, CancellationToken cancellationToken = defa |
| | | 32 | | { |
| | 4 | 33 | | ArgumentNullException.ThrowIfNull(query); |
| | | 34 | | |
| | 4 | 35 | | if (query.Id == Guid.Empty) |
| | | 36 | | { |
| | 0 | 37 | | return Result<ContactDto?>.Failure(ErrorCode.ValidationError, "Contact ID cannot be empty."); |
| | | 38 | | } |
| | | 39 | | |
| | 4 | 40 | | var ownerId = string.IsNullOrWhiteSpace(query.OwnerId) |
| | 4 | 41 | | ? OwnerId.Default.Value |
| | 4 | 42 | | : new OwnerId(query.OwnerId).Value; |
| | | 43 | | |
| | 4 | 44 | | var result = await _context.Contacts.GetByIdAsync(ownerId, query.Id, cancellationToken); |
| | 4 | 45 | | if (!result.IsSuccess) |
| | | 46 | | { |
| | 0 | 47 | | return Result<ContactDto?>.Failure(result.Error!.Value); |
| | | 48 | | } |
| | | 49 | | |
| | 4 | 50 | | if (result.Value is null) |
| | | 51 | | { |
| | 0 | 52 | | return Result<ContactDto?>.Failure(ErrorCode.NotFound, $"Contact '{query.Id}' not found."); |
| | | 53 | | } |
| | | 54 | | |
| | 4 | 55 | | var contact = result.Value; |
| | 4 | 56 | | return Result<ContactDto?>.Success(new ContactDto( |
| | 4 | 57 | | contact.Id, |
| | 4 | 58 | | contact.Name, |
| | 4 | 59 | | contact.Role, |
| | 4 | 60 | | contact.CompanyAffiliation, |
| | 4 | 61 | | contact.CompanyId, |
| | 4 | 62 | | contact.Relationship.ToSnakeCase(), |
| | 4 | 63 | | contact.Notes, |
| | 4 | 64 | | contact.IsRedacted, |
| | 4 | 65 | | contact.VersionToken, |
| | 4 | 66 | | contact.CreatedAt, |
| | 4 | 67 | | contact.UpdatedAt)); |
| | 4 | 68 | | } |
| | | 69 | | } |