using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using isnd.Data; using isnd.ViewModels; using isnd.Helpers; namespace isnd { [AllowAnonymous] public class PackageVersionController : Controller { private readonly ApplicationDbContext _context; public PackageVersionController(ApplicationDbContext context) { _context = context; } // GET: PackageVersion public async Task Index(PackageVersionIndexViewModel model) { var applicationDbContext = _context.PackageVersions.Include(p => p.Package).Where( p => ( model.Prerelease || !p.IsPrerelease) && ((model.PackageId == null) || p.PackageId.StartsWith(model.PackageId))); model.Versions = await applicationDbContext.ToArrayAsync(); return View(model); } [Authorize] public async Task Mines(PackageVersionIndexViewModel model) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var applicationDbContext = _context.PackageVersions .Include(p => p.Package).Where( p => (string.IsNullOrEmpty(model.PackageId) || p.PackageId.StartsWith(model.PackageId)) && p.Package.OwnerId == userId); model.Versions = await applicationDbContext.ToArrayAsync(); return View("Index", model); } // GET: PackageVersion/Details/5 public async Task Details(string pkgid, string version) { if (pkgid == null || version == null) { return NotFound(); } var packageVersion = await _context.PackageVersions .Include(p => p.Package) .FirstOrDefaultAsync(m => m.PackageId == pkgid && m.FullString == version); if (packageVersion == null) { return NotFound(); } return View(packageVersion); } [Authorize] public async Task Delete(string pkgid, string version) { if (pkgid == null || version == null) { return NotFound(); } var packageVersion = await _context.PackageVersions .Include(p => p.Package) .FirstOrDefaultAsync(m => m.PackageId == pkgid && m.FullString == version); if (packageVersion == null) { return NotFound(); } if (!User.IsOwner(packageVersion)) return Unauthorized(); return View(packageVersion); } // POST: PackageVersion/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(string PackageId, string FullString) { var packageVersion = await _context.PackageVersions.Include(p => p.Package) .FirstOrDefaultAsync(m => m.PackageId == PackageId && m.FullString == FullString); if (packageVersion == null) return NotFound(); if (!User.IsOwner(packageVersion)) return Unauthorized(); _context.PackageVersions.Remove(packageVersion); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } } }