autocomplete
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using nuget_host.Entities;
|
||||
using nuget_host.Data;
|
||||
using System.Linq;
|
||||
using nuget_host.ViewModels;
|
||||
|
||||
namespace nuget_host.Controllers
|
||||
{
|
||||
@ -15,19 +15,25 @@ namespace nuget_host.Controllers
|
||||
readonly IHostingEnvironment _environment;
|
||||
public SmtpSettings _smtpSettings { get; } //set only via Secret Manager
|
||||
|
||||
private ApplicationDbContext _dbContext;
|
||||
|
||||
public HomeController(
|
||||
IOptions<SmtpSettings> smtpSettings,
|
||||
IHostingEnvironment environment,
|
||||
ApplicationDbContext dbContext,
|
||||
ILogger<HomeController> logger)
|
||||
{
|
||||
_environment = environment;
|
||||
_logger = logger;
|
||||
_smtpSettings = smtpSettings.Value;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
return View(new HomeIndexViewModel{
|
||||
PkgCount = _dbContext.Packages.Count()
|
||||
});
|
||||
}
|
||||
|
||||
public IActionResult About()
|
||||
|
@ -33,7 +33,7 @@ namespace nuget_host.Controllers
|
||||
this.dbContext = dbContext;
|
||||
}
|
||||
|
||||
|
||||
const string defaultSemVer = "2.0.0";
|
||||
// dotnet add . package -s http://localhost:5000/packages nuget-cli
|
||||
// packages/FindPackagesById()?id='nuget-cli'&semVerLevel=2.0.0
|
||||
|
||||
@ -43,11 +43,11 @@ namespace nuget_host.Controllers
|
||||
[HttpGet("~/search/index.json")]
|
||||
public IActionResult Index(
|
||||
string q,
|
||||
string semVerLevel = defaultSemVer,
|
||||
bool prerelease = false,
|
||||
string packageType = null,
|
||||
int skip = 0,
|
||||
int take = 25,
|
||||
string semVerLevel = "2.0.0")
|
||||
int take = 25)
|
||||
{
|
||||
if (string.IsNullOrEmpty(q))
|
||||
{
|
||||
@ -56,14 +56,15 @@ namespace nuget_host.Controllers
|
||||
}
|
||||
else
|
||||
{
|
||||
var scope = dbContext.Packages
|
||||
var scope = dbContext.Packages
|
||||
.Include(p => p.Versions)
|
||||
.Where(
|
||||
p => (CamelCaseMatch(p.Id, q) || SeparatedByMinusMatch(p.Id, q))
|
||||
&& (prerelease || p.Versions.Any(v => !v.IsPrerelease))
|
||||
&& (packageType == null || p.Versions.Any(v => v.Type == packageType))
|
||||
);
|
||||
var result = new {
|
||||
var result = new
|
||||
{
|
||||
totalHits = scope.Count(),
|
||||
data = scope.Skip(skip).Take(take).ToArray()
|
||||
};
|
||||
@ -75,13 +76,13 @@ namespace nuget_host.Controllers
|
||||
{
|
||||
// Assert.False (q==null);
|
||||
string query = q;
|
||||
if (query.Length==0) return false;
|
||||
if (query.Length == 0) return false;
|
||||
|
||||
while (id.Length > 0)
|
||||
{
|
||||
int i = 0;
|
||||
while (id.Length > i && char.IsLower(id[i])) i++;
|
||||
if (i==0) break;
|
||||
if (i == 0) break;
|
||||
id = id.Substring(i);
|
||||
if (id.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
|
||||
}
|
||||
@ -95,5 +96,28 @@ namespace nuget_host.Controllers
|
||||
}
|
||||
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()
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
7
src/nuget-host/ViewModels/HomeIndexViewModel.cs
Normal file
7
src/nuget-host/ViewModels/HomeIndexViewModel.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace nuget_host.ViewModels
|
||||
{
|
||||
public class HomeIndexViewModel
|
||||
{
|
||||
public int PkgCount { get; set; }
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
@{
|
||||
@model HomeIndexViewModel
|
||||
@{
|
||||
ViewData["Title"] = "Home Page";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<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>
|
||||
<img src="~/icon.jpg">
|
||||
Welcome to Nuget host
|
||||
Welcome to Apple
|
||||
</h1>
|
||||
<strong>@Model.PkgCount identifiant(s) de paquet dans le SI</strong>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user