mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add consolidation for SSCs.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DiscImageChef.CommonTypes.Metadata;
|
||||
@@ -5,6 +6,7 @@ using DiscImageChef.Server.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DiscImageChef.Server.Areas.Admin.Controllers
|
||||
{
|
||||
@@ -48,5 +50,69 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
public IActionResult Consolidate()
|
||||
{
|
||||
List<SscModel> dups = _context.Ssc.GroupBy(x => new
|
||||
{
|
||||
x.BlockSizeGranularity, x.MaxBlockLength, x.MinBlockLength
|
||||
}).Where(x => x.Count() > 1).Select(x => new SscModel
|
||||
{
|
||||
BlockSizeGranularity = x.Key.BlockSizeGranularity, MaxBlockLength = x.Key.MaxBlockLength,
|
||||
MinBlockLength = x.Key.MinBlockLength
|
||||
}).ToList();
|
||||
|
||||
return View(new SscModelForView
|
||||
{
|
||||
List = dups, Json = JsonConvert.SerializeObject(dups)
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
|
||||
public IActionResult ConsolidateConfirmed(string models)
|
||||
{
|
||||
SscModel[] duplicates;
|
||||
|
||||
try
|
||||
{
|
||||
duplicates = JsonConvert.DeserializeObject<SscModel[]>(models);
|
||||
}
|
||||
catch(JsonSerializationException)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if(duplicates is null)
|
||||
return BadRequest();
|
||||
|
||||
foreach(SscModel duplicate in duplicates)
|
||||
{
|
||||
Ssc master =
|
||||
_context.Ssc.FirstOrDefault(m => m.BlockSizeGranularity == duplicate.BlockSizeGranularity &&
|
||||
m.MaxBlockLength == duplicate.MaxBlockLength &&
|
||||
m.MinBlockLength == duplicate.MinBlockLength);
|
||||
|
||||
if(master is null)
|
||||
continue;
|
||||
|
||||
foreach(Ssc ssc in _context.Ssc.Where(m => m.BlockSizeGranularity == duplicate.BlockSizeGranularity &&
|
||||
m.MaxBlockLength == duplicate.MaxBlockLength &&
|
||||
m.MinBlockLength == duplicate.MinBlockLength).Skip(1).
|
||||
ToArray())
|
||||
{
|
||||
foreach(TestedSequentialMedia media in _context.TestedSequentialMedia.Where(d => d.SscId == ssc.Id))
|
||||
{
|
||||
media.SscId = master.Id;
|
||||
}
|
||||
|
||||
_context.Ssc.Update(ssc);
|
||||
_context.Ssc.Remove(ssc);
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user