From 48e09021411844decd9b2c1a17d107437f0ae024 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Fri, 9 Dec 2022 13:14:13 +0900 Subject: feat: Implement initial session data --- .../src/Endpoints/Internal/Root/GetSessionRoute.cs | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 code/api/src/Endpoints/Internal/Root/GetSessionRoute.cs (limited to 'code/api/src') diff --git a/code/api/src/Endpoints/Internal/Root/GetSessionRoute.cs b/code/api/src/Endpoints/Internal/Root/GetSessionRoute.cs new file mode 100644 index 0000000..f0e305e --- /dev/null +++ b/code/api/src/Endpoints/Internal/Root/GetSessionRoute.cs @@ -0,0 +1,63 @@ +namespace IOL.GreatOffice.Api.Endpoints.Internal.Root; + +public class GetSessionRoute : RouteBaseSync.WithoutRequest.WithActionResult +{ + private readonly MainAppDatabase _database; + private readonly ILogger _logger; + + public GetSessionRoute(MainAppDatabase database, ILogger logger) { + _database = database; + _logger = logger; + } + + public class SessionResponse + { + public string Username { get; set; } + public string DisplayName { get; set; } + public Guid UserId { get; set; } + public SessionTenant CurrentTenant { get; set; } + public List AvailableTenants { get; set; } + + public class SessionTenant + { + public Guid Id { get; set; } + public string Name { get; set; } + } + } + + [Authorize] + [HttpGet("~/_/session-data")] + public override ActionResult Handle() { + var user = _database.Users.Include(c => c.Tenants).Select(c => new User() { + Id = c.Id, + Username = c.Username, + FirstName = c.FirstName, + LastName = c.LastName, + Tenants = c.Tenants + + }).FirstOrDefault(c => c.Id == LoggedInUser.Id); + if (user == default) { + return NotFound(); + } + + var currentTenant = user.Tenants.FirstOrDefault(c => c.Id == LoggedInUser.TenantId); + if (currentTenant == default) { + _logger.LogInformation("Could not find current tenant ({0}) for user {1}", LoggedInUser.TenantId, LoggedInUser.Id); + return NotFound(); + } + + return Ok(new SessionResponse() { + Username = user.Username, + DisplayName = user.DisplayName(), + UserId = user.Id, + CurrentTenant = new SessionResponse.SessionTenant() { + Id = currentTenant.Id, + Name = currentTenant.Name + }, + AvailableTenants = user.Tenants.Select(c => new SessionResponse.SessionTenant() { + Id = c.Id, + Name = c.Name + }).ToList() + }); + } +} \ No newline at end of file -- cgit v1.3