Refactorisation of deletion
This commit is contained in:
@ -7,16 +7,21 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using isnd.Data;
|
using isnd.Data;
|
||||||
using isnd.ViewModels;
|
using isnd.ViewModels;
|
||||||
using isnd.Helpers;
|
using isnd.Helpers;
|
||||||
|
using isnd.Interfaces;
|
||||||
|
|
||||||
namespace isnd
|
namespace isnd
|
||||||
{
|
{
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
public class PackageVersionController : Controller
|
public class PackageVersionController : Controller
|
||||||
{
|
{
|
||||||
private readonly ApplicationDbContext _context;
|
private readonly ApplicationDbContext _context;
|
||||||
|
private readonly IPackageManager _pm;
|
||||||
|
|
||||||
public PackageVersionController(ApplicationDbContext context)
|
public PackageVersionController(ApplicationDbContext context,
|
||||||
|
IPackageManager pm)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
|
_pm = pm;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: PackageVersion
|
// GET: PackageVersion
|
||||||
@ -63,40 +68,39 @@ namespace isnd
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<IActionResult> Delete(string pkgid, string version)
|
public async Task<IActionResult> Delete(string pkgid, string version, string pkgtype)
|
||||||
{
|
{
|
||||||
if (pkgid == null || version == null)
|
if (pkgid == null || version == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
var packageVersion = await _context.PackageVersions
|
var packageVersion = await _context.PackageVersions.Include(p => p.Package)
|
||||||
.Include(p => p.Package)
|
.FirstOrDefaultAsync(m => m.PackageId == pkgid
|
||||||
.FirstOrDefaultAsync(m => m.PackageId == pkgid && m.FullString == version);
|
&& m.FullString == version && m.Type == pkgtype);
|
||||||
if (packageVersion == null)
|
if (packageVersion == null) return NotFound();
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!User.IsOwner(packageVersion)) return Unauthorized();
|
if (!User.IsOwner(packageVersion)) return Unauthorized();
|
||||||
|
var pkg = await _pm.GetPackageAsync(pkgid, version, pkgtype);
|
||||||
return View(packageVersion);
|
return View(pkg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// POST: PackageVersion/Delete/5
|
// POST: PackageVersion/Delete/5
|
||||||
[HttpPost, ActionName("Delete")]
|
[HttpPost, ActionName("Delete")]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> DeleteConfirmed(string PackageId, string FullString)
|
public async Task<IActionResult> DeleteConfirmed(string PackageId, string FullString,
|
||||||
|
string Type)
|
||||||
{
|
{
|
||||||
var packageVersion = await _context.PackageVersions.Include(p => p.Package)
|
PackageVersion packageVersion = await _context.PackageVersions.Include(p => p.Package)
|
||||||
.FirstOrDefaultAsync(m => m.PackageId == PackageId && m.FullString == FullString);
|
.FirstOrDefaultAsync(m => m.PackageId == PackageId
|
||||||
|
&& m.FullString == FullString && m.Type == Type);
|
||||||
if (packageVersion == null) return NotFound();
|
if (packageVersion == null) return NotFound();
|
||||||
if (!User.IsOwner(packageVersion)) return Unauthorized();
|
if (!User.IsOwner(packageVersion)) return Unauthorized();
|
||||||
|
|
||||||
_context.PackageVersions.Remove(packageVersion);
|
await _pm.DeletePackageAsync(PackageId, FullString, Type);
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
return RedirectToAction(nameof(Index));
|
return RedirectToAction(nameof(Index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using isnd.Controllers;
|
using isnd.Controllers;
|
||||||
|
using isnd.Data;
|
||||||
using isnd.Data.Catalog;
|
using isnd.Data.Catalog;
|
||||||
using isnd.Services;
|
using isnd.Services;
|
||||||
using isnd.ViewModels;
|
using isnd.ViewModels;
|
||||||
@ -19,6 +20,7 @@ namespace isnd.Interfaces
|
|||||||
IEnumerable<Resource> GetResources(IUnleash unleashĈlient);
|
IEnumerable<Resource> GetResources(IUnleash unleashĈlient);
|
||||||
void ÛpdateCatalogFor(Commit commit);
|
void ÛpdateCatalogFor(Commit commit);
|
||||||
Task<PackageDeletionReport> DeletePackageAsync(string pkgId, string fullVersion, string pkgType);
|
Task<PackageDeletionReport> DeletePackageAsync(string pkgId, string fullVersion, string pkgType);
|
||||||
|
Task<PackageVersion> GetPackageAsync(string pkgid, string version, string type);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -256,6 +256,7 @@ namespace isnd.Services
|
|||||||
Action = PackageAction.DeletePackage,
|
Action = PackageAction.DeletePackage,
|
||||||
TimeStamp = DateTime.Now
|
TimeStamp = DateTime.Now
|
||||||
};
|
};
|
||||||
|
dbContext.Commits.Add(commit);
|
||||||
var pkg = await dbContext.PackageVersions.SingleOrDefaultAsync(
|
var pkg = await dbContext.PackageVersions.SingleOrDefaultAsync(
|
||||||
v => v.PackageId == pkgId &&
|
v => v.PackageId == pkgId &&
|
||||||
v.FullString == fullVersion &&
|
v.FullString == fullVersion &&
|
||||||
@ -268,7 +269,7 @@ namespace isnd.Services
|
|||||||
dbContext.PackageVersions.Remove(pkg);
|
dbContext.PackageVersions.Remove(pkg);
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
ÛpdateCatalogFor(commit);
|
ÛpdateCatalogFor(commit);
|
||||||
return new PackageDeletionReport{ Deleted = true };
|
return new PackageDeletionReport{ Deleted = true, DeletedVersion = pkg };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,11 @@
|
|||||||
|
using isnd.Data;
|
||||||
|
|
||||||
namespace isnd.ViewModels
|
namespace isnd.ViewModels
|
||||||
{
|
{
|
||||||
public class PackageDeletionReport
|
public class PackageDeletionReport
|
||||||
{
|
{
|
||||||
public bool Deleted { get; set; }
|
public bool Deleted { get; set; }
|
||||||
|
|
||||||
|
public PackageVersion DeletedVersion { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -46,6 +46,7 @@
|
|||||||
<form asp-action="Delete">
|
<form asp-action="Delete">
|
||||||
<input type="hidden" asp-for="PackageId" />
|
<input type="hidden" asp-for="PackageId" />
|
||||||
<input type="hidden" asp-for="FullString" />
|
<input type="hidden" asp-for="FullString" />
|
||||||
|
<input type="hidden" asp-for="Type" />
|
||||||
<input type="submit" value="Delete" class="btn btn-default" /> |
|
<input type="submit" value="Delete" class="btn btn-default" /> |
|
||||||
<a asp-action="Index">Back to List</a>
|
<a asp-action="Index">Back to List</a>
|
||||||
</form>
|
</form>
|
||||||
|
@ -59,8 +59,8 @@
|
|||||||
@Html.DisplayFor(modelItem => item.IsPrerelease)
|
@Html.DisplayFor(modelItem => item.IsPrerelease)
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
@Html.ActionLink("Details", "Details", new { pkgid = item.PackageId, version = item.FullString }) |
|
@Html.ActionLink("Details", "Details", new { pkgid = item.PackageId, version = item.FullString, pkgtype = item.Type }) |
|
||||||
@Html.ActionLink("Delete", "Delete", new { pkgid = item.PackageId, version = item.FullString })
|
@Html.ActionLink("Delete", "Delete", new { pkgid = item.PackageId, version = item.FullString, pkgtype = item.Type })
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user