Cleaned up ArchivesController

This commit is contained in:
Pat Hartl 2023-01-09 01:47:58 -06:00
parent bf8c7752cb
commit ed83489683

View file

@ -2,6 +2,7 @@
using LANCommander.Data.Models; using LANCommander.Data.Models;
using LANCommander.Extensions; using LANCommander.Extensions;
using LANCommander.Models; using LANCommander.Models;
using LANCommander.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.IO.Compression; using System.IO.Compression;
@ -14,32 +15,33 @@ namespace LANCommander.Controllers
public class ArchivesController : Controller public class ArchivesController : Controller
{ {
private readonly DatabaseContext Context; private readonly DatabaseContext Context;
private readonly GameService GameService;
private readonly ArchiveService ArchiveService;
public ArchivesController(DatabaseContext context) public ArchivesController(DatabaseContext context, GameService gameService, ArchiveService archiveService)
{ {
Context = context; Context = context;
GameService = gameService;
ArchiveService = archiveService;
} }
public async Task<IActionResult> Add(Guid? id) public async Task<IActionResult> Add(Guid? id)
{ {
if (id == null || Context.Games == null) if (id == null)
return NotFound(); return NotFound();
using (Repository<Game> repo = new Repository<Game>(Context, HttpContext)) var game = await GameService.Get(id.GetValueOrDefault());
Archive lastVersion = null;
if (game.Archives != null && game.Archives.Count > 0)
lastVersion = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault();
return View(new Archive()
{ {
var game = await repo.Find(id.GetValueOrDefault()); Game = game,
LastVersion = lastVersion,
Archive lastVersion = null; });
if (game.Archives != null && game.Archives.Count > 0)
lastVersion = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault();
return View(new Archive()
{
Game = game,
LastVersion = lastVersion,
});
}
} }
[HttpPost] [HttpPost]
@ -47,25 +49,18 @@ namespace LANCommander.Controllers
{ {
archive.Id = Guid.Empty; archive.Id = Guid.Empty;
using (Repository<Game> gameRepo = new Repository<Game>(Context, HttpContext)) var game = await GameService.Get(id.GetValueOrDefault());
archive.Game = game;
if (game.Archives != null && game.Archives.Any(a => a.Version == archive.Version))
ModelState.AddModelError("Version", "An archive for this game is already using that version.");
if (ModelState.IsValid)
{ {
var game = await gameRepo.Find(id.GetValueOrDefault()); await ArchiveService.Update(archive);
archive.Game = game; return RedirectToAction("Edit", "Games", new { id = id });
if (game.Archives != null && game.Archives.Any(a => a.Version == archive.Version))
ModelState.AddModelError("Version", "An archive for this game is already using that version.");
if (ModelState.IsValid)
{
using (Repository<Archive> archiveRepo = new Repository<Archive>(Context, HttpContext))
{
archive = await archiveRepo.Add(archive);
await archiveRepo.SaveChanges();
}
return RedirectToAction("Edit", "Games", new { id = id });
}
} }
return View(archive); return View(archive);
@ -73,39 +68,26 @@ namespace LANCommander.Controllers
public async Task<IActionResult> Download(Guid id) public async Task<IActionResult> Download(Guid id)
{ {
using (Repository<Archive> repo = new Repository<Archive>(Context, HttpContext)) var archive = await ArchiveService.Get(id);
{
var archive = await repo.Find(id);
var content = new FileStream(Path.Combine("Upload", archive.ObjectKey), FileMode.Open, FileAccess.Read, FileShare.Read); var content = new FileStream($"Upload/{archive.ObjectKey}".ToPath(), FileMode.Open, FileAccess.Read, FileShare.Read);
return File(content, "application/octet-stream", $"{archive.Game.Title.SanitizeFilename()}.zip"); return File(content, "application/octet-stream", $"{archive.Game.Title.SanitizeFilename()}.zip");
}
} }
public async Task<IActionResult> Delete(Guid? id) public async Task<IActionResult> Delete(Guid? id)
{ {
Guid gameId; var archive = await ArchiveService.Get(id.GetValueOrDefault());
var gameId = archive.Game.Id;
using (Repository<Archive> repo = new Repository<Archive>(Context, HttpContext)) await ArchiveService.Delete(archive);
{
var archive = await repo.Find(id.GetValueOrDefault());
gameId = archive.Game.Id;
System.IO.File.Delete(Path.Combine("Upload", archive.ObjectKey));
repo.Delete(archive);
await repo.SaveChanges();
}
return RedirectToAction("Edit", "Games", new { id = gameId }); return RedirectToAction("Edit", "Games", new { id = gameId });
} }
public async Task<IActionResult> Validate(Guid id, Archive archive) public async Task<IActionResult> Validate(Guid id, Archive archive)
{ {
var path = Path.Combine("Upload", id.ToString()); var path = $"Upload/{id}".ToPath();
string manifestContents = String.Empty; string manifestContents = String.Empty;
long compressedSize = 0; long compressedSize = 0;
@ -163,32 +145,24 @@ namespace LANCommander.Controllers
return BadRequest("The manifest file is invalid or corrupt."); return BadRequest("The manifest file is invalid or corrupt.");
} }
using (var repo = new Repository<Game>(Context, HttpContext)) var game = await GameService.Get(archive.Game.Id);
if (game == null)
return BadRequest("The related game is missing or corrupt.");
archive.Game = game;
archive.Id = Guid.Empty;
archive.CompressedSize = compressedSize;
archive.UncompressedSize = uncompressedSize;
archive.ObjectKey = id.ToString();
archive = await ArchiveService.Update(archive);
return Json(new
{ {
var game = await repo.Find(archive.Game.Id); Id = archive.Id,
ObjectKey = archive.ObjectKey,
if (game == null) });
return BadRequest("The related game is missing or corrupt.");
archive.Game = game;
}
using (var repo = new Repository<Archive>(Context, HttpContext))
{
archive.Id = Guid.Empty;
archive.CompressedSize = compressedSize;
archive.UncompressedSize = uncompressedSize;
archive.ObjectKey = id.ToString();
archive = await repo.Add(archive);
await repo.SaveChanges();
return Json(new
{
Id = archive.Id,
ObjectKey = archive.ObjectKey,
});
}
} }
} }
} }