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
{
///
/// Search
///
[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 log;
private readonly ApiServerState serverState;
///
/// ctor
///
///
///
///
public SearchController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
///
/// Create search parameters
/// MaxResults defaults to 500
/// MaxResults = 0 returns all results
///
///
/// SearchResult list
[HttpPost]
public async Task 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));
}
///
/// Get search result summary
///
///
///
///
///
/// A search result excerpt of object
[HttpGet("info/{sockType}/{id}")]
public async Task 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));
}
// ///
// /// Get the top level ancestor of provided type and id
// /// (e.g. find the WorkOrder principle for a WorkOrderItemPart object descendant)
// ///
// ///
// ///
// /// A type and id of ancestor
// [HttpGet("ancestor/{sockType}/{id}")]
// public async Task 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