isn/src/isnd/Data/Catalog/RegistrationPage.cs

100 lines
3.2 KiB
C#
Raw Normal View History

2022-09-23 20:34:51 +01:00
using System;
2022-09-25 02:24:21 +01:00
using System.Collections.Generic;
2022-10-15 18:26:55 +01:00
using System.Linq;
using isnd.Data.Packages;
2022-09-23 20:34:51 +01:00
using Newtonsoft.Json;
2022-10-15 18:26:55 +01:00
using NuGet.Versioning;
2022-09-23 20:34:51 +01:00
namespace isnd.Data.Catalog
{
2023-03-14 00:15:42 +00:00
public class RegistrationPage : HappyIdOwner
2022-09-23 20:34:51 +01:00
{
2022-09-25 02:24:21 +01:00
[JsonProperty("@id")]
2023-03-14 00:15:42 +00:00
public string Id { get => GetId(); }
2023-01-29 13:04:50 +00:00
private readonly string pkgid;
private readonly List<PackageVersion> items;
2022-10-15 18:26:55 +01:00
protected string Bid { get ; private set; }
2023-03-14 00:15:42 +00:00
protected string ExternalUrl { get; }
2022-10-15 18:26:55 +01:00
2023-03-14 00:15:42 +00:00
public RegistrationPage (string bid, string pkgid, string extUrl) : base(bid + "/" + pkgid + "/index.json")
2022-10-15 18:26:55 +01:00
{
Bid = bid;
2023-03-14 00:15:42 +00:00
Parent = Bid + $"/{pkgid}/index.json";
ExternalUrl = extUrl;
2022-10-15 18:26:55 +01:00
this.items = new List<PackageVersion>();
2023-03-14 00:15:42 +00:00
this.pkgid = pkgid;
2022-10-15 18:26:55 +01:00
}
2023-03-14 00:15:42 +00:00
public RegistrationPage(string bid, string pkgid, string extUrl, List<PackageVersion> versions) : this(bid, pkgid, extUrl)
2022-10-15 18:26:55 +01:00
{
2023-03-14 00:15:42 +00:00
AddVersionRange(versions);
2022-10-15 18:26:55 +01:00
}
2022-10-17 19:17:04 +01:00
public string GetPackageId()
2022-10-15 18:26:55 +01:00
{
2022-10-17 19:17:04 +01:00
return pkgid;
2022-10-15 18:26:55 +01:00
}
2022-09-23 20:34:51 +01:00
/// <summary>
2023-03-14 00:15:42 +00:00
/// The array of registration leaves and their associate metadata
2022-09-23 20:34:51 +01:00
/// </summary>
/// <value></value>
[JsonProperty("items")]
2023-03-14 00:15:42 +00:00
public CatalogEntry[] Items { get => items.Select((p) => p.ToLeave(Bid, ExternalUrl)).ToArray(); }
2022-10-15 18:26:55 +01:00
public void AddVersionRange(IEnumerable<PackageVersion> vitems)
{
if (vitems.Count() == 0) return;
2023-03-05 01:26:50 +00:00
NuGetVersion upper = null;
NuGetVersion lower = null;
if (Lower!=null) lower = new NuGetVersion(Lower);
if (Upper!=null) upper = new NuGetVersion(Upper);
// Assert.True(items.All(p=>p.Id == id));
long commitMax = 0;
foreach (var p in vitems)
{
2023-03-14 00:15:42 +00:00
if (items.Contains(p)) continue;
if (upper == null) upper = p.NugetVersion;
else if ( upper < p.NugetVersion) upper = p.NugetVersion;
if (lower == null) lower = p.NugetVersion;
else if (lower > p.NugetVersion) lower = p.NugetVersion;
if (p.CommitNId > commitMax) commitMax = p.CommitNId;
2023-03-05 01:26:50 +00:00
items.Add(p);
}
Upper = upper.ToFullString();
Lower = lower.ToFullString();
CommitId = commitMax.ToString();
2022-10-15 18:26:55 +01:00
}
2023-03-05 01:26:50 +00:00
2022-09-23 20:34:51 +01:00
/// <summary>
/// The highest SemVer 2.0.0 version in the page (inclusive)
/// </summary>
/// <value></value>
[JsonProperty("upper"), JsonRequired]
2022-10-15 18:26:55 +01:00
public string Upper { get; private set; }
2022-09-23 20:34:51 +01:00
/// <summary>
/// The lowest SemVer 2.0.0 version in the page (inclusive)
/// </summary>
/// <value></value>
[JsonProperty("lower"), JsonRequired]
2022-10-15 18:26:55 +01:00
public string Lower { get; private set; }
2022-09-23 20:34:51 +01:00
/// <summary>
/// The URL to the registration index
/// </summary>
/// <value></value>
[JsonProperty("parent")]
public string Parent { get; set; }
2022-09-25 02:24:21 +01:00
[JsonProperty("count")]
2023-03-05 01:26:50 +00:00
public int Count { get => items.Count; }
2022-09-25 02:24:21 +01:00
public string CommitId { get; internal set; }
public DateTime CommitTimeStamp { get; internal set; }
2022-09-23 20:34:51 +01:00
}
}