-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement first successful package upload
- Loading branch information
1 parent
5daaa82
commit a080893
Showing
52 changed files
with
2,426 additions
and
201 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
Backend/PubNet.API.Abstractions/CQRS/Commands/Packages/INugetPackageDmo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using PubNet.Database.Entities; | ||
using PubNet.Database.Entities.Nuget; | ||
|
||
namespace PubNet.API.Abstractions.CQRS.Commands.Packages; | ||
|
||
public interface INugetPackageDmo | ||
{ | ||
Task<NugetPackageVersion> CreateAsync(Author author, byte[] nupkg, CancellationToken cancellationToken = default); | ||
|
||
Task<NugetPackageVersion> AddVersionAsync(NugetPackage package, byte[] nupkg, CancellationToken cancellationToken = default); | ||
} |
6 changes: 6 additions & 0 deletions
6
Backend/PubNet.API.Abstractions/Packages/Nuget/INugetPackageUploadService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace PubNet.API.Abstractions.Packages.Nuget; | ||
|
||
public interface INugetPackageUploadService | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Backend/PubNet.API.DTO/Packages/Nuget/NugetInvalidPackageErrorDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace PubNet.API.DTO.Packages.Nuget; | ||
|
||
public class NugetInvalidPackageErrorDto | ||
{ | ||
public required string Message { get; init; } | ||
} |
6 changes: 6 additions & 0 deletions
6
Backend/PubNet.API.DTO/Packages/Nuget/NugetPackageAlreadyExistsErrorDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace PubNet.API.DTO.Packages.Nuget; | ||
|
||
public class NugetPackageAlreadyExistsErrorDto | ||
{ | ||
public required string Message { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace PubNet.API.DTO.Packages.Nuget; | ||
|
||
public class NugetSuccessDto | ||
{ | ||
public required string Success { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
Backend/PubNet.API.Services/CQRS/Commands/Packages/NugetPackageDmo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using NuGet.Packaging; | ||
using PubNet.API.Abstractions.CQRS.Commands.Packages; | ||
using PubNet.API.Services.Extensions; | ||
using PubNet.Database.Context; | ||
using PubNet.Database.Entities; | ||
using PubNet.Database.Entities.Nuget; | ||
using PubNet.PackageStorage.Abstractions; | ||
|
||
namespace PubNet.API.Services.CQRS.Commands.Packages; | ||
|
||
public class NugetPackageDmo(PubNetContext context, IArchiveStorage archiveStorage) : INugetPackageDmo | ||
{ | ||
public async Task<NugetPackageVersion> CreateAsync(Author author, byte[] nupkg, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var package = new NugetPackage | ||
{ | ||
Id = Guid.NewGuid(), | ||
Author = author, | ||
Versions = [], | ||
}; | ||
|
||
var version = ReadPackageVersion(package, nupkg); | ||
|
||
package.Name = version.NuspecId ?? throw new InvalidOperationException("Package ID is missing"); | ||
package.Versions.Add(version); | ||
package.LatestVersion = version; | ||
package.LatestVersionId = version.Id; | ||
|
||
await context.NugetPackageVersions.AddAsync(version, cancellationToken); | ||
await context.NugetPackages.AddAsync(package, cancellationToken); | ||
|
||
version.Package = package; | ||
|
||
await context.SaveChangesAsync(cancellationToken); | ||
|
||
await context.Entry(package).ReloadAsync(cancellationToken); | ||
await context.Entry(version).ReloadAsync(cancellationToken); | ||
|
||
return version; | ||
} | ||
|
||
public async Task<NugetPackageVersion> AddVersionAsync(NugetPackage package, byte[] nupkg, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var version = ReadPackageVersion(package, nupkg); | ||
|
||
_ = await archiveStorage.StoreArchiveAsync(package.Author.UserName, package.Name, version.Version, nupkg, | ||
cancellationToken); | ||
|
||
await context.NugetPackageVersions.AddAsync(version, cancellationToken); | ||
|
||
package.Versions.Add(version); | ||
package.LatestVersion = version; | ||
|
||
await context.SaveChangesAsync(cancellationToken); | ||
|
||
await context.Entry(package).ReloadAsync(cancellationToken); | ||
await context.Entry(version).ReloadAsync(cancellationToken); | ||
|
||
return version; | ||
} | ||
|
||
private static NugetPackageVersion ReadPackageVersion(NugetPackage package, byte[] nupkg) | ||
{ | ||
using var packageStream = new MemoryStream(nupkg); | ||
|
||
using var reader = new PackageArchiveReader(packageStream); | ||
var nuspec = reader.NuspecReader; | ||
|
||
var id = nuspec.GetId().ToNullIfEmpty(); | ||
var version = nuspec.GetVersion(); | ||
var title = nuspec.GetTitle().ToNullIfEmpty(); | ||
var description = nuspec.GetDescription().ToNullIfEmpty(); | ||
var authors = nuspec.GetAuthors().ToNullIfEmpty(); | ||
var iconUrl = nuspec.GetIconUrl().ToNullIfEmpty(); | ||
var projectUrl = nuspec.GetProjectUrl().ToNullIfEmpty(); | ||
var copyright = nuspec.GetCopyright().ToNullIfEmpty(); | ||
var tags = nuspec.GetTags().ToNullIfEmpty(); | ||
var readme = nuspec.GetReadme().ToNullIfEmpty(); | ||
var icon = nuspec.GetIcon().ToNullIfEmpty(); | ||
var repositoryMetadata = nuspec.GetRepositoryMetadata(); | ||
var dependencyGroups = nuspec.GetDependencyGroups() ?? []; | ||
|
||
var versionEntity = new NugetPackageVersion | ||
{ | ||
Id = Guid.NewGuid(), | ||
Version = version.ToString(), | ||
PublishedAt = DateTimeOffset.UtcNow, | ||
NuspecId = id, | ||
NuspecVersion = version, | ||
Title = title, | ||
Description = description, | ||
Authors = authors, | ||
IconUrl = iconUrl, | ||
ProjectUrl = projectUrl, | ||
Copyright = copyright, | ||
Tags = tags, | ||
ReadmeFile = readme, | ||
IconFile = icon, | ||
RepositoryMetadata = repositoryMetadata, | ||
DependencyGroups = dependencyGroups.ToArray(), | ||
}; | ||
|
||
return versionEntity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace PubNet.API.Services.Extensions; | ||
|
||
public static class StringExtensions | ||
{ | ||
public static string? ToNullIfEmpty(this string? value) | ||
{ | ||
return string.IsNullOrWhiteSpace(value) ? null : value; | ||
} | ||
} |
Oops, something went wrong.