From aaa49788dc936dd66790997f17c99fa9b26e5e24 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 22 Jun 2021 00:10:01 +0100 Subject: [PATCH] autocomplete --- src/nuget-host/Controllers/HomeController.cs | 14 +++++-- .../Controllers/PackagesController.cs | 38 +++++++++++++++---- .../ViewModels/HomeIndexViewModel.cs | 7 ++++ src/nuget-host/Views/Home/Index.cshtml | 7 ++-- 4 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 src/nuget-host/ViewModels/HomeIndexViewModel.cs diff --git a/src/nuget-host/Controllers/HomeController.cs b/src/nuget-host/Controllers/HomeController.cs index fa9346b..645d85e 100644 --- a/src/nuget-host/Controllers/HomeController.cs +++ b/src/nuget-host/Controllers/HomeController.cs @@ -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, IHostingEnvironment environment, + ApplicationDbContext dbContext, ILogger 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() diff --git a/src/nuget-host/Controllers/PackagesController.cs b/src/nuget-host/Controllers/PackagesController.cs index 760f768..12c9616 100644 --- a/src/nuget-host/Controllers/PackagesController.cs +++ b/src/nuget-host/Controllers/PackagesController.cs @@ -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() + }); + } + + } } \ No newline at end of file diff --git a/src/nuget-host/ViewModels/HomeIndexViewModel.cs b/src/nuget-host/ViewModels/HomeIndexViewModel.cs new file mode 100644 index 0000000..65a8410 --- /dev/null +++ b/src/nuget-host/ViewModels/HomeIndexViewModel.cs @@ -0,0 +1,7 @@ +namespace nuget_host.ViewModels +{ + public class HomeIndexViewModel + { + public int PkgCount { get; set; } + } +} \ No newline at end of file diff --git a/src/nuget-host/Views/Home/Index.cshtml b/src/nuget-host/Views/Home/Index.cshtml index b46d9cb..5d8ccb8 100644 --- a/src/nuget-host/Views/Home/Index.cshtml +++ b/src/nuget-host/Views/Home/Index.cshtml @@ -1,12 +1,13 @@ -@{ +@model HomeIndexViewModel +@{ ViewData["Title"] = "Home Page"; }

Welcome

-

Learn about building Web apps with ASP.NET Core.

- Welcome to Nuget host + Welcome to Apple

+ @Model.PkgCount identifiant(s) de paquet dans le SI