2019-11-17 23:32:57 +00:00
|
|
|
using System.Collections.Generic;
|
2019-11-08 00:03:43 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using DiscImageChef.CommonTypes.Metadata;
|
|
|
|
|
using DiscImageChef.Server.Models;
|
2019-11-08 00:37:51 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2019-11-08 20:30:13 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2019-11-17 23:32:57 +00:00
|
|
|
using Newtonsoft.Json;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Server.Areas.Admin.Controllers
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
[Area("Admin"), Authorize]
|
2019-11-08 00:03:43 +00:00
|
|
|
public class UsbsController : Controller
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
readonly DicServerContext _context;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
2019-11-08 20:30:13 +00:00
|
|
|
public UsbsController(DicServerContext context) => _context = context;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Usbs
|
2019-11-17 22:56:11 +00:00
|
|
|
public async Task<IActionResult> Index() =>
|
|
|
|
|
View(await _context.Usb.OrderBy(u => u.Manufacturer).ThenBy(u => u.Product).ThenBy(u => u.VendorID).
|
|
|
|
|
ThenBy(u => u.ProductID).ToListAsync());
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Usbs/Details/5
|
|
|
|
|
public async Task<IActionResult> Details(int? id)
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
if(id == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 20:30:13 +00:00
|
|
|
Usb usb = await _context.Usb.FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(usb == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(usb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Usbs/Delete/5
|
|
|
|
|
public async Task<IActionResult> Delete(int? id)
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
if(id == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 20:30:13 +00:00
|
|
|
Usb usb = await _context.Usb.FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(usb == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(usb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: Admin/Usbs/Delete/5
|
2019-11-08 20:30:13 +00:00
|
|
|
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
2019-11-08 00:03:43 +00:00
|
|
|
public async Task<IActionResult> DeleteConfirmed(int id)
|
|
|
|
|
{
|
2019-11-08 20:30:13 +00:00
|
|
|
Usb usb = await _context.Usb.FindAsync(id);
|
2019-11-08 00:03:43 +00:00
|
|
|
_context.Usb.Remove(usb);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2019-11-08 20:30:13 +00:00
|
|
|
|
2019-11-08 00:03:43 +00:00
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-11-17 23:32:57 +00:00
|
|
|
|
|
|
|
|
public IActionResult Consolidate()
|
|
|
|
|
{
|
|
|
|
|
List<UsbModel> dups = _context.Usb.GroupBy(x => new
|
|
|
|
|
{
|
|
|
|
|
x.Manufacturer, x.Product, x.VendorID, x.ProductID
|
|
|
|
|
}).Where(x => x.Count() > 1).Select(x => new UsbModel
|
|
|
|
|
{
|
|
|
|
|
Manufacturer = x.Key.Manufacturer, Product = x.Key.Product, VendorID = x.Key.VendorID,
|
|
|
|
|
ProductID = x.Key.ProductID
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
return View(new UsbModelForView
|
|
|
|
|
{
|
|
|
|
|
List = dups, Json = JsonConvert.SerializeObject(dups)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
|
|
|
|
|
public IActionResult ConsolidateConfirmed(string models)
|
|
|
|
|
{
|
|
|
|
|
UsbModel[] duplicates;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
duplicates = JsonConvert.DeserializeObject<UsbModel[]>(models);
|
|
|
|
|
}
|
|
|
|
|
catch(JsonSerializationException)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(duplicates is null)
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
foreach(UsbModel duplicate in duplicates)
|
|
|
|
|
{
|
|
|
|
|
Usb master = _context.Usb.FirstOrDefault(m => m.Manufacturer == duplicate.Manufacturer &&
|
|
|
|
|
m.Product == duplicate.Product &&
|
|
|
|
|
m.VendorID == duplicate.VendorID &&
|
|
|
|
|
m.ProductID == duplicate.ProductID);
|
|
|
|
|
|
|
|
|
|
if(master is null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach(Usb slave in _context.Usb.Where(m => m.Manufacturer == duplicate.Manufacturer &&
|
|
|
|
|
m.Product == duplicate.Product &&
|
|
|
|
|
m.VendorID == duplicate.VendorID &&
|
|
|
|
|
m.ProductID == duplicate.ProductID).Skip(1).ToArray())
|
|
|
|
|
{
|
|
|
|
|
if(slave.Descriptors != null &&
|
|
|
|
|
master.Descriptors != null)
|
|
|
|
|
{
|
|
|
|
|
if(!master.Descriptors.SequenceEqual(slave.Descriptors))
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(Device device in _context.Devices.Where(d => d.USB.Id == slave.Id))
|
|
|
|
|
{
|
|
|
|
|
device.USB = master;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(UploadedReport report in _context.Reports.Where(d => d.USB.Id == slave.Id))
|
|
|
|
|
{
|
|
|
|
|
report.USB = master;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(master.Descriptors is null &&
|
|
|
|
|
slave.Descriptors != null)
|
|
|
|
|
{
|
|
|
|
|
master.Descriptors = slave.Descriptors;
|
|
|
|
|
_context.Usb.Update(master);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.Usb.Remove(slave);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-11-17 22:56:11 +00:00
|
|
|
}
|
2019-11-08 20:30:13 +00:00
|
|
|
}
|