Add TenantGridViewDto and update TenantController to return DTOs; implement hashid encoding for tenant IDs
This commit is contained in:
parent
3d4f86c6a0
commit
bfbe661ee8
@ -1,10 +1,10 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
using HashidsNet;
|
using HashidsNet;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Suspectus.Gandalf.Core.Abstractions.DTOs;
|
|
||||||
using Suspectus.Gandalf.Palantir.Data.Database;
|
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;
|
namespace Suspectus.Gandalf.Palantir.Api.Controllers;
|
||||||
|
|
||||||
@ -26,6 +26,41 @@ public class TenantController : ControllerBase
|
|||||||
public async Task<IActionResult> Get(CancellationToken cancellationToken)
|
public async Task<IActionResult> Get(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var tenantEntities = await _context.Tenants.ToListAsync(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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; }
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user