2019-11-17 20:46:37 +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 20:46:37 +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 SscsController : 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 SscsController(DicServerContext context) => _context = context;
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Sscs
|
2019-11-17 20:23:32 +00:00
|
|
|
public async Task<IActionResult> Index() =>
|
|
|
|
|
View(await _context.Ssc.OrderBy(s => s.MinBlockLength).ThenBy(s => s.MaxBlockLength).
|
|
|
|
|
ThenBy(s => s.BlockSizeGranularity).ToListAsync());
|
2019-11-08 00:03:43 +00:00
|
|
|
|
|
|
|
|
// GET: Admin/Sscs/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
|
|
|
Ssc ssc = await _context.Ssc.FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(ssc == null)
|
2019-11-08 00:03:43 +00:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(ssc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: Admin/Sscs/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
|
|
|
Ssc ssc = await _context.Ssc.FindAsync(id);
|
2019-11-08 00:03:43 +00:00
|
|
|
_context.Ssc.Remove(ssc);
|
|
|
|
|
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 20:46:37 +00:00
|
|
|
|
|
|
|
|
public IActionResult Consolidate()
|
|
|
|
|
{
|
|
|
|
|
List<SscModel> dups = _context.Ssc.GroupBy(x => new
|
|
|
|
|
{
|
|
|
|
|
x.BlockSizeGranularity, x.MaxBlockLength, x.MinBlockLength
|
|
|
|
|
}).Where(x => x.Count() > 1).Select(x => new SscModel
|
|
|
|
|
{
|
|
|
|
|
BlockSizeGranularity = x.Key.BlockSizeGranularity, MaxBlockLength = x.Key.MaxBlockLength,
|
|
|
|
|
MinBlockLength = x.Key.MinBlockLength
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
return View(new SscModelForView
|
|
|
|
|
{
|
|
|
|
|
List = dups, Json = JsonConvert.SerializeObject(dups)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("Consolidate"), ValidateAntiForgeryToken]
|
|
|
|
|
public IActionResult ConsolidateConfirmed(string models)
|
|
|
|
|
{
|
|
|
|
|
SscModel[] duplicates;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
duplicates = JsonConvert.DeserializeObject<SscModel[]>(models);
|
|
|
|
|
}
|
|
|
|
|
catch(JsonSerializationException)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(duplicates is null)
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
foreach(SscModel duplicate in duplicates)
|
|
|
|
|
{
|
|
|
|
|
Ssc master =
|
|
|
|
|
_context.Ssc.FirstOrDefault(m => m.BlockSizeGranularity == duplicate.BlockSizeGranularity &&
|
|
|
|
|
m.MaxBlockLength == duplicate.MaxBlockLength &&
|
|
|
|
|
m.MinBlockLength == duplicate.MinBlockLength);
|
|
|
|
|
|
|
|
|
|
if(master is null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach(Ssc ssc in _context.Ssc.Where(m => m.BlockSizeGranularity == duplicate.BlockSizeGranularity &&
|
|
|
|
|
m.MaxBlockLength == duplicate.MaxBlockLength &&
|
|
|
|
|
m.MinBlockLength == duplicate.MinBlockLength).Skip(1).
|
|
|
|
|
ToArray())
|
|
|
|
|
{
|
|
|
|
|
foreach(TestedSequentialMedia media in _context.TestedSequentialMedia.Where(d => d.SscId == ssc.Id))
|
|
|
|
|
{
|
|
|
|
|
media.SscId = master.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.Ssc.Update(ssc);
|
|
|
|
|
_context.Ssc.Remove(ssc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-11-08 00:03:43 +00:00
|
|
|
}
|
2019-11-08 20:30:13 +00:00
|
|
|
}
|