Allow to add and remove GPUs from machine details in admin view.

This commit is contained in:
2020-05-29 02:13:46 +01:00
parent c922cbcdd1
commit 7cf5058f9b
12 changed files with 262 additions and 339 deletions

View File

@@ -1,163 +0,0 @@
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 GpusByMachineController : Controller
{
readonly MarechaiContext _context;
public GpusByMachineController(MarechaiContext context) => _context = context;
// GET: GpusByMachine
public async Task<IActionResult> Index()
{
IIncludableQueryable<GpusByMachine, Machine> marechaiContext =
_context.GpusByMachine.Include(g => g.Gpu).Include(g => g.Machine);
return View(await marechaiContext.OrderBy(g => g.Machine.Name).ThenBy(g => g.Gpu.Name).
Select(g => new GpusByMachineViewModel
{
Id = g.Id, Gpu = g.Gpu.Name, Machine = g.Machine.Name
}).ToListAsync());
}
// GET: GpusByMachine/Details/5
public async Task<IActionResult> Details(long? id)
{
if(id == null)
return NotFound();
GpusByMachine gpusByMachine = await _context.
GpusByMachine.Include(g => g.Gpu).Include(g => g.Machine).
FirstOrDefaultAsync(m => m.Id == id);
if(gpusByMachine == null)
return NotFound();
return View(gpusByMachine);
}
// GET: GpusByMachine/Create
public IActionResult Create()
{
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name");
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name");
return View();
}
// POST: GpusByMachine/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("GpuId,MachineId,Id")] GpusByMachine gpusByMachine)
{
if(ModelState.IsValid)
{
_context.Add(gpusByMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByMachine.GpuId);
ViewData["MachineId"] =
new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name", gpusByMachine.MachineId);
return View(gpusByMachine);
}
// GET: GpusByMachine/Edit/5
public async Task<IActionResult> Edit(long? id)
{
if(id == null)
return NotFound();
GpusByMachine gpusByMachine = await _context.GpusByMachine.FindAsync(id);
if(gpusByMachine == null)
return NotFound();
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByMachine.GpuId);
ViewData["MachineId"] =
new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name", gpusByMachine.MachineId);
return View(gpusByMachine);
}
// POST: GpusByMachine/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(long id, [Bind("GpuId,MachineId,Id")] GpusByMachine gpusByMachine)
{
if(id != gpusByMachine.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(gpusByMachine);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!GpusByMachineExists(gpusByMachine.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByMachine.GpuId);
ViewData["MachineId"] =
new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name", gpusByMachine.MachineId);
return View(gpusByMachine);
}
// GET: GpusByMachine/Delete/5
public async Task<IActionResult> Delete(long? id)
{
if(id == null)
return NotFound();
GpusByMachine gpusByMachine = await _context.
GpusByMachine.Include(g => g.Gpu).Include(g => g.Machine).
FirstOrDefaultAsync(m => m.Id == id);
if(gpusByMachine == null)
return NotFound();
return View(gpusByMachine);
}
// POST: GpusByMachine/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(long id)
{
GpusByMachine gpusByMachine = await _context.GpusByMachine.FindAsync(id);
_context.GpusByMachine.Remove(gpusByMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool GpusByMachineExists(long id) => _context.GpusByMachine.Any(e => e.Id == id);
}
}

View File

@@ -1,27 +0,0 @@
@model Marechai.Database.Models.GpusByMachine
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>GPU by machine</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="Machine" class="control-label"></label>
<select asp-for="MachineId" class="form-control" asp-items="ViewBag.MachineId"></select>
</div>
<div class="form-group">
<label asp-for="Gpu" class="control-label"></label>
<select asp-for="GpuId" class="form-control" asp-items="ViewBag.GpuId"></select>
</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>

View File

@@ -1,30 +0,0 @@
@model Marechai.Database.Models.GpusByMachine
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>GPU by machine</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Machine)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Machine.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Gpu)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Gpu.Name)
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input class="btn btn-danger" type="submit" value="Delete" />
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</form>
</div>

View File

@@ -1,28 +0,0 @@
@model Marechai.Database.Models.GpusByMachine
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>GPU by machine</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Machine)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Machine.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Gpu)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Gpu.Name)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id" class="btn btn-primary">Edit</a>
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</div>

View File

@@ -1,30 +0,0 @@
@model Marechai.Database.Models.GpusByMachine
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>GpusByMachine</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Machine" class="control-label"></label>
<select asp-for="MachineId" class="form-control" asp-items="ViewBag.MachineId"></select>
<span asp-validation-for="MachineId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Gpu" class="control-label"></label>
<select asp-for="GpuId" class="form-control" asp-items="ViewBag.GpuId"></select>
<span asp-validation-for="GpuId" class="text-danger"></span>
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Save" />
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</div>
</form>
</div>
</div>

