68 lines
No EOL
2.2 KiB
C#
68 lines
No EOL
2.2 KiB
C#
using Insight.Infrastructure.Entities;
|
|
using Insight.Infrastructure.Models;
|
|
using Insight.Infrastructure.Web;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Insight.Api.Controllers;
|
|
|
|
[ApiController, Route("api/inventory")]
|
|
public class InventoryController(IServiceScopeFactory scopeFactory) : ControllerBase
|
|
{
|
|
private readonly IServiceScopeFactory _scopeFactory = scopeFactory;
|
|
|
|
[HttpGet, Authorize]
|
|
public async Task<IActionResult> Get([FromQuery] HostApplicationEntity request, [FromQuery] PagedDataRequest meta, CancellationToken cancellationToken)
|
|
{
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var collection = scope.ServiceProvider.GetRequiredService<IMongoDatabase>().HostApplication();
|
|
|
|
try
|
|
{
|
|
var filter = Builders<HostApplicationEntity>.Filter.Empty;
|
|
|
|
if (request.Id is not null)
|
|
filter &= Builders<HostApplicationEntity>.Filter.Eq(p => p.Id, request.Id);
|
|
|
|
if (request.Host is not null)
|
|
filter &= Builders<HostApplicationEntity>.Filter.Eq(p => p.Host, request.Host);
|
|
|
|
if (request.Name is not null)
|
|
filter &= Builders<HostApplicationEntity>.Filter.Regex(p => p.Name, new BsonRegularExpression(new Regex(request.Name, RegexOptions.IgnoreCase)));
|
|
|
|
var result = await collection.GetPagedAsync(
|
|
filter: filter,
|
|
offset: meta.Offset,
|
|
limit: meta.Limit,
|
|
request: Request,
|
|
response: Response,
|
|
cancellationToken: cancellationToken).ConfigureAwait(false);
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
return Unauthorized(ex.ToString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
|
|
public class ApplicationRequest
|
|
{
|
|
[JsonPropertyName("id")]
|
|
public string? Id { get; set; }
|
|
|
|
[JsonPropertyName("host")]
|
|
public string? Host { get; set; }
|
|
|
|
[JsonPropertyName("name")]
|
|
public string? Name { get; set; }
|
|
}
|
|
} |