using HashidsNet; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; 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; [ApiController] [Route("api/[controller]")] [Authorize] public class TenantController : ControllerBase { private readonly ApplicationContext _context; private readonly IHashids _hashids; public TenantController(ApplicationContext context, IHashids hashids) { _context = context; _hashids = hashids; } [HttpGet] public async Task Get(CancellationToken cancellationToken) { var tenantEntities = await _context.Tenants.ToListAsync(cancellationToken: cancellationToken); 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 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); } }