Files
laatzen/LaaProductionWeb/LaaProduction.Web/API/Search/WildcardController.cs
T

81 lines
2.3 KiB
C#

namespace LaaProduction.Web.API.Search
{
using LaaProduction.Search;
using LaaProduction.Web.App_Infrastructure;
using LaaProductionDI;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Http;
using System.Web.Http;
[AuthorizeBearer]
[RoutePrefix("api/Search/Wildcard")]
public class WildcardController : ApiController
{
private readonly ISearchManager searchManager;
public WildcardController()
=> this.searchManager = LaaServiceProvider.GetService<ISearchManager>();
[HttpGet]
public IHttpActionResult Get()
{
var fields = this.searchManager.WildcardFields();
return this.Json(fields);
}
[HttpPost]
public IHttpActionResult Post([FromBody] WildcardSearchModel model)
{
IEnumerable<IDictionary<string, object>> results = Array.Empty<IDictionary<string, object>>();
if (this.ModelState.IsValid)
{
results = this.searchManager.SearchWildcard(model.Token, model.SelectedFields);
}
else
{
return this.ResponseMessage(new HttpResponseMessage
{
Content = new StringContent(this.ModelState.Content()),
RequestMessage = this.Request,
StatusCode = HttpStatusCode.BadRequest,
Version = this.Request.Version
});
}
return this.Json(results);
}
}
public class WildcardSearchModel
{
[Required]
[StringLength(50, MinimumLength = 3)]
[RegularExpression("^[0-9 a-z-A-Z]+$")]
public string Token { get; set; }
public string Fields { get; set; }
internal IEnumerable<string> SelectedFields
{
get
{
if (string.IsNullOrWhiteSpace(this.Fields))
{
return Array.Empty<string>();
}
return WebUtility
.UrlDecode(this.Fields)
?.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
?? Array.Empty<string>();
}
}
}
}