From 3fcfced91872f7c61ddfada6b0932abd228f0090 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 10 Nov 2019 13:25:20 +0000 Subject: [PATCH] Add consolidation of CHS entities. --- .../Areas/Admin/Controllers/ChsController.cs | 71 ++++++++++++++++++- .../Areas/Admin/Views/Chs/Consolidate.cshtml | 31 ++++++++ .../Areas/Admin/Views/Chs/Index.cshtml | 3 + DiscImageChef.Server/Models/ChsModel.cs | 17 +++++ 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 DiscImageChef.Server/Areas/Admin/Views/Chs/Consolidate.cshtml create mode 100644 DiscImageChef.Server/Models/ChsModel.cs diff --git a/DiscImageChef.Server/Areas/Admin/Controllers/ChsController.cs b/DiscImageChef.Server/Areas/Admin/Controllers/ChsController.cs index 9fd3ea9f..965e3ed6 100644 --- a/DiscImageChef.Server/Areas/Admin/Controllers/ChsController.cs +++ b/DiscImageChef.Server/Areas/Admin/Controllers/ChsController.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 { @@ -16,7 +18,9 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers public ChsController(DicServerContext context) => _context = context; // GET: Admin/Chs - public async Task Index() => View(await _context.Chs.ToListAsync()); + public async Task Index() => + View(await _context.Chs.OrderBy(c => c.Cylinders).ThenBy(c => c.Heads).ThenBy(c => c.Sectors). + ToListAsync()); // GET: Admin/Chs/Delete/5 public async Task Delete(int? id) @@ -47,6 +51,69 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers return RedirectToAction(nameof(Index)); } - bool ChsExists(int id) => _context.Chs.Any(e => e.Id == id); + public IActionResult Consolidate() + { + List dups = _context.Chs.GroupBy(x => new + { + x.Cylinders, x.Heads, x.Sectors + }).Where(x => x.Count() > 1).Select(x => new ChsModel + { + Cylinders = x.Key.Cylinders, Heads = x.Key.Heads, Sectors = x.Key.Sectors + }).ToList(); + + return View(new ChsModelForView + { + List = dups, Json = JsonConvert.SerializeObject(dups) + }); + } + + [HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken] + public IActionResult ConsolidateConfirmed(string models) + { + ChsModel[] duplicates; + + try + { + duplicates = JsonConvert.DeserializeObject(models); + } + catch(JsonSerializationException) + { + return BadRequest(); + } + + if(duplicates is null) + return BadRequest(); + + foreach(ChsModel duplicate in duplicates) + { + Chs master = _context.Chs.FirstOrDefault(m => m.Cylinders == duplicate.Cylinders && + m.Heads == duplicate.Heads && + m.Sectors == duplicate.Sectors); + + if(master is null) + continue; + + foreach(Chs chs in _context.Chs.Where(m => m.Cylinders == duplicate.Cylinders && + m.Heads == duplicate.Heads && + m.Sectors == duplicate.Sectors).Skip(1).ToArray()) + { + foreach(TestedMedia media in _context.TestedMedia.Where(d => d.CHS.Id == chs.Id)) + { + media.CHS = master; + } + + foreach(TestedMedia media in _context.TestedMedia.Where(d => d.CurrentCHS.Id == chs.Id)) + { + media.CurrentCHS = master; + } + + _context.Chs.Remove(chs); + } + } + + _context.SaveChanges(); + + return RedirectToAction(nameof(Index)); + } } } \ No newline at end of file diff --git a/DiscImageChef.Server/Areas/Admin/Views/Chs/Consolidate.cshtml b/DiscImageChef.Server/Areas/Admin/Views/Chs/Consolidate.cshtml new file mode 100644 index 00000000..24769ff9 --- /dev/null +++ b/DiscImageChef.Server/Areas/Admin/Views/Chs/Consolidate.cshtml @@ -0,0 +1,31 @@ +@model ChsModelForView + +@{ + ViewBag.Title = "Consolidate duplicate CHS"; + Layout = "_Layout"; +} +

Consolidate duplicate CHS

+
+ The following CHS have duplicates. + + + + @foreach (var item in Model.List) + { + + + + } + +
+ @Html.DisplayFor(modelItem => item.Cylinders)/@Html.DisplayFor(modelItem => item.Heads)/@Html.DisplayFor(modelItem => item.Sectors) +
+
+
+ Do you want to remove the duplicates? +
+ + Back to List + +
+
\ No newline at end of file diff --git a/DiscImageChef.Server/Areas/Admin/Views/Chs/Index.cshtml b/DiscImageChef.Server/Areas/Admin/Views/Chs/Index.cshtml index 377cf19b..9ef54969 100644 --- a/DiscImageChef.Server/Areas/Admin/Views/Chs/Index.cshtml +++ b/DiscImageChef.Server/Areas/Admin/Views/Chs/Index.cshtml @@ -33,6 +33,9 @@ // Copyright © 2011-2019 Natalia Portillo // ****************************************************************************/ } + diff --git a/DiscImageChef.Server/Models/ChsModel.cs b/DiscImageChef.Server/Models/ChsModel.cs new file mode 100644 index 00000000..3b1fd102 --- /dev/null +++ b/DiscImageChef.Server/Models/ChsModel.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace DiscImageChef.Server.Models +{ + public class ChsModel + { + public ushort Cylinders { get; set; } + public ushort Heads { get; set; } + public ushort Sectors { get; set; } + } + + public class ChsModelForView + { + public List List { get; set; } + public string Json { get; set; } + } +} \ No newline at end of file