106 lines
3.6 KiB
C#
106 lines
3.6 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using AyaNova.Models;
|
|
using AyaNova.Api.ControllerHelpers;
|
|
using AyaNova.Biz;
|
|
|
|
|
|
namespace AyaNova.Api.Controllers
|
|
{
|
|
|
|
/// <summary>
|
|
/// Search
|
|
/// </summary>
|
|
[ApiController]
|
|
[ApiVersion("8.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
public class SearchController : ControllerBase
|
|
{
|
|
private readonly AyContext ct;
|
|
private readonly ILogger<WidgetController> 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<WidgetController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Post 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), searchParams);
|
|
|
|
return Ok(ApiOkResponse.Response(SearchResults, true));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get search result summary
|
|
/// </summary>
|
|
/// <param name="ayaType"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="phrase"></param>
|
|
/// <param name="max"></param>
|
|
/// <returns>A search result excerpt of object</returns>
|
|
[HttpGet("Info/{ayaType}/{id}")]
|
|
public async Task<IActionResult> GetInfo([FromRoute] AyaType ayaType, [FromRoute] long id, [FromQuery] string phrase, [FromQuery] int max = 80)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
|
|
if (!Authorized.HasReadFullRole(HttpContext.Items, ayaType))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
if (id == 0)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var res = await Search.GetInfoAsync(ct, UserTranslationIdFromContext.Id(HttpContext.Items),
|
|
UserRolesFromContext.Roles(HttpContext.Items), UserIdFromContext.Id(HttpContext.Items), phrase, max, ayaType, id);
|
|
|
|
return Ok(ApiOkResponse.Response(res, true));
|
|
}
|
|
|
|
//------------
|
|
|
|
|
|
}//eoc
|
|
}//eons |