View File

@@ -1,48 +0,0 @@
@model IEnumerable<Marechai.Areas.Admin.Models.GpusByMachineViewModel>
@{
ViewData["Title"] = "Index";
}
<h1>GPUs by machine</h1>
<p>
<a asp-action="Create" class="btn btn-primary">
Create new
</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Machine)
</th>
<th>
@Html.DisplayNameFor(model => model.Gpu)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Machine)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gpu)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary">
Details
</a>
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-secondary">
Edit
</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">
Delete
</a>
</td>
</tr>
}
</tbody>
</table>

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.1364</Version> <Version>3.0.99.1373</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>
@@ -144,5 +144,10 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Screens\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\Screens\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundSynths\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundSynths\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\CompanyLogos\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\CompanyLogos\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Delete.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Index.cshtml" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -39,6 +39,8 @@
@inject CompaniesService CompaniesService @inject CompaniesService CompaniesService
@inject MachineFamiliesService MachineFamiliesService @inject MachineFamiliesService MachineFamiliesService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject GpuByMachineService GpuByMachineService
@inject GpusService GpusService
@attribute [Authorize(Roles = "UberAdmin, Admin")] @attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Machine details"]</h3> <h3>@L["Machine details"]</h3>
<hr /> <hr />
@@ -154,4 +156,77 @@
<Button Color="Color.Danger" Clicked="@OnCancelClicked">@L["Cancel"]</Button> <Button Color="Color.Danger" Clicked="@OnCancelClicked">@L["Cancel"]</Button>
} }
<a href="/admin/machines" class="btn btn-secondary">@L["Back to list"]</a> <a href="/admin/machines" class="btn btn-secondary">@L["Back to list"]</a>
</div> </div>
@if (!_editing)
{
<hr />
<h3>@L["Graphical processing units belonging to this machine"]</h3>
<Button Color="Color.Success" Clicked="OnAddGpuClick" Disabled="_addingGpu">@L["Add new (gpu by machine)"]</Button>
@if (_addingGpu)
{
<div>
<Field>
<FieldLabel>@L["Graphical processing units"]</FieldLabel>
<Select Disabled="_savingGpu" TValue="int?" @bind-SelectedValue="@_addingGpuId">
@foreach (var gpu in _gpus)
{
<SelectItem TValue="int?" Value="@gpu.Id">@gpu.Company - @gpu.Name</SelectItem>
}
</Select>
<Button Color="Color.Primary" Clicked="@CancelAddGpu" Disabled="@_savingGpu">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddGpu" Disabled="@_savingGpu">@L["Add"]</Button>
</Field>
</div>
}
@if (_machineGpus?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Company"]
</th>
<th>
@L["Name"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _machineGpus)
{
<tr>
<td>
@item.CompanyName
</td>
<td>
@item.Name
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowGpuDeleteModal(item.Id);}" Disabled="@_addingGpu">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop />
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@_deleteTitle</ModalTitle>
<CloseButton Clicked="@HideModal" />
</ModalHeader>
<ModalBody>
<Text>@_deleteText</Text>
</ModalBody>
<ModalFooter>
<Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
<Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
</ModalFooter>
</ModalContent>
</Modal>
}

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Blazorise; using Blazorise;
using Marechai.Database; using Marechai.Database;
@@ -11,16 +12,30 @@ namespace Marechai.Pages.Admin.Details
{ {
public partial class Machine public partial class Machine
{ {
List<CompanyViewModel> _companies; bool _addingGpu;
bool _creating; int? _addingGpuId;
List<CompanyViewModel> _companies;
bool _creating;
GpuByMachineViewModel _currentGpuByMachine;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingGpuByMachine;
bool _editing; bool _editing;
List<MachineFamilyViewModel> _families; List<MachineFamilyViewModel> _families;
bool _loaded; Modal _frmDelete;
MachineViewModel _model;
bool _noFamily; List<GpuViewModel> _gpus;
bool _prototype; bool _loaded;
bool _unknownIntroduced; List<GpuByMachineViewModel> _machineGpus;
bool _unknownModel; MachineViewModel _model;
bool _noFamily;
bool _prototype;
bool _savingGpu;
bool _unknownIntroduced;
bool _unknownModel;
[Parameter] [Parameter]
public int Id { get; set; } public int Id { get; set; }
@@ -44,9 +59,11 @@ namespace Marechai.Pages.Admin.Details
!_creating) !_creating)
return; return;
_companies = await CompaniesService.GetAsync(); _companies = await CompaniesService.GetAsync();
_families = await MachineFamiliesService.GetAsync(); _families = await MachineFamiliesService.GetAsync();
_model = _creating ? new MachineViewModel() : await Service.GetAsync(Id); _model = _creating ? new MachineViewModel() : await Service.GetAsync(Id);
_machineGpus = await GpuByMachineService.GetByMachine(Id);
_gpus = await GpusService.GetAsync();
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machines/edit/", StartsWith("admin/machines/edit/",
@@ -127,5 +144,98 @@ namespace Marechai.Pages.Admin.Details
Validators.ValidateString(e, L["Model must contain less than 50 characters."], 50); Validators.ValidateString(e, L["Model must contain less than 50 characters."], 50);
void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateIntroducedDate(e); void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateIntroducedDate(e);
void ShowGpuDeleteModal(long itemId)
{
_currentGpuByMachine = _machineGpus.FirstOrDefault(n => n.Id == itemId);
_deletingGpuByMachine = true;
_deleteTitle = L["Delete graphical processing unit from this machine"];
_deleteText =
string.Format(L["Are you sure you want to delete the graphical processing unit {0} manufactured by {1} from this machine?"],
_currentGpuByMachine?.Name, _currentGpuByMachine?.CompanyName);
_frmDelete.Show();
}
void HideModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
if(_deletingGpuByMachine)
await ConfirmDeleteGpuByMachine();
}
async Task ConfirmDeleteGpuByMachine()
{
if(_currentGpuByMachine is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await GpuByMachineService.DeleteAsync(_currentGpuByMachine.Id);
_machineGpus = await GpuByMachineService.GetByMachine(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
void ModalClosing(ModalClosingEventArgs obj)
{
_deleteInProgress = false;
_deletingGpuByMachine = false;
_currentGpuByMachine = null;
}
async Task OnAddGpuClick()
{
_addingGpu = true;
_savingGpu = false;
_addingGpuId = null;
}
void CancelAddGpu()
{
_addingGpu = false;
_savingGpu = false;
_addingGpuId = null;
}
async Task ConfirmAddGpu()
{
if(_addingGpuId is null)
{
CancelAddGpu();
return;
}
_savingGpu = true;
// Yield thread to let UI to update
await Task.Yield();
await GpuByMachineService.CreateAsync(_addingGpuId.Value, Id);
_machineGpus = await GpuByMachineService.GetByMachine(Id);
_addingGpu = false;
_savingGpu = false;
_addingGpuId = null;
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
} }
} }

View File

@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Marechai.ViewModels;
using Microsoft.EntityFrameworkCore;
namespace Marechai.Services
{
public class GpuByMachineService
{
readonly MarechaiContext _context;
public GpuByMachineService(MarechaiContext context) => _context = context;
public async Task<List<GpuByMachineViewModel>> GetByMachine(int machineId) =>
await _context.GpusByMachine.Where(g => g.MachineId == machineId).Select(g => new GpuByMachineViewModel
{
Id = g.Id, Name = g.Gpu.Name, CompanyName = g.Gpu.Company.Name, GpuId = g.GpuId,
MachineId = g.MachineId
}).OrderBy(g => g.CompanyName).ThenBy(g => g.Name).ToListAsync();
public async Task DeleteAsync(long id)
{
GpusByMachine item = await _context.GpusByMachine.FindAsync(id);
if(item is null)
return;
_context.GpusByMachine.Remove(item);
await _context.SaveChangesAsync();
}
public async Task<long> CreateAsync(int gpuId, int machineId)
{
var item = new GpusByMachine
{
GpuId = gpuId, MachineId = machineId
};
await _context.GpusByMachine.AddAsync(item);
await _context.SaveChangesAsync();
return item.Id;
}
}
}

View File

@@ -62,6 +62,7 @@ namespace Marechai.Services
services.AddScoped<Iso31661NumericService>(); services.AddScoped<Iso31661NumericService>();
services.AddScoped<ResolutionsService>(); services.AddScoped<ResolutionsService>();
services.AddScoped<CompanyLogosService>(); services.AddScoped<CompanyLogosService>();
services.AddScoped<GpuByMachineService>();
} }
} }
} }

View File

@@ -0,0 +1,10 @@
namespace Marechai.ViewModels
{
public class GpuByMachineViewModel : BaseViewModel<long>
{
public int GpuId { get; set; }
public int MachineId { get; set; }
public string CompanyName { get; set; }
public string Name { get; set; }
}
}