mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Refactor and reorganize code.
This commit is contained in:
@@ -30,8 +30,8 @@
|
||||
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Marechai.Database.Models;
|
||||
using Marechai.Areas.Admin.Models;
|
||||
using Marechai.Database.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
@@ -40,42 +40,39 @@ using Microsoft.EntityFrameworkCore.Query;
|
||||
|
||||
namespace Marechai.Areas.Admin.Controllers
|
||||
{
|
||||
[Area("Admin")]
|
||||
[Authorize]
|
||||
[Area("Admin"), Authorize]
|
||||
public class ProcessorsController : Controller
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
|
||||
public ProcessorsController(MarechaiContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
public ProcessorsController(MarechaiContext context) => _context = context;
|
||||
|
||||
// GET: Admin/Processors
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
IIncludableQueryable<Processor, InstructionSet> marechaiContext =
|
||||
_context.Processors.Include(p => p.Company).Include(p => p.InstructionSet);
|
||||
return View(await marechaiContext.OrderBy(p => p.Company.Name).ThenBy(p => p.Name)
|
||||
.Select(p => new ProcessorViewModel
|
||||
{
|
||||
Company = p.Company.Name,
|
||||
Id = p.Id,
|
||||
InstructionSet = p.InstructionSet.Name,
|
||||
Introduced = p.Introduced,
|
||||
ModelCode = p.ModelCode,
|
||||
Name = p.Name
|
||||
}).ToListAsync());
|
||||
|
||||
return View(await marechaiContext.OrderBy(p => p.Company.Name).ThenBy(p => p.Name).
|
||||
Select(p => new ProcessorViewModel
|
||||
{
|
||||
Company = p.Company.Name, Id = p.Id,
|
||||
InstructionSet = p.InstructionSet.Name, Introduced = p.Introduced,
|
||||
ModelCode = p.ModelCode, Name = p.Name
|
||||
}).ToListAsync());
|
||||
}
|
||||
|
||||
// GET: Admin/Processors/Details/5
|
||||
public async Task<IActionResult> Details(int? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Processor processor = await _context.Processors.Include(p => p.Company).Include(p => p.InstructionSet)
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if(processor == null) return NotFound();
|
||||
Processor processor = await _context.Processors.Include(p => p.Company).Include(p => p.InstructionSet).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(processor == null)
|
||||
return NotFound();
|
||||
|
||||
return View(processor);
|
||||
}
|
||||
@@ -83,59 +80,66 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
// GET: Admin/Processors/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name");
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name");
|
||||
ViewData["InstructionSetId"] = new SelectList(_context.InstructionSets, "Id", "Name");
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Admin/Processors/Create
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create(
|
||||
[Bind(
|
||||
"Id,Name,CompanyId,ModelCode,Introduced,InstructionSetId,Speed,Package,Gprs,GprSize,Fprs,FprSize,Cores,ThreadsPerCore,Process,ProcessNm,DieSize,Transistors,DataBus,AddrBus,SimdRegisters,SimdSize,L1Instruction,L1Data,L2,L3")]
|
||||
[Bind("Id,Name,CompanyId,ModelCode,Introduced,InstructionSetId,Speed,Package,Gprs,GprSize,Fprs,FprSize,Cores,ThreadsPerCore,Process,ProcessNm,DieSize,Transistors,DataBus,AddrBus,SimdRegisters,SimdSize,L1Instruction,L1Data,L2,L3")]
|
||||
Processor processor)
|
||||
{
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
_context.Add(processor);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", processor.CompanyId);
|
||||
|
||||
ViewData["InstructionSetId"] =
|
||||
new SelectList(_context.InstructionSets, "Id", "Name", processor.InstructionSetId);
|
||||
|
||||
return View(processor);
|
||||
}
|
||||
|
||||
// GET: Admin/Processors/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Processor processor = await _context.Processors.FindAsync(id);
|
||||
if(processor == null) return NotFound();
|
||||
|
||||
if(processor == null)
|
||||
return NotFound();
|
||||
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", processor.CompanyId);
|
||||
|
||||
ViewData["InstructionSetId"] =
|
||||
new SelectList(_context.InstructionSets, "Id", "Name", processor.InstructionSetId);
|
||||
|
||||
return View(processor);
|
||||
}
|
||||
|
||||
// POST: Admin/Processors/Edit/5
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(
|
||||
int id, [Bind(
|
||||
"Id,Name,CompanyId,ModelCode,Introduced,InstructionSetId,Speed,Package,Gprs,GprSize,Fprs,FprSize,Cores,ThreadsPerCore,Process,ProcessNm,DieSize,Transistors,DataBus,AddrBus,SimdRegisters,SimdSize,L1Instruction,L1Data,L2,L3")]
|
||||
int id,
|
||||
[Bind("Id,Name,CompanyId,ModelCode,Introduced,InstructionSetId,Speed,Package,Gprs,GprSize,Fprs,FprSize,Cores,ThreadsPerCore,Process,ProcessNm,DieSize,Transistors,DataBus,AddrBus,SimdRegisters,SimdSize,L1Instruction,L1Data,L2,L3")]
|
||||
Processor processor)
|
||||
{
|
||||
if(id != processor.Id) return NotFound();
|
||||
if(id != processor.Id)
|
||||
return NotFound();
|
||||
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
@@ -146,7 +150,8 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
}
|
||||
catch(DbUpdateConcurrencyException)
|
||||
{
|
||||
if(!ProcessorExists(processor.Id)) return NotFound();
|
||||
if(!ProcessorExists(processor.Id))
|
||||
return NotFound();
|
||||
|
||||
throw;
|
||||
}
|
||||
@@ -155,38 +160,39 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
}
|
||||
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", processor.CompanyId);
|
||||
|
||||
ViewData["InstructionSetId"] =
|
||||
new SelectList(_context.InstructionSets, "Id", "Name", processor.InstructionSetId);
|
||||
|
||||
return View(processor);
|
||||
}
|
||||
|
||||
// GET: Admin/Processors/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Processor processor = await _context.Processors.Include(p => p.Company).Include(p => p.InstructionSet)
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if(processor == null) return NotFound();
|
||||
Processor processor = await _context.Processors.Include(p => p.Company).Include(p => p.InstructionSet).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(processor == null)
|
||||
return NotFound();
|
||||
|
||||
return View(processor);
|
||||
}
|
||||
|
||||
// POST: Admin/Processors/Delete/5
|
||||
[HttpPost]
|
||||
[ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
Processor processor = await _context.Processors.FindAsync(id);
|
||||
_context.Processors.Remove(processor);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
bool ProcessorExists(int id)
|
||||
{
|
||||
return _context.Processors.Any(e => e.Id == id);
|
||||
}
|
||||
bool ProcessorExists(int id) => _context.Processors.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user