Skip to content

Commit

Permalink
HOSTSD-185 Add API endpoints (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fosol authored Dec 21, 2023
1 parent eb2986d commit bc7d0ed
Show file tree
Hide file tree
Showing 57 changed files with 1,262 additions and 106 deletions.
63 changes: 50 additions & 13 deletions src/api/Areas/Dashboard/Controllers/ConfigurationItemController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Net.Mime;
using Swashbuckle.AspNetCore.Annotations;

using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;

using HSB.Core.Models;
using HSB.DAL.Services;
using HSB.Keycloak;
using HSB.Keycloak.Extensions;
using HSB.Models;

using Swashbuckle.AspNetCore.Annotations;

namespace HSB.API.Areas.Hsb.Controllers;

/// <summary>
Expand All @@ -25,6 +29,7 @@ public class ConfigurationItemController : ControllerBase
#region Variables
private readonly ILogger _logger;
private readonly IConfigurationItemService _service;
private readonly IAuthorizationHelper _authorization;
private readonly IXlsExporter _exporter;
#endregion

Expand All @@ -33,11 +38,17 @@ public class ConfigurationItemController : ControllerBase
/// Creates a new instance of a ConfigurationItemController.
/// </summary>
/// <param name="service"></param>
/// <param name="authorization"></param>
/// <param name="exporter"></param>
/// <param name="logger"></param>
public ConfigurationItemController(IConfigurationItemService service, IXlsExporter exporter, ILogger<ConfigurationItemController> logger)
public ConfigurationItemController(
IConfigurationItemService service,
IAuthorizationHelper authorization,
IXlsExporter exporter,
ILogger<ConfigurationItemController> logger)
{
_service = service;
_authorization = authorization;
_exporter = exporter;
_logger = logger;
}
Expand All @@ -52,14 +63,28 @@ public ConfigurationItemController(IConfigurationItemService service, IXlsExport
[HttpGet(Name = "GetConfigurationItems-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<ConfigurationItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "ConfigurationItem" })]
[SwaggerOperation(Tags = new[] { "Configuration Item" })]
public IActionResult Get()
{
var uri = new Uri(this.Request.GetDisplayUrl());
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
var filter = new HSB.Models.Filters.ConfigurationItemFilter(query);
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new ConfigurationItemModel(ci)));

var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new ConfigurationItemModel(ci)));
}
else
{
// Only return server items this user has access to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

var result = _service.FindForUser(user.Id, filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new ConfigurationItemModel(ci)));
}
}

// TODO: Limit based on role and tenant.
Expand All @@ -72,14 +97,26 @@ public IActionResult Get()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(ConfigurationItemModel), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
[SwaggerOperation(Tags = new[] { "ConfigurationItem" })]
[SwaggerOperation(Tags = new[] { "Configuration Item" })]
public IActionResult GetForId(int id)
{
var entity = _service.FindForId(id);

if (entity == null) return new NoContentResult();
var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
var entity = _service.FindForId(id);
if (entity == null) return new NoContentResult();
return new JsonResult(new ConfigurationItemModel(entity));
}
else
{
// Only return tenants this user belongs to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

return new JsonResult(new ConfigurationItemModel(entity));
var entity = _service.FindForUser(user.Id, (t) => t.Id == id).FirstOrDefault();
if (entity == null) return Forbid();
return new JsonResult(new ConfigurationItemModel(entity));
}
}

// TODO: Complete functionality
Expand All @@ -95,7 +132,7 @@ public IActionResult GetForId(int id)
[Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "ConfigurationItem" })]
[SwaggerOperation(Tags = new[] { "Configuration Item" })]
public IActionResult Export(string format, string name = "service-now")
{
if (format == "excel")
Expand Down
65 changes: 51 additions & 14 deletions src/api/Areas/Dashboard/Controllers/FileSystemItemController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System.Net;
using System.Net.Mime;
using HSB.Models;

using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

using HSB.Core.Models;
using System.Net;
using HSB.DAL.Services;
using HSB.Keycloak;
using Microsoft.AspNetCore.Http.Extensions;
using HSB.Keycloak.Extensions;
using HSB.Models;

using Swashbuckle.AspNetCore.Annotations;

namespace HSB.API.Areas.Hsb.Controllers;

Expand All @@ -25,6 +29,7 @@ public class FileSystemItemController : ControllerBase
#region Variables
private readonly ILogger _logger;
private readonly IFileSystemItemService _service;
private readonly IAuthorizationHelper _authorization;
private readonly IXlsExporter _exporter;
#endregion

Expand All @@ -33,11 +38,17 @@ public class FileSystemItemController : ControllerBase
/// Creates a new instance of a FileSystemItemController.
/// </summary>
/// <param name="service"></param>
/// <param name="authorization"></param>
/// <param name="exporter"></param>
/// <param name="logger"></param>
public FileSystemItemController(IFileSystemItemService service, IXlsExporter exporter, ILogger<FileSystemItemController> logger)
public FileSystemItemController(
IFileSystemItemService service,
IAuthorizationHelper authorization,
IXlsExporter exporter,
ILogger<FileSystemItemController> logger)
{
_service = service;
_authorization = authorization;
_exporter = exporter;
_logger = logger;
}
Expand All @@ -52,14 +63,28 @@ public FileSystemItemController(IFileSystemItemService service, IXlsExporter exp
[HttpGet(Name = "GetFileSystemItems-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<FileSystemItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "FileSystemItem" })]
[SwaggerOperation(Tags = new[] { "File System Item" })]
public IActionResult Get()
{
var uri = new Uri(this.Request.GetDisplayUrl());
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
var filter = new HSB.Models.Filters.FileSystemItemFilter(query);
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new FileSystemItemModel(ci)));

var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(fsi => new FileSystemItemModel(fsi)));
}
else
{
// Only return server items this user has access to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

var result = _service.FindForUser(user.Id, filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(fsi => new FileSystemItemModel(fsi)));
}
}

