Skip to content

Commit

Permalink
Fix organization admin (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fosol authored May 31, 2024
1 parent 30bc2ba commit e21a41d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 27 deletions.
6 changes: 5 additions & 1 deletion src/api/Areas/Admin/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using HSB.Keycloak;
using HSB.Core.Exceptions;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Caching.Memory;

namespace HSB.API.Areas.SystemAdmin.Controllers;

Expand All @@ -34,7 +35,9 @@ public class OrganizationController : ControllerBase
/// </summary>
/// <param name="service"></param>
/// <param name="logger"></param>
public OrganizationController(IOrganizationService service, ILogger<OrganizationController> logger)
public OrganizationController(
IOrganizationService service,
ILogger<OrganizationController> logger)
{
_service = service;
_logger = logger;
Expand Down Expand Up @@ -96,6 +99,7 @@ public IActionResult Add(OrganizationModel model)

var result = _service.FindForId(model.Id, true);
if (result == null) return new BadRequestObjectResult(new ErrorResponseModel("Organization does not exist"));

return CreatedAtAction(nameof(GetForId), new { id = result.Id }, new OrganizationModel(result, true));
}

Expand Down
26 changes: 0 additions & 26 deletions src/api/Areas/Dashboard/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,21 @@ public class OrganizationController : ControllerBase
private readonly ILogger _logger;
private readonly IOrganizationService _service;
private readonly IAuthorizationHelper _authorization;
private readonly IMemoryCache _memoryCache;
private const string HSB_CACHE_KEY = "organizations-hsb";
private const string HSB_LIST_CACHE_KEY = "organizationsList-hsb";
#endregion

#region Constructors
/// <summary>
/// Creates a new instance of a OrganizationController.
/// </summary>
/// <param name="service"></param>
/// <param name="memoryCache"></param>
/// <param name="authorization"></param>
/// <param name="logger"></param>
public OrganizationController(
IOrganizationService service,
IMemoryCache memoryCache,
IAuthorizationHelper authorization,
ILogger<OrganizationController> logger)
{
_service = service;
_memoryCache = memoryCache;
_authorization = authorization;
_logger = logger;
}
Expand All @@ -67,7 +61,6 @@ public OrganizationController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OrganizationModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Organization" })]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand All @@ -77,17 +70,8 @@ public IActionResult Find()
var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
if (_memoryCache.TryGetValue(HSB_CACHE_KEY, out IEnumerable<OrganizationModel>? cachedItems))
{
return new JsonResult(cachedItems);
}
var result = _service.Find(filter);
var model = result.Select(ci => new OrganizationModel(ci, true));
var cacheOptions = new MemoryCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60)
};
_memoryCache.Set(HSB_CACHE_KEY, model, cacheOptions);
return new JsonResult(model);
}
else
Expand All @@ -110,7 +94,6 @@ public IActionResult Find()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OrganizationListModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Organization" })]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult FindList()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand All @@ -120,16 +103,7 @@ public IActionResult FindList()
var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
if (_memoryCache.TryGetValue(HSB_LIST_CACHE_KEY, out IEnumerable<OrganizationListModel>? cachedItems))
{
return new JsonResult(cachedItems);
}
var result = _service.FindList(filter);
var cacheOptions = new MemoryCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60)
};
_memoryCache.Set(HSB_LIST_CACHE_KEY, result, cacheOptions);
return new JsonResult(result);
}
else
Expand Down

0 comments on commit e21a41d

Please sign in to comment.