isn/src/isnd/Data/Packages/Package.cs
2022-10-17 19:17:04 +01:00

73 lines
No EOL
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using isnd.Data.Catalog;
using isnd.Interfaces;
using Newtonsoft.Json;
namespace isnd.Data.Packages
{
public class Package : IObject
{
[Key][Required]
[StringLength(1024)]
public string Id { get; set; }
[Required]
[ForeignKey("Owner")]
public string OwnerId { get; set; }
[StringLength(1024)]
public string Description { get; set; }
public bool Public { get ; set;}
[JsonIgnore]
virtual public ApplicationUser Owner { get; set; }
[JsonIgnore]
public virtual List<PackageVersion> Versions { get; set; }
/// <summary>
/// Latest version at put, posted,
/// or even deletion when no more active version.
/// </summary>
/// <value></value>
[Required][JsonIgnore]
public long CommitNId { get; set ; }
[NotMapped]
public string CommitId { get => CommitNId.ToString(); }
[ForeignKey("CommitNId")]
public virtual Commit LatestVersion{ get; set; }
public DateTime CommitTimeStamp { get; set; }
/// <summary>
/// Returns the leaf
/// </summary>
/// <param name="bid">base url tu use for building the id property</param>
/// <returns></returns>
public RegistrationLeaf ToLeave(string bid, string dlbase)
{
if (Versions.Count == 0) throw new Exception("NO VERSION");
var v = Versions.OrderBy(w => w.NugetVersion).First();
RegistrationLeaf leave = new RegistrationLeaf
{
Id = bid + Id + "/" + v.FullString + ".json",
PackageContent = dlbase + "/" + v.NugetLink,
Entry = new CatalogEntry
{
Id = bid + Id + ".json",
idp = Id,
version = v.FullString,
authors = $"{Owner.FullName} <${Owner.Email}>"
}
};
return leave;
}
}
}