diff --git a/Marechai/Areas/Admin/Controllers/GpusController.cs b/Marechai/Areas/Admin/Controllers/GpusController.cs deleted file mode 100644 index b4a998a5..00000000 --- a/Marechai/Areas/Admin/Controllers/GpusController.cs +++ /dev/null @@ -1,187 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : GpusController.cs -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// 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 Index() - { - IIncludableQueryable 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 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 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 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 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 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 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); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/Gpus/Create.cshtml b/Marechai/Areas/Admin/Views/Gpus/Create.cshtml deleted file mode 100644 index 3af218b4..00000000 --- a/Marechai/Areas/Admin/Views/Gpus/Create.cshtml +++ /dev/null @@ -1,119 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Create.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model Marechai.Database.Models.Gpu - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

GPU

-
-
-
-
-
-
-
- - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 8fabbefe..d2ec2bb9 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1244 + 3.0.99.1245 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website @@ -131,5 +131,6 @@ <_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentPeople\Create.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\Gpus\Create.cshtml" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Gpu.razor b/Marechai/Pages/Admin/Details/Gpu.razor index bf7a2c9e..b3537644 100644 --- a/Marechai/Pages/Admin/Details/Gpu.razor +++ b/Marechai/Pages/Admin/Details/Gpu.razor @@ -32,6 +32,7 @@ @page "/admin/gpus/details/{Id:int}" @page "/admin/gpus/edit/{Id:int}" +@page "/admin/gpus/create" @inherits OwningComponentBase @inject IStringLocalizer L @inject CompaniesService CompaniesService diff --git a/Marechai/Pages/Admin/Details/Gpu.razor.cs b/Marechai/Pages/Admin/Details/Gpu.razor.cs index 379fea5c..235960a6 100644 --- a/Marechai/Pages/Admin/Details/Gpu.razor.cs +++ b/Marechai/Pages/Admin/Details/Gpu.razor.cs @@ -11,6 +11,7 @@ namespace Marechai.Pages.Admin.Details public partial class Gpu { List _companies; + bool _creating; bool _editing; bool _loaded; GpuViewModel _model; @@ -33,14 +34,18 @@ namespace Marechai.Pages.Admin.Details _loaded = true; - if(Id <= 0) + _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/gpus/create", StringComparison.InvariantCulture); + + if(Id <= 0 && + !_creating) return; _companies = await CompaniesService.GetAsync(); - _model = await Service.GetAsync(Id); + _model = _creating ? new GpuViewModel() : await Service.GetAsync(Id); - _editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). - StartsWith("admin/gpus/edit/", StringComparison.InvariantCulture); + _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/gpus/edit/", StringComparison.InvariantCulture); if(_editing) SetCheckboxes(); @@ -71,7 +76,15 @@ namespace Marechai.Pages.Admin.Details async void OnCancelClicked() { _editing = false; - _model = await Service.GetAsync(Id); + + if(_creating) + { + NavigationManager.ToBaseRelativePath("admin/document_people"); + + return; + } + + _model = await Service.GetAsync(Id); SetCheckboxes(); StateHasChanged(); } @@ -120,9 +133,14 @@ namespace Marechai.Pages.Admin.Details else if(_model.Transistors < 0) 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); SetCheckboxes(); StateHasChanged(); } diff --git a/Marechai/Pages/Admin/Gpus.razor b/Marechai/Pages/Admin/Gpus.razor index 9c143a99..36846506 100644 --- a/Marechai/Pages/Admin/Gpus.razor +++ b/Marechai/Pages/Admin/Gpus.razor @@ -42,9 +42,7 @@ return; }

- - @L["Create new"] - + @L["Create new"]

diff --git a/Marechai/Services/GpusService.cs b/Marechai/Services/GpusService.cs index eca8b3d5..6da16eb4 100644 --- a/Marechai/Services/GpusService.cs +++ b/Marechai/Services/GpusService.cs @@ -59,6 +59,21 @@ namespace Marechai.Services await _context.SaveChangesAsync(); } + public async Task 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) { Gpu item = await _context.Gpus.FindAsync(id);