From 06b011540524321a7b5c739124577108e765988a Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 30 Apr 2023 23:40:39 +0100 Subject: [PATCH] Commit Id and TimerStamps --- .../Packages/PackagesController.Put.cs | 18 ++++------------- src/isnd/Data/ApplicationDbContext.cs | 20 ++++++++++++------- src/isnd/Data/Catalog/CatalogEntry.cs | 3 +++ src/isnd/Data/Catalog/PackageRegistration.cs | 3 --- src/isnd/Data/Packages/Package.cs | 13 +++++------- src/isnd/Data/Packages/PackageVersion.cs | 3 ++- ...=> 20230430215312_pkgversions.Designer.cs} | 5 +---- ...sions.cs => 20230430215312_pkgversions.cs} | 14 ++++++++++++- .../ApplicationDbContextModelSnapshot.cs | 3 --- src/isnd/Services/PackageManager.cs | 7 +++++++ 10 files changed, 48 insertions(+), 41 deletions(-) rename src/isnd/Migrations/{20230430192411_pkgversions.Designer.cs => 20230430215312_pkgversions.Designer.cs} (98%) rename src/isnd/Migrations/{20230430192411_pkgversions.cs => 20230430215312_pkgversions.cs} (70%) diff --git a/src/isnd/Controllers/Packages/PackagesController.Put.cs b/src/isnd/Controllers/Packages/PackagesController.Put.cs index b87b6bd..cbf26a7 100644 --- a/src/isnd/Controllers/Packages/PackagesController.Put.cs +++ b/src/isnd/Controllers/Packages/PackagesController.Put.cs @@ -19,10 +19,6 @@ using isnd.Entities; using Microsoft.AspNetCore.Http; using isn.abst; using isnd.Data.Packages; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc.ModelBinding; -using isn; -using isnd.Helpers; using Microsoft.EntityFrameworkCore; namespace isnd.Controllers @@ -55,8 +51,6 @@ namespace isnd.Controllers TimeStamp = DateTimeOffset.Now.ToUniversalTime() }; - - foreach (IFormFile file in Request.Form.Files) { string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? @@ -122,27 +116,23 @@ namespace isnd.Controllers var destdir = new DirectoryInfo(dest.DirectoryName); if (dest.Exists) { - logger.LogWarning($"Existant package in disk : {dest.FullName}"); + logger.LogWarning($"Existant package on disk : '{dest.FullName}'"); // La version existe sur le disque, // mais si elle ne l'est pas en base de donnéés, // on remplace la version sur disque. string exFullString = version.ToFullString(); var pkgv = dbContext.PackageVersions. Include(v=>v.LatestCommit) - .Single( + .SingleOrDefault( v => v.PackageId == pkg.Id && v.FullString == exFullString ); - if (pkgv!=null && pkgv.IsDeleted) - { - dest.Delete(); - } - else if (pkgv != null) + if (pkgv!=null && ! pkgv.IsDeleted) { string msg = $"existant : {pkg.Id}-{exFullString}"; logger.LogWarning("400 : {msg}", msg); ModelState.AddModelError("pkgversion", msg); return BadRequest(this.CreateAPIKO("existant")); - } + } else dest.Delete(); } { if (!destdir.Exists) destdir.Create(); diff --git a/src/isnd/Data/ApplicationDbContext.cs b/src/isnd/Data/ApplicationDbContext.cs index d01c0b6..ec0489a 100644 --- a/src/isnd/Data/ApplicationDbContext.cs +++ b/src/isnd/Data/ApplicationDbContext.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; -using isnd.Data; using isnd.Data.ApiKeys; using isnd.Data.Packages; @@ -22,6 +17,17 @@ namespace isnd.Data public ApplicationDbContext(DbContextOptions options) : base(options) { } + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + _ = builder.Entity() + .HasKey( v => new { v.PackageId, v.FullString } ); + _ = builder.Entity() + .HasOne(v => v.Package).WithMany(p => p.Versions).HasForeignKey(x => x.PackageId); + // _ = builder.Entity().HasMany(p => p.Versions).WithOne(V => V.Package).HasForeignKey(x => new { x.PackageId, x.FullString }); + } + + /// /// User API keys /// @@ -33,7 +39,7 @@ namespace isnd.Data /// /// public DbSet Packages { get; set; } - + /// /// Package Versions /// diff --git a/src/isnd/Data/Catalog/CatalogEntry.cs b/src/isnd/Data/Catalog/CatalogEntry.cs index 2edfbbc..acdebf9 100644 --- a/src/isnd/Data/Catalog/CatalogEntry.cs +++ b/src/isnd/Data/Catalog/CatalogEntry.cs @@ -22,6 +22,9 @@ namespace isnd.Data.Catalog Version = pkg.FullString; authors = $"{pkg.Package.Owner.FullName} <${pkg.Package.Owner.Email}>"; packageContent = apiBase + pkg.NugetLink; + CommitId = pkg.CommitId; + CommitTimeStamp = pkg.LatestCommit.CommitTimeStamp; + } [JsonProperty("@type")] diff --git a/src/isnd/Data/Catalog/PackageRegistration.cs b/src/isnd/Data/Catalog/PackageRegistration.cs index 33b96a4..bf4aeac 100644 --- a/src/isnd/Data/Catalog/PackageRegistration.cs +++ b/src/isnd/Data/Catalog/PackageRegistration.cs @@ -20,11 +20,8 @@ namespace isnd.Data.Catalog { new CatalogPage(bid, pkg.Id, apiBase, pkg.Versions) }; - - CommitId = pkg.LatestCommit.CommitId; CommitTimeStamp = pkg.LatestCommit.CommitTimeStamp; - } [JsonProperty("count")] diff --git a/src/isnd/Data/Packages/Package.cs b/src/isnd/Data/Packages/Package.cs index c140f08..00c93ac 100644 --- a/src/isnd/Data/Packages/Package.cs +++ b/src/isnd/Data/Packages/Package.cs @@ -2,9 +2,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using isnd.Data.Catalog; -using isnd.Data.Packages.Catalog; using isnd.Interfaces; using Newtonsoft.Json; @@ -24,7 +21,7 @@ namespace isnd.Data.Packages DateTimeOffset CommitTimeStamp { get; set; } } - public class Package : IObject, IPackage + public class Package { [Key] [Required] @@ -53,16 +50,16 @@ namespace isnd.Data.Packages /// /// [Required] - [JsonIgnore] + [JsonIgnore] + [ForeignKey("LatestCommit")] public long CommitNId { get; set; } + [NotMapped] public string CommitId { get => CommitNId.ToString(); } - [ForeignKey("CommitNId")] - public virtual Commit LatestCommit { get; set; } - public DateTimeOffset CommitTimeStamp { get; set; } + } } \ No newline at end of file diff --git a/src/isnd/Data/Packages/PackageVersion.cs b/src/isnd/Data/Packages/PackageVersion.cs index 77940d5..5203bdc 100644 --- a/src/isnd/Data/Packages/PackageVersion.cs +++ b/src/isnd/Data/Packages/PackageVersion.cs @@ -1,3 +1,4 @@ +using System.Security.Principal; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -13,7 +14,7 @@ using NuGet.Versioning; namespace isnd.Data { [PrimaryKey("PackageId", "FullString")] - public class PackageVersion + public class PackageVersion { [Required] [ForeignKey("Package")] diff --git a/src/isnd/Migrations/20230430192411_pkgversions.Designer.cs b/src/isnd/Migrations/20230430215312_pkgversions.Designer.cs similarity index 98% rename from src/isnd/Migrations/20230430192411_pkgversions.Designer.cs rename to src/isnd/Migrations/20230430215312_pkgversions.Designer.cs index 22455e0..cf5aadd 100644 --- a/src/isnd/Migrations/20230430192411_pkgversions.Designer.cs +++ b/src/isnd/Migrations/20230430215312_pkgversions.Designer.cs @@ -12,7 +12,7 @@ using isnd.Data; namespace isnd.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20230430192411_pkgversions")] + [Migration("20230430215312_pkgversions")] partial class pkgversions { /// @@ -317,9 +317,6 @@ namespace isnd.Migrations b.Property("CommitNId") .HasColumnType("bigint"); - b.Property("CommitTimeStamp") - .HasColumnType("timestamp with time zone"); - b.Property("Description") .HasMaxLength(1024) .HasColumnType("character varying(1024)"); diff --git a/src/isnd/Migrations/20230430192411_pkgversions.cs b/src/isnd/Migrations/20230430215312_pkgversions.cs similarity index 70% rename from src/isnd/Migrations/20230430192411_pkgversions.cs rename to src/isnd/Migrations/20230430215312_pkgversions.cs index a4ac060..b1caa40 100644 --- a/src/isnd/Migrations/20230430192411_pkgversions.cs +++ b/src/isnd/Migrations/20230430215312_pkgversions.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore.Migrations; +using System; +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable @@ -18,6 +19,10 @@ namespace isnd.Migrations name: "IX_PackageVersions_PackageId", table: "PackageVersions"); + migrationBuilder.DropColumn( + name: "CommitTimeStamp", + table: "Packages"); + migrationBuilder.AddPrimaryKey( name: "PK_PackageVersions", table: "PackageVersions", @@ -31,6 +36,13 @@ namespace isnd.Migrations name: "PK_PackageVersions", table: "PackageVersions"); + migrationBuilder.AddColumn( + name: "CommitTimeStamp", + table: "Packages", + type: "timestamp with time zone", + nullable: false, + defaultValue: new DateTimeOffset(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0))); + migrationBuilder.AddPrimaryKey( name: "PK_PackageVersions", table: "PackageVersions", diff --git a/src/isnd/Migrations/ApplicationDbContextModelSnapshot.cs b/src/isnd/Migrations/ApplicationDbContextModelSnapshot.cs index a71c386..9d2f708 100644 --- a/src/isnd/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/src/isnd/Migrations/ApplicationDbContextModelSnapshot.cs @@ -314,9 +314,6 @@ namespace isnd.Migrations b.Property("CommitNId") .HasColumnType("bigint"); - b.Property("CommitTimeStamp") - .HasColumnType("timestamp with time zone"); - b.Property("Description") .HasMaxLength(1024) .HasColumnType("character varying(1024)"); diff --git a/src/isnd/Services/PackageManager.cs b/src/isnd/Services/PackageManager.cs index dcf7e65..92d3ea5 100644 --- a/src/isnd/Services/PackageManager.cs +++ b/src/isnd/Services/PackageManager.cs @@ -281,6 +281,8 @@ namespace isnd.Services .SingleAsync(p => p.Id.ToLower() == query.Query); if (scope.Versions.Count==0) return null; string bid = $"{apiBase}{ApiConfig.Registration}"; + foreach (var version in scope.Versions) + version.LatestCommit = dbContext.Commits.Single(c=>c.Id == version.CommitNId); return new PackageRegistration(bid, apiBase, scope); } @@ -299,6 +301,11 @@ namespace isnd.Services .ToListAsync() ); var pkgs = scope; + foreach (var pkg in pkgs) + { + foreach (var version in pkg.Versions) + version.LatestCommit = dbContext.Commits.Single(c=>c.Id == version.CommitNId); + } return pkgs.Select(p => new PackageRegistration(bid, apiBase, p)); }