laatzen/LaaProductionWeb/LaaProduction.Web/Controllers/SearchController.cs
2023-06-21 15:29:08 +02:00

107 lines
3.2 KiB
C#

namespace LaaProduction.Web.Controllers
{
using LaaProduction.Personalization;
using LaaProductionHttp.Interfaces;
using LaaProduction.Web.App_Infrastructure;
using LaaProduction.Services.Interfaces;
using LaaProduction.Services.Models.Search;
using LaaProductionDI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
public class SearchController : Controller
{
private readonly IOrdersService ordersService;
private readonly IHttpClient httpClient;
public SearchController()
{
this.ordersService = LaaServiceProvider.GetService<IOrdersService>();
this.httpClient = LaaServiceProvider.GetService<IHttpClient>();
}
[HttpGet]
public ActionResult Orders(string search)
{
var model = this.ordersService.FindOrders(search);
return this.PartialView(model);
}
[HttpGet]
public ActionResult Production(string productionNr)
{
var model = this.ordersService.FindProductionOrders(productionNr);
return this.PartialView(model);
}
[HttpGet]
public ActionResult Positions(string orderNr)
{
var model = this.ordersService.FindPositions(orderNr);
return this.PartialView(model);
}
[HttpGet]
[AllowedRoles(EmployeesRoles.WEB_API_Explorer)]
public async Task<ActionResult> Wildcard()
{
var fieldsResult = await this.httpClient
.Get("/Search/Wildcard")
.AuthorizeBearer(this.User.GetNameIdentifier())
.SendAsync<IEnumerable<string>>();
var model = new SearchWildcardModel();
if (fieldsResult.Succeeded)
{
if (fieldsResult.Value != null)
{
model.Fields = fieldsResult.Value.ToDictionary(x => x, x => false);
}
}
else
{
this.ModelState.AddModelErrors(fieldsResult);
}
return this.View(model);
}
[HttpPost]
[AllowedRoles(EmployeesRoles.WEB_API_Explorer)]
public async Task<ActionResult> Wildcard(SearchWildcardModel model)
{
var fields = string.Empty;
if (model.Fields.Any(x => x.Value))
{
fields = WebUtility.UrlEncode(string.Join(",", model.Fields.Where(x => x.Value).Select(x => x.Key)));
}
var httpResult = await this.httpClient
.Post($"/Search/Wildcard")
.WhitJsonBody(new { model.Token, fields })
.AuthorizeBearer(this.User.GetNameIdentifier())
.SendAsync<IEnumerable<IDictionary<string, object>>>();
if (httpResult.Succeeded)
{
model.Results = httpResult.Value ?? Array.Empty<IDictionary<string, object>>();
}
else
{
this.ModelState.AddModelErrors(httpResult);
}
return this.View(model);
}
}
}