mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add code to consolidate FireWire.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DiscImageChef.CommonTypes.Metadata;
|
||||
@@ -6,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
|
||||
{
|
||||
@@ -105,6 +106,76 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
|
||||
|
||||
bool FireWireExists(int id) => _context.FireWire.Any(e => e.Id == id);
|
||||
|
||||
public IActionResult Consolidate() => throw new NotImplementedException();
|
||||
public IActionResult Consolidate()
|
||||
{
|
||||
List<FireWireModel> dups = _context.FireWire.GroupBy(x => new
|
||||
{
|
||||
x.VendorID, x.ProductID, x.Manufacturer, x.Product,
|
||||
x.RemovableMedia
|
||||
}).Where(x => x.Count() > 1).Select(x => new FireWireModel
|
||||
{
|
||||
VendorID = x.Key.VendorID, ProductID = x.Key.ProductID, Manufacturer = x.Key.Manufacturer,
|
||||
Product = x.Key.Product, RemovableMedia = x.Key.RemovableMedia
|
||||
}).ToList();
|
||||
|
||||
return View(new FireWireModelForView
|
||||
{
|
||||
List = dups, Json = JsonConvert.SerializeObject(dups)
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
|
||||
public IActionResult ConsolidateConfirmed(string models)
|
||||
{
|
||||
FireWireModel[] duplicates;
|
||||
|
||||
try
|
||||
{
|
||||
duplicates = JsonConvert.DeserializeObject<FireWireModel[]>(models);
|
||||
}
|
||||
catch(JsonSerializationException)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if(duplicates is null)
|
||||
return BadRequest();
|
||||
|
||||
foreach(FireWireModel duplicate in duplicates)
|
||||
{
|
||||
FireWire master = _context.FireWire.FirstOrDefault(m => m.VendorID == duplicate.VendorID &&
|
||||
m.ProductID == duplicate.ProductID &&
|
||||
m.Manufacturer == duplicate.Manufacturer &&
|
||||
m.Product == duplicate.Product &&
|
||||
m.RemovableMedia == duplicate.RemovableMedia);
|
||||
|
||||
if(master is null)
|
||||
continue;
|
||||
|
||||
foreach(FireWire firewire in _context.FireWire.Where(m => m.VendorID == duplicate.VendorID &&
|
||||
m.ProductID == duplicate.ProductID &&
|
||||
m.Manufacturer == duplicate.Manufacturer &&
|
||||
m.Product == duplicate.Product &&
|
||||
m.RemovableMedia == duplicate.RemovableMedia).
|
||||
Skip(1).ToArray())
|
||||
{
|
||||
foreach(Device device in _context.Devices.Where(d => d.FireWire.Id == firewire.Id))
|
||||
{
|
||||
device.FireWire = master;
|
||||
}
|
||||
|
||||
foreach(UploadedReport report in _context.Reports.Where(d => d.FireWire.Id == firewire.Id))
|
||||
{
|
||||
report.FireWire = master;
|
||||
}
|
||||
|
||||
_context.FireWire.Remove(firewire);
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
@model FireWireModelForView
|
||||
|
||||
@{
|
||||
ViewBag.Title = "Consolidate duplicate FireWire";
|
||||
Layout = "_Layout";
|
||||
}
|
||||
<h2>Consolidate duplicate FireWire</h2>
|
||||
<div>
|
||||
The following FireWire have duplicates.
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.List[0].Manufacturer)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.List[0].Product)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.List[0].VendorID)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.List[0].ProductID)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.List[0].RemovableMedia)
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in Model.List)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Manufacturer)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Product)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.VendorID)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.ProductID)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.RemovableMedia)
|
||||
</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>
|
||||
26
DiscImageChef.Server/Models/FireWireModel.cs
Normal file
26
DiscImageChef.Server/Models/FireWireModel.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DiscImageChef.Server.Models
|
||||
{
|
||||
public class FireWireModel
|
||||
{
|
||||
[DisplayName("Vendor ID"), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "0x{0:X8}")]
|
||||
public uint VendorID { get; set; }
|
||||
[DisplayName("Product ID"), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "0x{0:X8}")]
|
||||
public uint ProductID { get; set; }
|
||||
[DisplayFormat(NullDisplayText = "Unknown")]
|
||||
public string Manufacturer { get; set; }
|
||||
[DisplayFormat(NullDisplayText = "Unknown")]
|
||||
public string Product { get; set; }
|
||||
[DisplayName("Is media removable?")]
|
||||
public bool RemovableMedia { get; set; }
|
||||
}
|
||||
|
||||
public class FireWireModelForView
|
||||
{
|
||||
public List<FireWireModel> List { get; set; }
|
||||
public string Json { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user