autocomplete

This commit is contained in:
Paul Schneider 2021-06-22 00:10:01 +01:00
commit aaa49788dc
4 changed files with 52 additions and 14 deletions

View file

@ -1,11 +1,11 @@
using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using nuget_host.Entities; using nuget_host.Entities;
using nuget_host.Data; using nuget_host.Data;
using System.Linq;
using nuget_host.ViewModels;
namespace nuget_host.Controllers namespace nuget_host.Controllers
{ {
@ -15,19 +15,25 @@ namespace nuget_host.Controllers
readonly IHostingEnvironment _environment; readonly IHostingEnvironment _environment;
public SmtpSettings _smtpSettings { get; } //set only via Secret Manager public SmtpSettings _smtpSettings { get; } //set only via Secret Manager
private ApplicationDbContext _dbContext;
public HomeController( public HomeController(
IOptions<SmtpSettings> smtpSettings, IOptions<SmtpSettings> smtpSettings,
IHostingEnvironment environment, IHostingEnvironment environment,
ApplicationDbContext dbContext,
ILogger<HomeController> logger) ILogger<HomeController> logger)
{ {
_environment = environment; _environment = environment;
_logger = logger; _logger = logger;
_smtpSettings = smtpSettings.Value; _smtpSettings = smtpSettings.Value;
_dbContext = dbContext;
} }
public IActionResult Index() public IActionResult Index()
{ {
return View(); return View(new HomeIndexViewModel{
PkgCount = _dbContext.Packages.Count()
});
} }
public IActionResult About() public IActionResult About()

View file

@ -33,7 +33,7 @@ namespace nuget_host.Controllers
this.dbContext = dbContext; this.dbContext = dbContext;
} }
const string defaultSemVer = "2.0.0";
// dotnet add . package -s http://localhost:5000/packages nuget-cli // dotnet add . package -s http://localhost:5000/packages nuget-cli
// packages/FindPackagesById()?id='nuget-cli'&semVerLevel=2.0.0 // packages/FindPackagesById()?id='nuget-cli'&semVerLevel=2.0.0
@ -43,11 +43,11 @@ namespace nuget_host.Controllers
[HttpGet("~/search/index.json")] [HttpGet("~/search/index.json")]
public IActionResult Index( public IActionResult Index(
string q, string q,
string semVerLevel = defaultSemVer,
bool prerelease = false, bool prerelease = false,
string packageType = null, string packageType = null,
int skip = 0, int skip = 0,
int take = 25, int take = 25)
string semVerLevel = "2.0.0")
{ {
if (string.IsNullOrEmpty(q)) if (string.IsNullOrEmpty(q))
{ {
@ -63,7 +63,8 @@ namespace nuget_host.Controllers
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease)) && (prerelease || p.Versions.Any(v => !v.IsPrerelease))
&& (packageType == null || p.Versions.Any(v => v.Type == packageType)) && (packageType == null || p.Versions.Any(v => v.Type == packageType))
); );
var result = new { var result = new
{
totalHits = scope.Count(), totalHits = scope.Count(),
data = scope.Skip(skip).Take(take).ToArray() data = scope.Skip(skip).Take(take).ToArray()
}; };
@ -75,13 +76,13 @@ namespace nuget_host.Controllers
{ {
// Assert.False (q==null); // Assert.False (q==null);
string query = q; string query = q;
if (query.Length==0) return false; if (query.Length == 0) return false;
while (id.Length > 0) while (id.Length > 0)
{ {
int i = 0; int i = 0;
while (id.Length > i && char.IsLower(id[i])) i++; while (id.Length > i && char.IsLower(id[i])) i++;
if (i==0) break; if (i == 0) break;
id = id.Substring(i); id = id.Substring(i);
if (id.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true; if (id.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
} }
@ -95,5 +96,28 @@ namespace nuget_host.Controllers
} }
return false; return false;
} }
// GET /autocomplete?id=nuget.protocol&prerelease=true
[HttpGet("~/autocomplete")]
public IActionResult AutoComplete(
string id,
string semVerLevel = defaultSemVer,
bool prerelease = false,
string packageType = null,
int skip = 0,
int take = 25)
{
return Ok(new
{
data =
dbContext.PackageVersions.Where(
v => v.PackageId == id
&& (prerelease || !v.IsPrerelease)
&& (packageType == null || v.Type == packageType)
).Select(v => v.FullString)
.Skip(skip).Take(take).ToArray()
});
}
} }
} }

View file

@ -0,0 +1,7 @@
namespace nuget_host.ViewModels
{
public class HomeIndexViewModel
{
public int PkgCount { get; set; }
}
}

View file

@ -1,12 +1,13 @@
@{ @model HomeIndexViewModel
@{
ViewData["Title"] = "Home Page"; ViewData["Title"] = "Home Page";
} }
<div class="text-center"> <div class="text-center">
<h1 class="display-4">Welcome</h1> <h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<h1> <h1>
<img src="~/icon.jpg"> <img src="~/icon.jpg">
Welcome to Nuget host Welcome to Apple
</h1> </h1>
<strong>@Model.PkgCount identifiant(s) de paquet dans le SI</strong>
</div> </div>