Add instruction set creation in admin view.

This commit is contained in:
2020-05-28 01:38:31 +01:00
parent 1ae71d9d6c
commit b61cf1459b
9 changed files with 48 additions and 473 deletions

View File

@@ -1,170 +0,0 @@
/******************************************************************************
// 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
using System;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Marechai.Areas.Admin.Controllers
{
[Area("Admin"), Authorize]
public class InstructionSetExtensionsController : Controller
{
readonly MarechaiContext _context;
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)
{
if(id == null)
return NotFound();
InstructionSetExtension instructionSetExtension =
await _context.InstructionSetExtensions.FirstOrDefaultAsync(m => m.Id == id);
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.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Extension")] InstructionSetExtension instructionSetExtension)
{
if(ModelState.IsValid)
{
_context.Add(instructionSetExtension);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(instructionSetExtension);
}
// GET: Admin/InstructionSetExtensions/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null)
return NotFound();
InstructionSetExtension instructionSetExtension = await _context.InstructionSetExtensions.FindAsync(id);
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.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
int id, [Bind("Id,Extension")] InstructionSetExtension instructionSetExtension)
{
if(id != instructionSetExtension.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(instructionSetExtension);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
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)
{
if(id == null)
return NotFound();
InstructionSetExtension instructionSetExtension =
await _context.InstructionSetExtensions.FirstOrDefaultAsync(m => m.Id == id);
if(instructionSetExtension == null)
return NotFound();
return View(instructionSetExtension);
}
// POST: Admin/InstructionSetExtensions/Delete/5
[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();
return RedirectToAction(nameof(Index));
}
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))
? Json("Instruction set extension already exists.") : Json(true);
}
}

View File

@@ -1,160 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : InstructionSetsController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Instruction sets 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
using System;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Marechai.Areas.Admin.Controllers
{
[Area("Admin"), Authorize]
public class InstructionSetsController : Controller
{
readonly MarechaiContext _context;
public InstructionSetsController(MarechaiContext context) => _context = context;
// GET: Admin/InstructionSets
public async Task<IActionResult> Index() =>
View(await _context.InstructionSets.OrderBy(s => s.Name).ToListAsync());
// GET: Admin/InstructionSets/Details/5
public async Task<IActionResult> Details(int? id)
{
if(id == null)
return NotFound();
InstructionSet instructionSet = await _context.InstructionSets.FirstOrDefaultAsync(m => m.Id == id);
if(instructionSet == null)
return NotFound();
return View(instructionSet);
}
// GET: Admin/InstructionSets/Create
public IActionResult Create() => View();
// POST: Admin/InstructionSets/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]
public async Task<IActionResult> Create([Bind("Id,Name")] InstructionSet instructionSet)
{
if(!ModelState.IsValid)
return View(instructionSet);
_context.Add(instructionSet);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
// GET: Admin/InstructionSets/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null)
return NotFound();
InstructionSet instructionSet = await _context.InstructionSets.FindAsync(id);
if(instructionSet == null)
return NotFound();
return View(instructionSet);
}
// POST: Admin/InstructionSets/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]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name")] InstructionSet instructionSet)
{
if(id != instructionSet.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(instructionSet);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!InstructionSetExists(instructionSet.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
return View(instructionSet);
}
// GET: Admin/InstructionSets/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null)
return NotFound();
InstructionSet instructionSet = await _context.InstructionSets.FirstOrDefaultAsync(m => m.Id == id);
if(instructionSet == null)
return NotFound();
return View(instructionSet);
}
// POST: Admin/InstructionSets/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
InstructionSet instructionSet = await _context.InstructionSets.FindAsync(id);
_context.InstructionSets.Remove(instructionSet);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool InstructionSetExists(int id) => _context.InstructionSets.Any(e => e.Id == id);
[AcceptVerbs("Get", "Post")]
public IActionResult VerifyUnique(string name) =>
_context.InstructionSets.Any(i => string.Equals(i.Name, name, StringComparison.InvariantCultureIgnoreCase))
? Json("Instruction set already exists.") : Json(true);
}
}

View File

@@ -1,64 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Create.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.InstructionSetExtension
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>Instruction set extension</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<div class="form-group">
<label asp-for="Extension" class="control-label">
</label>
<input asp-for="Extension" class="form-control" />
<span asp-validation-for="Extension" class="text-danger">
</span>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Create" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -1,64 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Create.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.InstructionSet
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>Instruction set</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<div class="form-group">
<label asp-for="Name" class="control-label">
</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger">
</span>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Create" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1245</Version>
<Version>3.0.99.1246</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>
@@ -132,5 +132,7 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentPeople\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Gpus\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSetExtensions\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSets\Create.cshtml" />
</ItemGroup>
</Project>

View File

@@ -32,6 +32,7 @@
@page "/admin/instruction_sets/details/{Id:int}"
@page "/admin/instruction_sets/edit/{Id:int}"
@page "/admin/instruction_sets/create"
@inherits OwningComponentBase<InstructionSetsService>
@inject IStringLocalizer<InstructionSetsService> L
@attribute [Authorize(Roles = "UberAdmin, Admin")]

View File

@@ -8,8 +8,9 @@ namespace Marechai.Pages.Admin.Details
{
public partial class InstructionSet
{
bool _editing;
bool _loaded;
bool _creating;
bool _editing;
bool _loaded;
Database.Models.InstructionSet _model;
[Parameter]
public int Id { get; set; }
@@ -21,14 +22,19 @@ namespace Marechai.Pages.Admin.Details
_loaded = true;
if(Id <= 0)
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/instruction_sets/create",
StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
return;
_model = await Service.GetAsync(Id);
_model = _creating ? new Database.Models.InstructionSet() : await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/instruction_sets/edit/",
StringComparison.InvariantCulture);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/instruction_sets/edit/",
StringComparison.InvariantCulture);
StateHasChanged();
}
@@ -42,7 +48,15 @@ namespace Marechai.Pages.Admin.Details
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/instruction_sets");
return;
}
_model = await Service.GetAsync(Id);
StateHasChanged();
}
@@ -53,9 +67,14 @@ namespace Marechai.Pages.Admin.Details
!Service.VerifyUnique(_model.Name))
return;
_editing = false;
await Service.UpdateAsync(_model);
_model = await Service.GetAsync(Id);
if(_creating)
Id = await Service.CreateAsync(_model);
else
await Service.UpdateAsync(_model);
_editing = false;
_creating = false;
_model = await Service.GetAsync(Id);
StateHasChanged();
}

View File

@@ -42,9 +42,7 @@
return;
}
<p>
<span class="btn btn-primary">
@L["Create new"]
</span>
<a class="btn btn-primary" href="/admin/instruction_sets/create">@L["Create new"]</a>
</p>
<table class="table table-striped">
<thead>

View File

@@ -37,6 +37,19 @@ namespace Marechai.Services
await _context.SaveChangesAsync();
}
public async Task<int> CreateAsync(InstructionSet viewModel)
{
var model = new InstructionSet
{
Name = viewModel.Name
};
await _context.InstructionSets.AddAsync(model);
await _context.SaveChangesAsync();
return model.Id;
}
public async Task DeleteAsync(int id)
{
InstructionSet item = await _context.InstructionSets.FindAsync(id);