Update only changed fields on editing.

This commit is contained in:
2019-12-27 17:13:10 +00:00
parent 55de4f24b5
commit 8dd0def757
7 changed files with 137 additions and 122 deletions

View File

@@ -43,30 +43,34 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Manufacturer,Model,Offset,Submissions,Agreement")]
CompactDiscOffset compactDiscOffset)
CompactDiscOffset changedModel)
{
if(id != compactDiscOffset.Id)
{
if(id != changedModel.Id)
return NotFound();
}
if(!ModelState.IsValid)
return View(compactDiscOffset);
return View(changedModel);
CompactDiscOffset model = await _context.CdOffsets.FirstOrDefaultAsync(m => m.Id == id);
if(model is null)
return NotFound();
model.Manufacturer = changedModel.Manufacturer;
model.Model = changedModel.Model;
model.Offset = changedModel.Offset;
model.Submissions = changedModel.Submissions;
model.Agreement = changedModel.Agreement;
model.ModifiedWhen = DateTime.UtcNow;
try
{
compactDiscOffset.ModifiedWhen = DateTime.UtcNow;
_context.Update(compactDiscOffset);
_context.Update(model);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!CompactDiscOffsetExists(compactDiscOffset.Id))
{
return NotFound();
}
throw;
ModelState.AddModelError("Concurrency", "Concurrency error, please report to the administrator.");
}
return RedirectToAction(nameof(Index));
@@ -101,8 +105,6 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
return RedirectToAction(nameof(Index));
}
bool CompactDiscOffsetExists(int id) => _context.CdOffsets.Any(e => e.Id == id);
public IActionResult Update() => throw new NotImplementedException();
}
}

View File

@@ -42,34 +42,35 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Manufacturer,Model,Revision,Bus")]
DeviceStat deviceStat)
DeviceStat changedModel)
{
if(id != deviceStat.Id)
{
if(id != changedModel.Id)
return NotFound();
}
if(ModelState.IsValid)
if(!ModelState.IsValid)
return View(changedModel);
DeviceStat model = await _context.DeviceStats.FirstOrDefaultAsync(m => m.Id == id);
if(model is null)
return NotFound();
model.Manufacturer = changedModel.Manufacturer;
model.Model = changedModel.Model;
model.Revision = changedModel.Revision;
model.Bus = changedModel.Bus;
try
{
try
{
_context.Update(deviceStat);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!DeviceStatExists(deviceStat.Id))
{
return NotFound();
}
throw;
}
return RedirectToAction(nameof(Index));
_context.Update(model);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
ModelState.AddModelError("Concurrency", "Concurrency error, please report to the administrator.");
}
return View(deviceStat);
return RedirectToAction(nameof(Index));
}
// GET: Admin/DeviceStats/Delete/5
@@ -100,7 +101,5 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
return RedirectToAction(nameof(Index));
}
bool DeviceStatExists(int id) => _context.DeviceStats.Any(e => e.Id == id);
}
}

View File

@@ -119,30 +119,35 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
int id, [Bind("OptimalMultipleSectorsRead,Id,CompactFlash,Manufacturer,Model,Revision,Type")]
Device device)
Device changedModel)
{
if(id != device.Id)
{
if(id != changedModel.Id)
return NotFound();
}
if(!ModelState.IsValid)
return View(device);
return View(changedModel);
Device model = await _context.Devices.FirstOrDefaultAsync(m => m.Id == id);
if(model is null)
return NotFound();
model.OptimalMultipleSectorsRead = changedModel.OptimalMultipleSectorsRead;
model.CompactFlash = changedModel.CompactFlash;
model.Manufacturer = changedModel.Manufacturer;
model.Model = changedModel.Model;
model.Revision = changedModel.Revision;
model.Type = changedModel.Type;
model.ModifiedWhen = DateTime.UtcNow;
try
{
device.ModifiedWhen = DateTime.UtcNow;
_context.Update(device);
_context.Update(model);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!DeviceExists(device.Id))
{
return NotFound();
}
throw;
ModelState.AddModelError("Concurrency", "Concurrency error, please report to the administrator.");
}
return RedirectToAction(nameof(Index));

View File

