Add TenantGridViewDto and update TenantController to return DTOs; implement hashid encoding for tenant IDs

This commit is contained in:
Christian Werner 2025-10-22 02:56:43 +02:00
parent 3d4f86c6a0
commit bfbe661ee8
2 changed files with 50 additions and 3 deletions

View File

@ -1,10 +1,10 @@
using System.ComponentModel.DataAnnotations;
using HashidsNet;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Suspectus.Gandalf.Core.Abstractions.DTOs;
using Suspectus.Gandalf.Palantir.Data.Database;
using Suspectus.Gandalf.Palantir.Data.Dto.Tenant;
using Suspectus.Gandalf.Palantir.Data.Entities.Base;
namespace Suspectus.Gandalf.Palantir.Api.Controllers;
@ -26,6 +26,41 @@ public class TenantController : ControllerBase
public async Task<IActionResult> Get(CancellationToken cancellationToken)
{
var tenantEntities = await _context.Tenants.ToListAsync(cancellationToken: cancellationToken);
return Ok(tenantEntities);
var dtos = tenantEntities.Select(x => new TenantGridViewDto
{
Id = _hashids.EncodeLong(x.Id!.Value),
Name = x.Name,
IsMaster = x.IsMaster,
OwnerId = _hashids.EncodeLong(x.OwnerId),
Visibility = x.Visibility
});
return Ok(dtos);
}
[HttpGet("{idHash}")]
public async Task<IActionResult> Get(CancellationToken cancellationToken, string idHash)
{
if (!_hashids.TryDecodeSingleLong(idHash, out var id))
{
return BadRequest();
}
var tenant = await _context.Tenants.SingleOrDefaultAsync(x => x.Id!.Value == id, cancellationToken: cancellationToken);
if (tenant is null)
{
return NotFound();
}
var dto = new TenantGridViewDto
{
Id = _hashids.EncodeLong(tenant.Id!.Value),
Name = tenant.Name,
IsMaster = tenant.IsMaster,
OwnerId = _hashids.EncodeLong(tenant.OwnerId),
Visibility = tenant.Visibility
};
return Ok(dto);
}
}

View File

@ -0,0 +1,12 @@
using Suspectus.Gandalf.Palantir.Data.Entities.Base;
namespace Suspectus.Gandalf.Palantir.Data.Dto.Tenant;
public class TenantGridViewDto
{
public required string Id { get; set; }
public required string Name { get; set; }
public required bool IsMaster { get; set; }
public required string OwnerId { get; set; }
public required EntityVisibility Visibility { get; set; }
}