145 lines
5.2 KiB
C#
145 lines
5.2 KiB
C#
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.Extensions.Logging;
|
|
using Sockeye.Models;
|
|
using Sockeye.Api.ControllerHelpers;
|
|
using Sockeye.Biz;
|
|
|
|
|
|
namespace Sockeye.Api.Controllers
|
|
{
|
|
|
|
/// <summary>
|
|
/// Search
|
|
/// </summary>
|
|
[ApiController]
|
|
[ApiVersion("8.0")]
|
|
[Route("api/v{version:apiVersion}/search")]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
public class SearchController : ControllerBase
|
|
{
|
|
private readonly AyContext ct;
|
|
private readonly ILogger<SearchController> log;
|
|
private readonly ApiServerState serverState;
|
|
|
|
/// <summary>
|
|
/// ctor
|
|
/// </summary>
|
|
/// <param name="dbcontext"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="apiServerState"></param>
|
|
public SearchController(AyContext dbcontext, ILogger<SearchController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Create search parameters
|
|
/// MaxResults defaults to 500
|
|
/// MaxResults = 0 returns all results
|
|
/// </summary>
|
|
/// <param name="searchParams"></param>
|
|
/// <returns>SearchResult list</returns>
|
|
[HttpPost]
|
|
public async Task<IActionResult> PostSearch([FromBody] Search.SearchRequestParameters searchParams)
|
|
{
|
|
if (!serverState.IsOpen)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
}
|
|
|
|
//Do the search
|
|
var SearchResults = await Search.DoSearchAsync(
|
|
ct,
|
|
UserTranslationIdFromContext.Id(HttpContext.Items),
|
|
UserRolesFromContext.Roles(HttpContext.Items),
|
|
UserIdFromContext.Id(HttpContext.Items),
|
|
searchParams
|
|
);
|
|
|
|
return Ok(ApiOkResponse.Response(SearchResults));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get search result summary
|
|
/// </summary>
|
|
/// <param name="sockType"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="phrase"></param>
|
|
/// <param name="max"></param>
|
|
/// <returns>A search result excerpt of object</returns>
|
|
[HttpGet("info/{sockType}/{id}")]
|
|
public async Task<IActionResult> GetInfo([FromRoute] SockType sockType, [FromRoute] long id, [FromQuery] string phrase, [FromQuery] int max = 80)
|
|
{
|
|
if (!serverState.IsOpen)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
if (!Authorized.HasReadFullRole(HttpContext.Items, sockType))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
if (id == 0)
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "id", "id can't be zero"));
|
|
|
|
|
|
var res = await Search.GetInfoAsync(UserTranslationIdFromContext.Id(HttpContext.Items),
|
|
UserRolesFromContext.Roles(HttpContext.Items), UserIdFromContext.Id(HttpContext.Items), phrase, max, sockType, id, ct);
|
|
|
|
return Ok(ApiOkResponse.Response(res));
|
|
}
|
|
|
|
|
|
|
|
// /// <summary>
|
|
// /// Get the top level ancestor of provided type and id
|
|
// /// (e.g. find the WorkOrder principle for a WorkOrderItemPart object descendant)
|
|
// /// </summary>
|
|
// /// <param name="sockType"></param>
|
|
// /// <param name="id"></param>
|
|
// /// <returns>A type and id of ancestor</returns>
|
|
// [HttpGet("ancestor/{sockType}/{id}")]
|
|
// public async Task<IActionResult> GetAncestor([FromRoute] SockType sockType, [FromRoute] long id)
|
|
// {
|
|
// if (serverState.IsClosed)
|
|
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
// //since this is for opening an entire object it's appropriate to check if they have any role first
|
|
// if (!Authorized.HasAnyRole(HttpContext.Items, sockType))
|
|
// return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
|
|
// if (!ModelState.IsValid)
|
|
// return BadRequest(new ApiErrorResponse(ModelState));
|
|
// if (id == 0)
|
|
// return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, "id can't be zero"));
|
|
|
|
// switch (sockType)
|
|
// {
|
|
|
|
|
|
|
|
// default:
|
|
// return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, "Only types with ancestors are valid"));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
//------------
|
|
|
|
|
|
}//eoc
|
|
}//eons |