refact
This commit is contained in:
parent
5de53a3cba
commit
476d35ae8a
270 changed files with 477 additions and 401 deletions
93
src/isnd/Controllers/PackageVersionController.cs
Normal file
93
src/isnd/Controllers/PackageVersionController.cs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using isn.Data;
|
||||
using isn.ViewModels;
|
||||
|
||||
namespace isn
|
||||
{
|
||||
[AllowAnonymous]
|
||||
public class PackageVersionController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public PackageVersionController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: PackageVersion
|
||||
public async Task<IActionResult> Index(PackageVersionIndexViewModel model)
|
||||
{
|
||||
var applicationDbContext = _context.PackageVersions.Include(p => p.Package).Where(p => p.PackageId == model.PackageId);
|
||||
|
||||
model.Versions = await applicationDbContext.ToListAsync();
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
// GET: PackageVersion/Details/5
|
||||
public async Task<IActionResult> 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<IActionResult> 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 (!IsOwner(packageVersion)) return Unauthorized();
|
||||
|
||||
return View(packageVersion);
|
||||
}
|
||||
|
||||
bool IsOwner(PackageVersion v)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
return v.Package.OwnerId == userId;
|
||||
}
|
||||
|
||||
// POST: PackageVersion/Delete/5
|
||||
[HttpPost, ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> 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 (!IsOwner(packageVersion)) return Unauthorized();
|
||||
|
||||
_context.PackageVersions.Remove(packageVersion);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue