Files
isn/src/isnd/ViewModels/PackageSearchResult.cs

70 lines
2.1 KiB
C#

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text.Json.Serialization;
using System.Threading;
using isnd.Data;
using isnd.Data.Packages;
using isnd.Entities;
using NuGet.Packaging.Core;
namespace isnd.ViewModels
{
public class PackageSearchResult
{
public IEnumerable<Package> GetResults()
{
return result;
}
private IEnumerable<Package> result=null;
[JsonIgnore]
public string ApiBase{get; private set;}
public PackageSearchResult(string apiBase)
{
// empty result
this.ApiBase = apiBase;
}
public PackageSearchResult(IEnumerable<Package> result, string apiBase, int totalHit)
{
this.result = result;
this.ApiBase = apiBase;
data = (totalHit>0) ? result.Select(p=> NewPackageHit(apiBase, p)).ToArray()
: new PackageHit[0];
this.totalHits = totalHit;
}
private static PackageHit NewPackageHit(string apiBase, Package package)
{
string regId = $"{apiBase}{ApiConfig.Registration}/{package.Id}/index.json";
var latest = package.GetLatestVersion();
if (latest==null) return null;
var pkgHit = new PackageHit(regId, package.Id,
latest.NugetVersion.ToString(),
latest.Description);
if (package.Versions!=null)
{
pkgHit.versions = package.Versions
.Select(v => new SearchVersionInfo(apiBase, v)).ToArray();
pkgHit.packageTypes = package.Versions.Where(v=> v.Type!=null)
.Select(v=>new PackageType(v.Type, new System.Version(v.Major,v.Minor,v.Patch, v.Revision)))?.ToArray();
}
return pkgHit;
}
public PackageHit[] data { get; protected set; } = [];
public int totalHits { get; set; } = 0;
}
}