From 53b8163a43252b9c3a810f4ec01937e4ac15d89d Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 17 Nov 2019 20:46:37 +0000 Subject: [PATCH] Add consolidation for SSCs. --- DiscImageChef.CommonTypes | 2 +- .../Areas/Admin/Controllers/SscsController.cs | 66 +++++++++++++++++++ .../Areas/Admin/Views/Sscs/Consolidate.cshtml | 49 ++++++++++++++ .../Areas/Admin/Views/Sscs/Index.cshtml | 3 + DiscImageChef.Server/Models/SscModel.cs | 17 +++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 DiscImageChef.Server/Areas/Admin/Views/Sscs/Consolidate.cshtml create mode 100644 DiscImageChef.Server/Models/SscModel.cs diff --git a/DiscImageChef.CommonTypes b/DiscImageChef.CommonTypes index 40ce44b1..73e5c504 160000 --- a/DiscImageChef.CommonTypes +++ b/DiscImageChef.CommonTypes @@ -1 +1 @@ -Subproject commit 40ce44b1d4b9e66338c8715ab6c0f2660c33c634 +Subproject commit 73e5c50481ac71dd330c7a9d824c9597df178372 diff --git a/DiscImageChef.Server/Areas/Admin/Controllers/SscsController.cs b/DiscImageChef.Server/Areas/Admin/Controllers/SscsController.cs index 19d37d77..cb1cf6ee 100644 --- a/DiscImageChef.Server/Areas/Admin/Controllers/SscsController.cs +++ b/DiscImageChef.Server/Areas/Admin/Controllers/SscsController.cs @@ -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 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(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)); + } } } \ No newline at end of file diff --git a/DiscImageChef.Server/Areas/Admin/Views/Sscs/Consolidate.cshtml b/DiscImageChef.Server/Areas/Admin/Views/Sscs/Consolidate.cshtml new file mode 100644 index 00000000..cf6d9631 --- /dev/null +++ b/DiscImageChef.Server/Areas/Admin/Views/Sscs/Consolidate.cshtml @@ -0,0 +1,49 @@ +@model SscModelForView + +@{ + ViewBag.Title = "Consolidate duplicate CHS"; + Layout = "_Layout"; +} +

Consolidate duplicate SSC

+
+ The following SSC have duplicates. + + + + + + + + + + @foreach (var item in Model.List) + { + + + + + + } + +
+ @Html.DisplayNameFor(model => model.List[0].MinBlockLength) + + @Html.DisplayNameFor(model => model.List[0].MaxBlockLength) + + @Html.DisplayNameFor(model => model.List[0].BlockSizeGranularity) +
+ @Html.DisplayFor(modelItem => item.MinBlockLength) + + @Html.DisplayFor(modelItem => item.MaxBlockLength) + + @Html.DisplayFor(modelItem => item.BlockSizeGranularity) +
+
+
+ Do you want to remove the duplicates? +
+ + Back to List + +
+
\ No newline at end of file diff --git a/DiscImageChef.Server/Areas/Admin/Views/Sscs/Index.cshtml b/DiscImageChef.Server/Areas/Admin/Views/Sscs/Index.cshtml index 1be0cb11..0f502716 100644 --- a/DiscImageChef.Server/Areas/Admin/Views/Sscs/Index.cshtml +++ b/DiscImageChef.Server/Areas/Admin/Views/Sscs/Index.cshtml @@ -33,6 +33,9 @@ // Copyright © 2011-2019 Natalia Portillo // ****************************************************************************/ } +
+ Consolidate duplicates +
diff --git a/DiscImageChef.Server/Models/SscModel.cs b/DiscImageChef.Server/Models/SscModel.cs new file mode 100644 index 00000000..c4888036 --- /dev/null +++ b/DiscImageChef.Server/Models/SscModel.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace DiscImageChef.Server.Models +{ + public class SscModel + { + public byte? BlockSizeGranularity { get; set; } + public uint? MaxBlockLength { get; set; } + public uint? MinBlockLength { get; set; } + } + + public class SscModelForView + { + public List List { get; set; } + public string Json { get; set; } + } +} \ No newline at end of file