Permalink
Browse files

Allow optional inclusion of prerelease packages in search results

Fixes issue #523
  • Loading branch information...
1 parent 87344bd commit 3a7bc73d4f9e8ea422f157c64783bcc72eda0b71 @pranavkm pranavkm committed with pranavkm Aug 10, 2012
@@ -891,7 +891,7 @@ private static Package CreatePackage(string id, string version)
return new Package
{
PackageRegistration = new PackageRegistration { Id = id },
- Version = version
+ Version = version,
};
}
@@ -707,6 +707,7 @@ header.main {
fieldset.search label {
display: inline;
+ margin-left: 10px;
}
#searchResults {
@@ -715,7 +716,6 @@ header.main {
padding: 0;
}
-
/* List Package */
section.package {
border-top: 1px solid #ccc;
@@ -3,7 +3,7 @@
namespace NuGetGallery
{
- public abstract class AppController : Controller
+ public abstract partial class AppController : Controller
{
protected virtual IIdentity Identity
{
@@ -129,14 +129,14 @@ public virtual ActionResult DisplayPackage(string id, string version)
return View(model);
}
- public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1)
+ public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1, bool prerelease = false)
{
if (page < 1)
{
page = 1;
}
- IQueryable<Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true);
+ IQueryable<Package> packageVersions = packageSvc.GetPackagesForListing(prerelease);
q = (q ?? "").Trim();
@@ -147,21 +147,13 @@ public virtual ActionResult ListPackages(string q, string sortOrder = null, int
sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder;
}
+ var searchFilter = GetSearchFilter(q, sortOrder, page, prerelease);
int totalHits;
- if (!String.IsNullOrEmpty(q))
+ packageVersions = searchSvc.Search(packageVersions, searchFilter, out totalHits);
+ if (page == 1 && !packageVersions.Any())
{
- var searchFilter = GetSearchFilter(q, sortOrder, page);
- packageVersions = searchSvc.Search(packageVersions, searchFilter, out totalHits);
- if (page == 1 && !packageVersions.Any())
- {
- // In the event the index wasn't updated, we may get an incorrect count.
- totalHits = 0;
- }
- }
- else
- {
- packageVersions = packageVersions.SortBy(GetSortExpression(sortOrder));
- totalHits = packageVersions.Count();
+ // In the event the index wasn't updated, we may get an incorrect count.
+ totalHits = 0;
}
var viewModel = new PackageListViewModel(packageVersions,
@@ -170,7 +162,8 @@ public virtual ActionResult ListPackages(string q, string sortOrder = null, int
totalHits,
page - 1,
Constants.DefaultPackageListPageSize,
- Url);
+ Url,
+ prerelease);
ViewBag.SearchTerm = q;
@@ -521,13 +514,14 @@ protected internal virtual IPackage ReadNuGetPackage(Stream stream)
return new ZipPackage(stream);
}
- private SearchFilter GetSearchFilter(string q, string sortOrder, int page)
+ private SearchFilter GetSearchFilter(string q, string sortOrder, int page, bool includePrerelease)
{
var searchFilter = new SearchFilter
{
SearchTerm = q,
Skip = (page - 1) * Constants.DefaultPackageListPageSize, // pages are 1-based.
- Take = Constants.DefaultPackageListPageSize
+ Take = Constants.DefaultPackageListPageSize,
+ IncludePrerelease = includePrerelease
};
switch (sortOrder)
@@ -134,6 +134,7 @@ private static void UpdatePackage(PackageIndexEntity package)
// Fields meant for filtering and sorting
document.Add(new Field("Key", key, Field.Store.YES, Field.Index.NO));
+ document.Add(new Field("IsLatest", package.IsLatest.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));
document.Add(new Field("IsLatestStable", package.IsLatestStable.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));
document.Add(new Field("PublishedDate", package.Published.Ticks.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));
document.Add(new Field("DownloadCount", package.DownloadCount.ToString(CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED));
@@ -50,7 +50,6 @@ public IQueryable<Package> Search(IQueryable<Package> packages, SearchFilter sea
return keys.Select(key => LookupPackage(lookup, key))
.Where(p => p != null)
- .ToList()
.AsQueryable();
}
@@ -65,7 +64,7 @@ private static IList<int> SearchCore(SearchFilter searchFilter, out int totalHit
{
if (!Directory.Exists(LuceneCommon.IndexDirectory))
{
- totalHits = 0;
+ totalHits = 0;
return new int[0];
}
@@ -77,12 +76,10 @@ private static IList<int> SearchCore(SearchFilter searchFilter, out int totalHit
var searcher = new IndexSearcher(directory, readOnly: true);
var query = ParseQuery(searchFilter);
- Filter filter = null;
- if (!searchFilter.IncludePrerelease)
- {
- var isLatestStableQuery = new TermQuery(new Term("IsLatestStable", Boolean.TrueString));
- filter = new QueryWrapperFilter(isLatestStableQuery);
- }
+ var filterTerm = searchFilter.IncludePrerelease ? "IsLatest" : "IsLatestStable";
+ var termQuery = new TermQuery(new Term(filterTerm, Boolean.TrueString));
+ Filter filter = new QueryWrapperFilter(termQuery);
+
var results = searcher.Search(query, filter: filter, n: numRecords, sort: new Sort(sortField));
var keys = results.scoreDocs.Skip(searchFilter.Skip)
@@ -105,6 +105,7 @@ public class ActionNamesClass {
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
public class ViewNames {
public readonly string _ListPackage = "~/Views/Packages/_ListPackage.cshtml";
+ public readonly string _PackageDependencies = "~/Views/Packages/_PackageDependencies.cshtml";
public readonly string ConfirmOwner = "~/Views/Packages/ConfirmOwner.cshtml";
public readonly string ContactOwners = "~/Views/Packages/ContactOwners.cshtml";
public readonly string Delete = "~/Views/Packages/Delete.cshtml";
@@ -141,11 +142,12 @@ public class T4MVC_PackagesController: NuGetGallery.PackagesController {
return callInfo;
}
- public override System.Web.Mvc.ActionResult ListPackages(string q, string sortOrder, int page) {
+ public override System.Web.Mvc.ActionResult ListPackages(string q, string sortOrder, int page, bool prerelease) {
var callInfo = new T4MVC_ActionResult(Area, Name, ActionNames.ListPackages);
callInfo.RouteValueDictionary.Add("q", q);
callInfo.RouteValueDictionary.Add("sortOrder", sortOrder);
callInfo.RouteValueDictionary.Add("page", page);
+ callInfo.RouteValueDictionary.Add("prerelease", prerelease);
return callInfo;
}
@@ -14,7 +14,7 @@ public interface IPackageService
Package FindPackageByIdAndVersion(string id, string version, bool allowPrerelease = true);
- IQueryable<Package> GetLatestPackageVersions(bool allowPrerelease);
+ IQueryable<Package> GetPackagesForListing(bool includePrerelease);
void PublishPackage(string id, string version);
@@ -147,19 +147,15 @@ public virtual Package FindPackageByIdAndVersion(string id, string version, bool
return package;
}
- public IQueryable<Package> GetLatestPackageVersions(bool allowPrerelease)
+ public IQueryable<Package> GetPackagesForListing(bool includePrerelease)
{
var packages = packageRepo.GetAll()
.Include(x => x.PackageRegistration)
.Include(x => x.PackageRegistration.Owners)
.Where(p => p.Listed);
- if (allowPrerelease)
- {
- // Since we use this for listing, only show prerelease versions of a package if it does not have stable version
- return packages.Where(p => p.IsLatestStable || (p.IsLatest && !p.PackageRegistration.Packages.Any(p2 => p2.IsLatestStable)));
- }
- return packages.Where(x => x.IsLatestStable);
+ return includePrerelease ? packages.Where(p => p.IsLatest) :
+ packages.Where(p => p.IsLatestStable);
}
public IEnumerable<Package> FindPackagesByOwner(User user)
View
@@ -253,8 +253,7 @@ public static class T4Extensions {
result.RouteValueDictionary.Add("Action", action);
}
- public static bool FileExists(string virtualPath)
- {
+ public static bool FileExists(string virtualPath) {
if (!HostingEnvironment.IsHosted) return false;
string filePath = HostingEnvironment.MapPath(virtualPath);
return System.IO.File.Exists(filePath);
@@ -304,28 +303,24 @@ public class T4MVC_ActionResult : System.Web.Mvc.ActionResult, IT4MVCActionResul
public RouteValueDictionary RouteValueDictionary { get; set; }
}
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
-public class T4MVC_JsonResult : System.Web.Mvc.JsonResult, IT4MVCActionResult
-{
- public T4MVC_JsonResult(string area, string controller, string action)
- : base()
- {
+public class T4MVC_JsonResult : System.Web.Mvc.JsonResult, IT4MVCActionResult {
+ public T4MVC_JsonResult(string area, string controller, string action): base() {
this.InitMVCT4Result(area, controller, action);
}
-
+
public string Controller { get; set; }
public string Action { get; set; }
public RouteValueDictionary RouteValueDictionary { get; set; }
}
+
namespace Links {
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
public static class Scripts {
private const string URLPATH = "~/Scripts";
public static string Url() { return T4MVCHelpers.ProcessVirtualPath(URLPATH); }
public static string Url(string fileName) { return T4MVCHelpers.ProcessVirtualPath(URLPATH + "/" + fileName); }
- public static readonly string Home_js = T4MVCHelpers.IsProduction() && T4Extensions.FileExists(URLPATH + "/Home.min.js") ? Url("Home.min.js") : Url("Home.js");
-
public static readonly string jquery_1_6_2_vsdoc_js = T4MVCHelpers.IsProduction() && T4Extensions.FileExists(URLPATH + "/jquery-1.6.2-vsdoc.min.js") ? Url("jquery-1.6.2-vsdoc.min.js") : Url("jquery-1.6.2-vsdoc.js");
public static readonly string jquery_1_6_2_js = T4MVCHelpers.IsProduction() && T4Extensions.FileExists(URLPATH + "/jquery-1.6.2.min.js") ? Url("jquery-1.6.2.min.js") : Url("jquery-1.6.2.js");
@@ -341,8 +336,10 @@ public static class Scripts {
public static readonly string modernizr_2_0_6_development_only_js = T4MVCHelpers.IsProduction() && T4Extensions.FileExists(URLPATH + "/modernizr-2.0.6-development-only.min.js") ? Url("modernizr-2.0.6-development-only.min.js") : Url("modernizr-2.0.6-development-only.js");
+ public static readonly string stats_js = T4MVCHelpers.IsProduction() && T4Extensions.FileExists(URLPATH + "/stats.min.js") ? Url("stats.min.js") : Url("stats.js");
+
public static readonly string ZeroClipboard_js = T4MVCHelpers.IsProduction() && T4Extensions.FileExists(URLPATH + "/ZeroClipboard.min.js") ? Url("ZeroClipboard.min.js") : Url("ZeroClipboard.js");
-
+
public static readonly string ZeroClipboard_swf = Url("ZeroClipboard.swf");
}
@@ -15,9 +15,9 @@ public static string Home(this UrlHelper url)
return url.RouteUrl(RouteName.Home);
}
- public static string PackageList(this UrlHelper url, int page, string sortOrder, string searchTerm)
+ public static string PackageList(this UrlHelper url, int page, string sortOrder, string searchTerm, bool prerelease)
{
- return url.Action(MVC.Packages.ListPackages(searchTerm, sortOrder, page));
+ return url.Action(MVC.Packages.ListPackages(searchTerm, sortOrder, page, prerelease));
}
public static string PackageList(this UrlHelper url)
@@ -13,15 +13,14 @@ public class PackageListViewModel
int totalCount,
int pageIndex,
int pageSize,
- UrlHelper url)
+ UrlHelper url,
+ bool includePrerelease)
{
// TODO: Implement actual sorting
IEnumerable<ListPackageItemViewModel> items;
using (MiniProfiler.Current.Step("Querying and mapping packages to list"))
{
- items = packages.Skip(pageIndex * pageSize)
- .Take(pageSize)
- .ToList()
+ items = packages.ToList()
.Select(pv => new ListPackageItemViewModel(pv, needAuthors: false));
}
PageIndex = pageIndex;
@@ -35,12 +34,13 @@ public class PackageListViewModel
items,
PageIndex,
pageCount,
- page => url.PackageList(page, sortOrder, searchTerm)
+ page => url.PackageList(page, sortOrder, searchTerm, includePrerelease)
);
Items = pager.Items;
FirstResultIndex = 1 + (PageIndex * PageSize);
LastResultIndex = FirstResultIndex + Items.Count() - 1;
Pager = pager;
+ IncludePrerelease = includePrerelease ? "true" : null;
}
public int FirstResultIndex { get; set; }
@@ -60,5 +60,7 @@ public class PackageListViewModel
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
+
+ public string IncludePrerelease { get; private set; }
}
}
@@ -19,12 +19,17 @@
}
</hgroup>
-@using (Html.BeginForm()) {
+<form method="get" action="">
<fieldset class="form search">
<legend>Sort Order</legend>
<input type="hidden" name="q" value="@Model.SearchTerm" />
<div class="form-field">
+ <select name="prerelease" id="prerelease">
+ @ViewHelpers.Option("", "Stable Only", Model.IncludePrerelease)
+ @ViewHelpers.Option("true", "Include Prerelease", Model.IncludePrerelease)
+ </select>
+
<label for="sortOrder">Sort By</label>
<select name="sortOrder" id="sortOrder">
@if (!Model.SearchTerm.IsEmpty()) {
@@ -36,7 +41,7 @@
</select>
</div>
</fieldset>
-}
+</form>
<ol id="searchResults">
@foreach (var package in Model.Items) {
@@ -52,7 +57,7 @@
@section BottomScripts {
<script>
$(function () {
- $("#sortOrder").change(function () {
+ $("#sortOrder,#prerelease").change(function () {
$(this).closest("form").submit();
});
});

0 comments on commit 3a7bc73

Please sign in to comment.