Cascade delete archives when deleting games
This commit is contained in:
parent
8b17aca569
commit
260474aee8
1 changed files with 30 additions and 10 deletions
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -138,18 +139,37 @@ namespace LANCommander.Controllers
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> DeleteConfirmed(Guid id)
|
public async Task<IActionResult> DeleteConfirmed(Guid id)
|
||||||
{
|
{
|
||||||
if (Context.Games == null)
|
using (Repository<Game> repo = new Repository<Game>(Context, HttpContext))
|
||||||
{
|
{
|
||||||
return Problem("Entity set 'DatabaseContext.Games' is null.");
|
var game = await repo.Find(id);
|
||||||
}
|
|
||||||
var game = await Context.Games.FindAsync(id);
|
|
||||||
if (game != null)
|
|
||||||
{
|
|
||||||
Context.Games.Remove(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
await Context.SaveChangesAsync();
|
if (game == null)
|
||||||
return RedirectToAction(nameof(Index));
|
return NotFound();
|
||||||
|
|
||||||
|
if (game.Archives != null && game.Archives.Count > 0)
|
||||||
|
{
|
||||||
|
using (var archiveRepo = new Repository<Archive>(Context, HttpContext))
|
||||||
|
{
|
||||||
|
foreach (var archive in game.Archives.OrderByDescending(a => a.CreatedOn))
|
||||||
|
{
|
||||||
|
var archiveFile = Path.Combine("Upload", archive.ObjectKey);
|
||||||
|
|
||||||
|
if (System.IO.File.Exists(archiveFile))
|
||||||
|
System.IO.File.Delete(archiveFile);
|
||||||
|
|
||||||
|
archiveRepo.Delete(archive);
|
||||||
|
}
|
||||||
|
|
||||||
|
await archiveRepo.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repo.Delete(game);
|
||||||
|
|
||||||
|
await repo.SaveChanges();
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Index));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool GameExists(Guid id)
|
private bool GameExists(Guid id)
|
||||||
|
|
Loading…
Add table
Reference in a new issue