Files
marechai/Marechai/Areas/Admin/Controllers/InstructionSetExtensionsController.cs

170 lines
6.7 KiB
C#
Raw Normal View History

/******************************************************************************
2020-02-10 01:52:56 +00:00
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : InstructionSetExtensionsController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Instruction set extensions admin controller
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2020-02-10 03:05:39 +00:00
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
using System;
using System.Linq;
using System.Threading.Tasks;
2020-02-10 02:10:18 +00:00
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
{
2020-02-10 22:44:18 +00:00
[Area("Admin"), Authorize]
public class InstructionSetExtensionsController : Controller
{
2020-02-10 02:47:39 +00:00
readonly MarechaiContext _context;
2020-02-10 22:44:18 +00:00
public InstructionSetExtensionsController(MarechaiContext context) => _context = context;
// GET: Admin/InstructionSetExtensions
public async Task<IActionResult> Index() =>
View(await _context.InstructionSetExtensions.OrderBy(e => e.Extension).ToListAsync());
// GET: Admin/InstructionSetExtensions/Details/5
public async Task<IActionResult> Details(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
InstructionSetExtension instructionSetExtension =
await _context.InstructionSetExtensions.FirstOrDefaultAsync(m => m.Id == id);
2020-02-10 22:44:18 +00:00
if(instructionSetExtension == null)
return NotFound();
ViewBag.Processors = _context.InstructionSetExtensionsByProcessor.Where(e => e.ExtensionId == id).
Join(_context.Processors, p => p.ProcessorId, i => i.Id, (p, i) => i).
Select(p => p.Name);
return View(instructionSetExtension);
}
// GET: Admin/InstructionSetExtensions/Create
public IActionResult Create() => View();
// POST: Admin/InstructionSetExtensions/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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Extension")] InstructionSetExtension instructionSetExtension)
{
if(ModelState.IsValid)
{
_context.Add(instructionSetExtension);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
return RedirectToAction(nameof(Index));
}
return View(instructionSetExtension);
}
// GET: Admin/InstructionSetExtensions/Edit/5
public async Task<IActionResult> Edit(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
InstructionSetExtension instructionSetExtension = await _context.InstructionSetExtensions.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(instructionSetExtension == null)
return NotFound();
return View(instructionSetExtension);
}
// POST: Admin/InstructionSetExtensions/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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
int id, [Bind("Id,Extension")] InstructionSetExtension instructionSetExtension)
{
2020-02-10 22:44:18 +00:00
if(id != instructionSetExtension.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(instructionSetExtension);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
2020-02-10 22:44:18 +00:00
if(!InstructionSetExtensionExists(instructionSetExtension.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
return View(instructionSetExtension);
}
// GET: Admin/InstructionSetExtensions/Delete/5
public async Task<IActionResult> Delete(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
InstructionSetExtension instructionSetExtension =
await _context.InstructionSetExtensions.FirstOrDefaultAsync(m => m.Id == id);
2020-02-10 22:44:18 +00:00
if(instructionSetExtension == null)
return NotFound();
return View(instructionSetExtension);
}
// POST: Admin/InstructionSetExtensions/Delete/5
2020-02-10 22:44:18 +00:00
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
InstructionSetExtension instructionSetExtension = await _context.InstructionSetExtensions.FindAsync(id);
_context.InstructionSetExtensions.Remove(instructionSetExtension);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
bool InstructionSetExtensionExists(int id) => _context.InstructionSetExtensions.Any(e => e.Id == id);
[AcceptVerbs("Get", "Post")]
public IActionResult VerifyUnique(string extension) =>
_context.InstructionSetExtensions.Any(i => string.Equals(i.Extension, extension,
StringComparison.InvariantCultureIgnoreCase))
2020-02-10 22:44:18 +00:00
? Json("Instruction set extension already exists.") : Json(true);
}
}