Add GPU creation in admin view.

This commit is contained in:
2020-05-27 22:59:03 +01:00
parent f9935de1b6
commit 1ae71d9d6c
7 changed files with 45 additions and 318 deletions

View File

@@ -1,187 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : GpusController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// GPUs 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.Linq;
using System.Threading.Tasks;
using Marechai.Areas.Admin.Models;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace Marechai.Areas.Admin.Controllers
{
[Area("Admin"), Authorize]
public class GpusController : Controller
{
readonly MarechaiContext _context;
public GpusController(MarechaiContext context) => _context = context;
// GET: Admin/Gpus
public async Task<IActionResult> Index()
{
IIncludableQueryable<Gpu, Company> marechaiContext = _context.Gpus.Include(g => g.Company);
return View(await marechaiContext.OrderBy(g => g.Company.Name).ThenBy(g => g.Name).
ThenBy(g => g.Introduced).Select(g => new GpuViewModel
{
Id = g.Id, Company = g.Company.Name,
Introduced = g.Introduced,
ModelCode = g.ModelCode, Name = g.Name
}).ToListAsync());
}
// GET: Admin/Gpus/Details/5
public async Task<IActionResult> Details(int? id)
{
if(id == null)
return NotFound();
Gpu gpu = await _context.Gpus.Include(g => g.Company).FirstOrDefaultAsync(m => m.Id == id);
if(gpu == null)
return NotFound();
return View(gpu);
}
// GET: Admin/Gpus/Create
public IActionResult Create()
{
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name");
return View();
}
// POST: Admin/Gpus/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,CompanyId,ModelCode,Introduced,Package,Process,ProcessNm,DieSize,Transistors")]
Gpu gpu)
{
if(ModelState.IsValid)
{
_context.Add(gpu);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", gpu.CompanyId);
return View(gpu);
}
// GET: Admin/Gpus/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null)
return NotFound();
Gpu gpu = await _context.Gpus.FindAsync(id);
if(gpu == null)
return NotFound();
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", gpu.CompanyId);
return View(gpu);
}
// POST: Admin/Gpus/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,CompanyId,ModelCode,Introduced,Package,Process,ProcessNm,DieSize,Transistors")]
Gpu gpu)
{
if(id != gpu.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(gpu);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!GpuExists(gpu.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", gpu.CompanyId);
return View(gpu);
}
// GET: Admin/Gpus/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null)
return NotFound();
Gpu gpu = await _context.Gpus.Include(g => g.Company).FirstOrDefaultAsync(m => m.Id == id);
if(gpu == null)
return NotFound();
return View(gpu);
}
// POST: Admin/Gpus/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
Gpu gpu = await _context.Gpus.FindAsync(id);
_context.Gpus.Remove(gpu);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool GpuExists(int id) => _context.Gpus.Any(e => e.Id == id);
}
}

View File

@@ -1,119 +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.Gpu
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>GPU</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="Company" class="control-label">
</label>
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
</select>
</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">
<label asp-for="ModelCode" class="control-label">
</label>
<input asp-for="ModelCode" class="form-control" />
<span asp-validation-for="ModelCode" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Introduced" class="control-label">
</label>
<input asp-for="Introduced" class="form-control" />
<span asp-validation-for="Introduced" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Package" class="control-label">
</label>
<input asp-for="Package" class="form-control" />
<span asp-validation-for="Package" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Process" class="control-label">
</label>
<input asp-for="Process" class="form-control" />
<span asp-validation-for="Process" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="ProcessNm" class="control-label">
</label>
<input asp-for="ProcessNm" class="form-control" />
<span asp-validation-for="ProcessNm" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="DieSize" class="control-label">
</label>
<input asp-for="DieSize" class="form-control" />
<span asp-validation-for="DieSize" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Transistors" class="control-label">
</label>
<input asp-for="Transistors" class="form-control" />
<span asp-validation-for="Transistors" 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"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1244</Version> <Version>3.0.99.1245</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>
@@ -131,5 +131,6 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentPeople\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentPeople\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Gpus\Create.cshtml" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -32,6 +32,7 @@
@page "/admin/gpus/details/{Id:int}" @page "/admin/gpus/details/{Id:int}"
@page "/admin/gpus/edit/{Id:int}" @page "/admin/gpus/edit/{Id:int}"
@page "/admin/gpus/create"
@inherits OwningComponentBase<GpusService> @inherits OwningComponentBase<GpusService>
@inject IStringLocalizer<GpusService> L @inject IStringLocalizer<GpusService> L
@inject CompaniesService CompaniesService @inject CompaniesService CompaniesService

View File

@@ -11,6 +11,7 @@ namespace Marechai.Pages.Admin.Details
public partial class Gpu public partial class Gpu
{ {
List<CompanyViewModel> _companies; List<CompanyViewModel> _companies;
bool _creating;
bool _editing; bool _editing;
bool _loaded; bool _loaded;
GpuViewModel _model; GpuViewModel _model;
@@ -33,13 +34,17 @@ namespace Marechai.Pages.Admin.Details
_loaded = true; _loaded = true;
if(Id <= 0) _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/gpus/create", StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
return; return;
_companies = await CompaniesService.GetAsync(); _companies = await CompaniesService.GetAsync();
_model = await Service.GetAsync(Id); _model = _creating ? new GpuViewModel() : await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/gpus/edit/", StringComparison.InvariantCulture); StartsWith("admin/gpus/edit/", StringComparison.InvariantCulture);
if(_editing) if(_editing)
@@ -71,6 +76,14 @@ namespace Marechai.Pages.Admin.Details
async void OnCancelClicked() async void OnCancelClicked()
{ {
_editing = false; _editing = false;
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/document_people");
return;
}
_model = await Service.GetAsync(Id); _model = await Service.GetAsync(Id);
SetCheckboxes(); SetCheckboxes();
StateHasChanged(); StateHasChanged();
@@ -120,8 +133,13 @@ namespace Marechai.Pages.Admin.Details
else if(_model.Transistors < 0) else if(_model.Transistors < 0)
return; return;
_editing = false; if(_creating)
Id = await Service.CreateAsync(_model);
else
await Service.UpdateAsync(_model); await Service.UpdateAsync(_model);
_editing = false;
_creating = false;
_model = await Service.GetAsync(Id); _model = await Service.GetAsync(Id);
SetCheckboxes(); SetCheckboxes();
StateHasChanged(); StateHasChanged();

View File

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

View File

@@ -59,6 +59,21 @@ namespace Marechai.Services
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
public async Task<int> CreateAsync(GpuViewModel viewModel)
{
var model = new Gpu
{
Name = viewModel.Name, CompanyId = viewModel.CompanyId, ModelCode = viewModel.ModelCode,
Introduced = viewModel.Introduced, Package = viewModel.Package, Process = viewModel.Process,
ProcessNm = viewModel.ProcessNm, DieSize = viewModel.DieSize, Transistors = viewModel.Transistors
};
await _context.Gpus.AddAsync(model);
await _context.SaveChangesAsync();
return model.Id;
}
public async Task DeleteAsync(int id) public async Task DeleteAsync(int id)
{ {
Gpu item = await _context.Gpus.FindAsync(id); Gpu item = await _context.Gpus.FindAsync(id);