// TODO: Limit based on role and tenant.
Expand All @@ -72,14 +97,26 @@ public IActionResult Get()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(FileSystemItemModel), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
[SwaggerOperation(Tags = new[] { "FileSystemItem" })]
[SwaggerOperation(Tags = new[] { "File System Item" })]
public IActionResult GetForId(int id)
{
var tenant = _service.FindForId(id);

if (tenant == null) return new NoContentResult();
var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
var entity = _service.FindForId(id);
if (entity == null) return new NoContentResult();
return new JsonResult(new FileSystemItemModel(entity));
}
else
{
// Only return tenants this user belongs to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

return new JsonResult(new FileSystemItemModel(tenant));
var entity = _service.FindForUser(user.Id, (t) => t.Id == id).FirstOrDefault();
if (entity == null) return Forbid();
return new JsonResult(new FileSystemItemModel(entity));
}
}

// TODO: Complete functionality
Expand All @@ -95,7 +132,7 @@ public IActionResult GetForId(int id)
[Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "ConfigurationItem" })]
[SwaggerOperation(Tags = new[] { "File System Item" })]
public IActionResult Export(string format, string name = "service-now")
{
if (format == "excel")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System.Net;
using System.Net.Mime;
using HSB.Models;

using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

using HSB.Core.Models;
using System.Net;
using HSB.DAL.Services;
using HSB.Keycloak;
using Microsoft.AspNetCore.Http.Extensions;
using HSB.Keycloak.Extensions;
using HSB.Models;

using Swashbuckle.AspNetCore.Annotations;

namespace HSB.API.Areas.Hsb.Controllers;

Expand All @@ -25,6 +29,7 @@ public class OperatingSystemItemController : ControllerBase
#region Variables
private readonly ILogger _logger;
private readonly IOperatingSystemItemService _service;
private readonly IAuthorizationHelper _authorization;
private readonly IXlsExporter _exporter;
#endregion

Expand All @@ -33,11 +38,17 @@ public class OperatingSystemItemController : ControllerBase
/// Creates a new instance of a OperatingSystemItemController.
/// </summary>
/// <param name="service"></param>
/// <param name="authorization"></param>
/// <param name="exporter"></param>
/// <param name="logger"></param>
public OperatingSystemItemController(IOperatingSystemItemService service, IXlsExporter exporter, ILogger<OperatingSystemItemController> logger)
public OperatingSystemItemController(
IOperatingSystemItemService service,
IAuthorizationHelper authorization,
IXlsExporter exporter,
ILogger<OperatingSystemItemController> logger)
{
_service = service;
_authorization = authorization;
_exporter = exporter;
_logger = logger;
}
Expand All @@ -52,14 +63,28 @@ public OperatingSystemItemController(IOperatingSystemItemService service, IXlsEx
[HttpGet(Name = "GetOperatingSystemItems-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OperatingSystemItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "OperatingSystemItem" })]
[SwaggerOperation(Tags = new[] { "Operating System Item" })]
public IActionResult Get()
{
var uri = new Uri(this.Request.GetDisplayUrl());
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
var filter = new HSB.Models.Filters.OperatingSystemItemFilter(query);
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new OperatingSystemItemModel(ci)));

var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(fsi => new OperatingSystemItemModel(fsi)));
}
else
{
// Only return server items this user has access to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

var result = _service.FindForUser(user.Id, filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(fsi => new OperatingSystemItemModel(fsi)));
}
}

// TODO: Limit based on role and tenant.
Expand All @@ -72,14 +97,26 @@ public IActionResult Get()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(OperatingSystemItemModel), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
[SwaggerOperation(Tags = new[] { "OperatingSystemItem" })]
[SwaggerOperation(Tags = new[] { "Operating System Item" })]
public IActionResult GetForId(int id)
{
var tenant = _service.FindForId(id);

if (tenant == null) return new NoContentResult();
var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
var entity = _service.FindForId(id);
if (entity == null) return new NoContentResult();
return new JsonResult(new OperatingSystemItemModel(entity));
}
else
{
// Only return tenants this user belongs to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

return new JsonResult(new OperatingSystemItemModel(tenant));
var entity = _service.FindForUser(user.Id, (t) => t.Id == id).FirstOrDefault();
if (entity == null) return Forbid();
return new JsonResult(new OperatingSystemItemModel(entity));
}
}

// TODO: Complete functionality
Expand All @@ -95,7 +132,7 @@ public IActionResult GetForId(int id)
[Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "ConfigurationItem" })]
[SwaggerOperation(Tags = new[] { "Operating System Item" })]
public IActionResult Export(string format, string name = "service-now")
{
if (format == "excel")
Expand Down
Loading

0 comments on commit bc7d0ed

Please sign in to comment.