@@ -45,34 +45,36 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
int id, [Bind("Id,VendorID,ProductID,Manufacturer,Product,RemovableMedia")]
FireWire fireWire)
FireWire changedModel)
{
if(id != fireWire.Id)
{
if(id != changedModel.Id)
return NotFound();
}
if(ModelState.IsValid)
if(!ModelState.IsValid)
return View(changedModel);
FireWire model = await _context.FireWire.FirstOrDefaultAsync(m => m.Id == id);
if(model is null)
return NotFound();
model.VendorID = changedModel.VendorID;
model.ProductID = changedModel.ProductID;
model.Manufacturer = changedModel.Manufacturer;
model.Product = changedModel.Product;
model.RemovableMedia = changedModel.RemovableMedia;
try
{
try
{
_context.Update(fireWire);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!FireWireExists(fireWire.Id))
{
return NotFound();
}
throw;
}
return RedirectToAction(nameof(Index));
_context.Update(model);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
ModelState.AddModelError("Concurrency", "Concurrency error, please report to the administrator.");
}
return View(fireWire);
return RedirectToAction(nameof(Index));
}
// GET: Admin/FireWires/Delete/5

View File

@@ -106,29 +106,33 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,CompactFlash,Manufacturer,Model,Revision,Type")]
UploadedReport uploadedReport)
UploadedReport changedModel)
{
if(id != uploadedReport.Id)
{
if(id != changedModel.Id)
return NotFound();
}
if(!ModelState.IsValid)
return View(uploadedReport);
return View(changedModel);
UploadedReport model = await _context.Reports.FirstOrDefaultAsync(m => m.Id == id);
if(model is null)
return NotFound();
model.CompactFlash = changedModel.CompactFlash;
model.Manufacturer = changedModel.Manufacturer;
model.Model = changedModel.Model;
model.Revision = changedModel.Revision;
model.Type = changedModel.Type;
try
{
_context.Update(uploadedReport);
_context.Update(model);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!UploadedReportExists(uploadedReport.Id))
{
return NotFound();
}
throw;
ModelState.AddModelError("Concurrency", "Concurrency error, please report to the administrator.");
}
return RedirectToAction(nameof(Index));

View File

@@ -75,34 +75,37 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
int id, [Bind("Id,Blocks,BlockSize,LongBlockSize,Manufacturer,MediumTypeName,Model")]
TestedMedia testedMedia)
TestedMedia changedModel)
{
if(id != testedMedia.Id)
{
if(id != changedModel.Id)
return NotFound();
}
if(ModelState.IsValid)
if(!ModelState.IsValid)
return View(changedModel);
TestedMedia model = await _context.TestedMedia.FirstOrDefaultAsync(m => m.Id == id);
if(model is null)
return NotFound();
model.Blocks = changedModel.Blocks;
model.BlockSize = changedModel.BlockSize;
model.LongBlockSize = changedModel.LongBlockSize;
model.Manufacturer = changedModel.Manufacturer;
model.MediumTypeName = changedModel.MediumTypeName;
model.Model = changedModel.Model;
try
{
try
{
_context.Update(testedMedia);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!TestedMediaExists(testedMedia.Id))
{
return NotFound();
}
throw;
}
return RedirectToAction(nameof(Index));
_context.Update(model);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
ModelState.AddModelError("Concurrency", "Concurrency error, please report to the administrator.");
}
return View(testedMedia);
return RedirectToAction(nameof(Index));
}
// GET: Admin/TestedMedias/Delete/5

View File

@@ -43,29 +43,31 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Manufacturer,MediumTypeName,Model")]
TestedSequentialMedia testedSequentialMedia)
TestedSequentialMedia changedModel)
{
if(id != testedSequentialMedia.Id)
{
if(id != changedModel.Id)
return NotFound();
}
if(!ModelState.IsValid)
return View(testedSequentialMedia);
return View(changedModel);
TestedSequentialMedia model = await _context.TestedSequentialMedia.FirstOrDefaultAsync(m => m.Id == id);
if(model is null)
return NotFound();
model.Manufacturer = changedModel.Manufacturer;
model.MediumTypeName = changedModel.MediumTypeName;
model.Model = changedModel.Model;
try
{
_context.Update(testedSequentialMedia);
_context.Update(model);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!TestedSequentialMediaExists(testedSequentialMedia.Id))
{
return NotFound();
}
throw;
ModelState.AddModelError("Concurrency", "Concurrency error, please report to the administrator.");
}
return RedirectToAction(nameof(Index));
@@ -100,7 +102,5 @@ namespace DiscImageChef.Server.Areas.Admin.Controllers
return RedirectToAction(nameof(Index));
}
bool TestedSequentialMediaExists(int id) => _context.TestedSequentialMedia.Any(e => e.Id == id);
}
}