isn/src/isnd/Controllers/Packages/Catalog.cs
2022-12-17 01:29:22 +00:00

89 lines
No EOL
3.5 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using isnd.Services;
using isnd.Entities;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using NuGet.Versioning;
namespace isnd.Controllers
{
public partial class PackagesController
{
// https://docs.microsoft.com/en-us/nuget/api/catalog-resource#versioning
[HttpGet(_pkgRootPrefix + ApiConfig.Catalog)]
public IActionResult CatalogIndex()
{
return Ok(PackageManager.CurrentCatalogIndex);
}
[HttpGet(_pkgRootPrefix + ApiConfig.CatalogPage + "-{id}")]
public IActionResult Index(string id)
{
// https://docs.microsoft.com/en-us/nuget/api/catalog-resource#versioning
return Ok(PackageManager.CurrentCatalogPages[int.Parse(id)]);
}
[HttpGet(_pkgRootPrefix + "{apiVersion}/" + ApiConfig.Registration + "/{id}/{lower}.json")]
public IActionResult CatalogRegistration(string apiVersion, string id, string lower)
{
if (lower.Equals("index", System.StringComparison.InvariantCultureIgnoreCase))
{
var query = new Data.Catalog.RegistrationPageIndexQuery
{
Query = id,
Prerelease = true
};
var index = packageManager.GetPackageRegistrationIndex(query);
if (index == null) return NotFound();
// query.TotalHits = result.Items.Select(i=>i.Items.Length).Aggregate((a,b)=>a+b);
return Ok(index);
}
var leaf = packageManager.SearchById(id, lower, null);
if (leaf.Count() == 0) return NotFound(new { id, lower });
return Ok(leaf.First());
}
/// <summary>
/// Catalog Leaf,
/// Get info about given package id, and optional lower part .
/// </summary>
/// <param name="id">Given Package Id</param>
/// <param name="lower">lower part, a semantic version for the package,
/// and eventually followed by the "package type"</param>
/// <returns>Info about concerned packages, in order to be able and download them</returns>
[HttpGet(_pkgRootPrefix + ApiConfig.CatalogLeaf + "/{id}/{lower?}/index.json")]
public IActionResult CatalogLeaf(string id, string lower = null)
{
Data.PackageVersion[] pkgvs = null;
if (lower == "{lower}") lower = null;
bool askForindex = lower == null;
if (lower != null && lower.IndexOf('/') > 0 )
{
string version = lower.Substring(lower.IndexOf('/'));
pkgvs = this.packageManager.GetCatalogLeaf(id, version, lower).ToArray();
}
else {
pkgvs = this.packageManager.GetCatalogLeaf(id, null, lower).ToArray();
}
if (pkgvs.Count() == 0) return NotFound();
List<string> types = pkgvs.Select(
v => v.Type ?? "Dependency"
).Distinct().ToList();
if (!types.Contains("PackageDelete"))
types.Add("PackageDetails");
var last = pkgvs.Last();
var pub = last.LatestCommit.CommitTimeStamp;
return Ok(new Data.Packages.Catalog.CatalogLeaf
{
CommitId = last.CommitId,
Id = id,
CommitTimeStamp = pub,
Version = last.FullString,
Published = pub,
RefType = types.ToArray()
});
}
}
}