95 lines
3.2 KiB
C#
95 lines
3.2 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using isnd.Data;
|
|
using isnd.Data.Catalog;
|
|
using isnd.Helpers;
|
|
using isnd.ViewModels;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace isnd.Controllers
|
|
{
|
|
|
|
public partial class PackagesController
|
|
{
|
|
// Web search
|
|
public async Task<IActionResult> Index(PackageRegistrationIndexViewModel model)
|
|
{
|
|
var applicationDbContext = dbContext.Packages.Include(p => p.Versions)
|
|
.Include(p => p.Owner)
|
|
.Where(
|
|
p => (model.Prerelease || p.Versions.Any(v => !v.IsPrerelease))
|
|
&& ((model.Query == null) || p.Id.StartsWith(model.Query)));
|
|
model.Data = await applicationDbContext.Select(p => p.ToLeave()).ToArrayAsync();
|
|
return View(model);
|
|
}
|
|
|
|
public async Task<IActionResult> Details(string pkgid)
|
|
{
|
|
if (pkgid == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var packageVersion = dbContext.PackageVersions
|
|
.Include(p => p.Package)
|
|
.Where(m => m.PackageId == pkgid)
|
|
.OrderByDescending(p => p)
|
|
;
|
|
|
|
if (packageVersion == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
bool results = await packageVersion.AnyAsync();
|
|
var latest = await packageVersion.FirstAsync();
|
|
|
|
return View("Details", new PackageDetailViewModel
|
|
{
|
|
latest = latest,
|
|
pkgid = pkgid,
|
|
totalHits = packageVersion.Count(),
|
|
data = packageVersion.Take(MAX_PKG_VERSION_LIST).ToArray()
|
|
});
|
|
|
|
}
|
|
const int MAX_PKG_VERSION_LIST = 50;
|
|
|
|
[Authorize]
|
|
public async Task<IActionResult> Delete(string pkgid, string version, string pkgtype)
|
|
{
|
|
if (pkgid == null || version == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var packageVersion = await dbContext.PackageVersions.Include(p => p.Package)
|
|
.FirstOrDefaultAsync(m => m.PackageId == pkgid
|
|
&& m.FullString == version && m.Type == pkgtype);
|
|
if (packageVersion == null) return NotFound();
|
|
if (!User.IsOwner(packageVersion)) return Unauthorized();
|
|
var pkg = await packageManager.GetPackageAsync(pkgid, version, pkgtype);
|
|
return View(pkg);
|
|
}
|
|
|
|
// POST: PackageVersion/Delete/5
|
|
[HttpPost, ActionName("Delete")]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> DeleteConfirmed(string PackageId, string FullString,
|
|
string Type)
|
|
{
|
|
PackageVersion packageVersion = await dbContext.PackageVersions
|
|
.Include(pv => pv.Package)
|
|
.FirstOrDefaultAsync(m => m.PackageId == PackageId
|
|
&& m.FullString == FullString && m.Type == Type);
|
|
if (packageVersion == null) return NotFound();
|
|
if (!User.IsOwner(packageVersion)) return Unauthorized();
|
|
|
|
await packageManager.DeletePackageAsync(PackageId, FullString, Type);
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
}
|
|
|
|
} |