Add consolidation of CHS entities.

This commit is contained in:
2019-11-10 13:25:20 +00:00
parent 3e12a41a5f
commit 3fcfced918
4 changed files with 120 additions and 2 deletions

View File

@@ -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<IActionResult> Index() => View(await _context.Chs.ToListAsync());
public async Task<IActionResult> 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<IActionResult> 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<ChsModel> 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<ChsModel[]>(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));
}
}
}

View File

@@ -0,0 +1,31 @@
@model ChsModelForView
@{
ViewBag.Title = "Consolidate duplicate CHS";
Layout = "_Layout";
}
<h2>Consolidate duplicate CHS</h2>
<div>
The following CHS have duplicates.
<table class="table">
<thead>
<tbody>
@foreach (var item in Model.List)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Cylinders)/@Html.DisplayFor(modelItem => item.Heads)/@Html.DisplayFor(modelItem => item.Sectors)
</td>
</tr>
}
</tbody>
</table>
</div>
<div>
Do you want to remove the duplicates?
<form asp-action="Consolidate" enctype="multipart/form-data">
<input type="hidden" asp-for="Json" name="models" />
<a asp-action="Index" class="btn btn-primary">Back to List</a>
<input class="btn btn-danger" type="submit" value="Consolidate" />
</form>
</div>

View File

@@ -33,6 +33,9 @@
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
}
<div>
<a asp-action="Consolidate" class="btn btn-danger">Consolidate duplicates</a>
</div>
<table class="table">
<thead>
<tr>

View File

@@ -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<ChsModel> List { get; set; }
public string Json { get; set; }
}
}