isn/src/isnd/Controllers/Packages/WebViews.cs

92 lines
3.1 KiB
C#
Raw Normal View History

2022-04-17 14:10:20 +01:00
using System;
2021-09-05 15:44:47 +01:00
using System.Linq;
using System.Threading.Tasks;
2022-06-19 21:40:31 +01:00
using isnd.Data;
using isnd.Helpers;
2021-09-05 15:44:47 +01:00
using isnd.ViewModels;
2022-06-19 21:40:31 +01:00
using Microsoft.AspNetCore.Authorization;
2021-09-05 15:44:47 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace isnd.Controllers
{
public partial class PackagesController
{
2022-04-17 16:22:40 +01:00
// Web search
2021-09-05 15:44:47 +01:00
public async Task<IActionResult> Index(PackageIndexViewModel model)
{
2022-04-18 20:58:46 +01:00
var applicationDbContext = dbContext.Packages.Include(p => p.Versions).Where(
2021-09-05 15:44:47 +01:00
p => ( model.Prerelease || p.Versions.Any(v => !v.IsPrerelease))
2021-09-12 20:45:43 +01:00
&& ((model.Query == null) || p.Id.StartsWith(model.Query)));
model.Data = await applicationDbContext.ToArrayAsync();
2021-09-05 15:44:47 +01:00
return View(model);
}
public async Task<IActionResult> Details(string pkgid)
{
if (pkgid == null)
{
return NotFound();
}
2022-04-18 20:58:46 +01:00
var packageVersion = dbContext.PackageVersions
2021-09-05 15:44:47 +01:00
.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(),
2022-06-18 15:44:06 +01:00
data = packageVersion.Take(MAX_PKG_VERSION_LIST).ToArray()
2021-09-05 15:44:47 +01:00
});
}
2022-06-18 15:44:06 +01:00
const int MAX_PKG_VERSION_LIST = 50;
2022-06-19 21:40:31 +01:00
[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));
}
2021-09-05 15:44:47 +01:00
}
}