mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add option to consolidate duplicate ATA entities.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DiscImageChef.Server.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DiscImageChef.Server.Areas.Admin.Controllers
|
||||
{
|
||||
@@ -36,7 +38,6 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
|
||||
return View(ata);
|
||||
}
|
||||
|
||||
|
||||
// GET: Admin/Atas/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
@@ -67,5 +68,77 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
|
||||
}
|
||||
|
||||
bool AtaExists(int id) => _context.Ata.Any(e => e.Id == id);
|
||||
|
||||
public IActionResult Consolidate()
|
||||
{
|
||||
List<IdHashModel> hashes =
|
||||
_context.Ata.Select(m => new IdHashModel(m.Id, Hash.Sha512(m.Identify))).ToList();
|
||||
|
||||
List<IdHashModel> dups = hashes.GroupBy(x => x.Hash).Where(g => g.Count() > 1).
|
||||
Select(x => hashes.FirstOrDefault(y => y.Hash == x.Key)).ToList();
|
||||
|
||||
for(int i = 0; i < dups.Count; i++)
|
||||
{
|
||||
CommonTypes.Metadata.Ata unique = _context.Ata.First(a => a.Id == dups[i].Id);
|
||||
|
||||
dups[i].Description = unique.IdentifyDevice?.Model;
|
||||
dups[i].Duplicates = hashes.Where(h => h.Hash == dups[i].Hash).Skip(1).Select(x => x.Id).ToArray();
|
||||
}
|
||||
|
||||
return View(new IdHashModelForView
|
||||
{
|
||||
List = dups, Json = JsonConvert.SerializeObject(dups)
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
|
||||
public IActionResult ConsolidateConfirmed(string models)
|
||||
{
|
||||
IdHashModel[] duplicates;
|
||||
|
||||
try
|
||||
{
|
||||
duplicates = JsonConvert.DeserializeObject<IdHashModel[]>(models);
|
||||
}
|
||||
catch(JsonSerializationException)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if(duplicates is null)
|
||||
return BadRequest();
|
||||
|
||||
foreach(IdHashModel duplicate in duplicates)
|
||||
{
|
||||
foreach(int duplicateId in duplicate.Duplicates)
|
||||
{
|
||||
foreach(Device ataDevice in _context.Devices.Where(d => d.ATAId == duplicateId))
|
||||
{
|
||||
ataDevice.ATAId = duplicate.Id;
|
||||
}
|
||||
|
||||
foreach(Device atapiDevice in _context.Devices.Where(d => d.ATAPIId == duplicateId))
|
||||
{
|
||||
atapiDevice.ATAPIId = duplicate.Id;
|
||||
}
|
||||
|
||||
foreach(UploadedReport ataReport in _context.Reports.Where(d => d.ATAId == duplicateId))
|
||||
{
|
||||
ataReport.ATAId = duplicate.Id;
|
||||
}
|
||||
|
||||
foreach(UploadedReport atapiReport in _context.Reports.Where(d => d.ATAPIId == duplicateId))
|
||||
{
|
||||
atapiReport.ATAPIId = duplicate.Id;
|
||||
}
|
||||
|
||||
_context.Ata.Remove(_context.Ata.First(d => d.Id == duplicateId));
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user