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

This commit is contained in:
2020-05-29 18:36:05 +01:00
parent 092121c6c2
commit 45d9bac9b2
14 changed files with 320 additions and 452 deletions

View File

@@ -1,220 +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 ScreensByMachineController : Controller
{
readonly MarechaiContext _context;
public ScreensByMachineController(MarechaiContext context) => _context = context;
// GET: ScreensByMachine
public async Task<IActionResult> Index()
{
IIncludableQueryable<ScreensByMachine, Screen> marechaiContext =
_context.ScreensByMachine.Include(s => s.Machine).Include(s => s.Screen);
return View(await marechaiContext.Select(s => new ScreensByMachineViewModel
{
Id = s.Id,
Screen = s.Screen.NativeResolution != null
? $"{s.Screen.Diagonal}\" {s.Screen.Type} with {s.Screen.NativeResolution}"
: $"{s.Screen.Diagonal}\" {s.Screen}",
Machine = $"{s.Machine.Company.Name} {s.Machine.Name}"
}).OrderBy(s => s.Machine).ThenBy(s => s.Screen).ToListAsync());
}
// GET: ScreensByMachine/Details/5
public async Task<IActionResult> Details(long? id)
{
if(id == null)
return NotFound();
ScreensByMachineViewModel screensByMachine =
await _context.ScreensByMachine.Include(s => s.Machine).Include(s => s.Screen).
Select(s => new ScreensByMachineViewModel
{
Id = s.Id,
Screen = s.Screen.NativeResolution != null
? $"{s.Screen.Diagonal}\" {s.Screen.Type} with {s.Screen.NativeResolution}"
: $"{s.Screen.Diagonal}\" {s.Screen}",
Machine = $"{s.Machine.Company.Name} {s.Machine.Name}"
}).FirstOrDefaultAsync(m => m.Id == id);
if(screensByMachine == null)
return NotFound();
return View(screensByMachine);
}
// GET: ScreensByMachine/Create
public IActionResult Create()
{
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
Select(m => new
{
m.Id, Name = $"{m.Company.Name} {m.Name}"
}), "Id", "Name");
ViewData["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name");
return View();
}
// POST: ScreensByMachine/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("ScreenId,MachineId,Id")] ScreensByMachine screensByMachine)
{
if(ModelState.IsValid)
{
_context.Add(screensByMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
Select(m => new
{
m.Id, Name = $"{m.Company.Name} {m.Name}"
}), "Id", "Name", screensByMachine.MachineId);
ViewData["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name", screensByMachine.ScreenId);
return View(screensByMachine);
}
// GET: ScreensByMachine/Edit/5
public async Task<IActionResult> Edit(long? id)
{
if(id == null)
return NotFound();
ScreensByMachine screensByMachine = await _context.ScreensByMachine.FindAsync(id);
if(screensByMachine == null)
return NotFound();
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
Select(m => new
{
m.Id, Name = $"{m.Company.Name} {m.Name}"
}), "Id", "Name", screensByMachine.MachineId);
ViewData["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name", screensByMachine.ScreenId);
return View(screensByMachine);
}
// POST: ScreensByMachine/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("ScreenId,MachineId,Id")] ScreensByMachine screensByMachine)
{
if(id != screensByMachine.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(screensByMachine);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!ScreensByMachineExists(screensByMachine.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
Select(m => new
{
m.Id, Name = $"{m.Company.Name} {m.Name}"
}), "Id", "Name", screensByMachine.MachineId);
ViewData["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name", screensByMachine.ScreenId);
return View(screensByMachine);
}
// GET: ScreensByMachine/Delete/5
public async Task<IActionResult> Delete(long? id)
{
if(id == null)
return NotFound();
ScreensByMachineViewModel screensByMachine =
await _context.ScreensByMachine.Include(s => s.Machine).Include(s => s.Screen).
Select(s => new ScreensByMachineViewModel
{
Id = s.Id,
Screen = s.Screen.NativeResolution != null
? $"{s.Screen.Diagonal}\" {s.Screen.Type} with {s.Screen.NativeResolution}"
: $"{s.Screen.Diagonal}\" {s.Screen}",
Machine = $"{s.Machine.Company.Name} {s.Machine.Name}"
}).FirstOrDefaultAsync(m => m.Id == id);
if(screensByMachine == null)
return NotFound();
return View(screensByMachine);
}
// POST: ScreensByMachine/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(long id)
{
ScreensByMachine screensByMachine = await _context.ScreensByMachine.FindAsync(id);
_context.ScreensByMachine.Remove(screensByMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool ScreensByMachineExists(long id) => _context.ScreensByMachine.Any(e => e.Id == id);
[AcceptVerbs("Get", "Post")]
public async Task<IActionResult> VerifyUnique(int screenId, int machineId) =>
await _context.ScreensByMachine.FirstOrDefaultAsync(i => i.ScreenId == screenId && i.MachineId == machineId)
is null ? Json(true) : Json("The selected machine already has the selected screen.");
}
}

View File

@@ -1,42 +0,0 @@
@model Marechai.Database.Models.ScreensByMachine
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Screens 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="Screen" class="control-label">
</label>
<select asp-for="ScreenId" class="form-control" asp-items="ViewBag.ScreenId">
</select>
<span asp-validation-for="ScreenId" class="text-danger">
</span>
</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">
<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,32 +0,0 @@
@model Marechai.Areas.Admin.Models.ScreensByMachineViewModel
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Screens by machine</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Screen)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Screen)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Machine)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Machine)
</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,32 +0,0 @@
@model Marechai.Areas.Admin.Models.ScreensByMachineViewModel
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>Screens by machine</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Screen)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Screen)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Machine)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Machine)
</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,43 +0,0 @@
@model Marechai.Database.Models.ScreensByMachine
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Screens by machine</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="Screen" class="control-label">
</label>
<select asp-for="ScreenId" class="form-control" asp-items="ViewBag.ScreenId">
</select>
<span asp-validation-for="ScreenId" class="text-danger">
</span>
</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>
<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>
<div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -1,48 +0,0 @@
@model IEnumerable<Marechai.Areas.Admin.Models.ScreensByMachineViewModel>
@{
ViewData["Title"] = "Index";
}
<h1>Screens 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.Screen)
</th>
<th>
@Html.DisplayNameFor(model => model.Machine)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Screen)
</td>
<td>
@Html.DisplayFor(modelItem => item.Machine)
</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.1390</Version> <Version>3.0.99.1396</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>
@@ -169,5 +169,10 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Details.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Edit.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Index.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Index.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\ScreensByMachine\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\ScreensByMachine\Delete.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\ScreensByMachine\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\ScreensByMachine\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\ScreensByMachine\Index.cshtml" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -47,6 +47,8 @@
@inject ProcessorsService ProcessorsService @inject ProcessorsService ProcessorsService
@inject MemoriesByMachineService MemoriesByMachineService @inject MemoriesByMachineService MemoriesByMachineService
@inject StorageByMachineService StorageByMachineService @inject StorageByMachineService StorageByMachineService
@inject ScreensByMachineService ScreensByMachineService
@inject ScreensService ScreensService
@attribute [Authorize(Roles = "UberAdmin, Admin")] @attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Machine details"]</h3> <h3>@L["Machine details"]</h3>
<hr /> <hr />
@@ -554,6 +556,87 @@
</div> </div>
} }
<hr />
<h3>@L["Screens attached"]</h3>
<Button Color="Color.Success" Clicked="OnAddScreenClick" Disabled="_addingScreen">@L["Add new (screen by machine)"]</Button>
@if (_addingScreen)
{
<div>
<Field>
<FieldLabel>@L["Screens"]</FieldLabel>
<Select Disabled="_savingScreen" TValue="int?" @bind-SelectedValue="@_addingScreenId">
@foreach (var screen in _screens)
{
if (_machineScreens.Any(s => s.ScreenId == screen.Id))
{
<SelectItem TValue="int?" Value="@screen.Id">@screen.Diagonal with a native resolution of @screen.NativeResolution</SelectItem>
}
}
</Select>
<Button Color="Color.Primary" Clicked="@CancelAddScreen" Disabled="@_savingScreen">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddScreen" Disabled="@_savingScreen">@L["Add"]</Button>
</Field>
</div>
}
@if (_machineScreens?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Diagonal"]
</th>
<th>
@L["Width"]
</th>
<th>
@L["Height"]
</th>
<th>
@L["Type"]
</th>
<th>
@L["Effective colors"]
</th>
<th>
@L["Native resolution"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _machineScreens)
{
<tr>
<td>
@item.Screen.Diagonal
</td>
<td>
@item.Screen.Width
</td>
<td>
@item.Screen.Height
</td>
<td>
@item.Screen.Type
</td>
<td>
@item.Screen.EffectiveColors
</td>
<td>
@item.Screen.NativeResolution
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowScreenDeleteModal(item.Id);}" Disabled="@_addingScreen">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing"> <Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop /> <ModalBackdrop />
<ModalContent Centered="true"> <ModalContent Centered="true">

View File

@@ -21,19 +21,22 @@ namespace Marechai.Pages.Admin.Details
double? _addingMemorySpeed; double? _addingMemorySpeed;
int _addingMemoryType; int _addingMemoryType;
int _addingMemoryUsage; int _addingMemoryUsage;
bool _addingStorage;
long? _addingStorageSize;
int _addingStorageType;
int _addingStorageInterface;
float? _addingProcessorSpeed; float? _addingProcessorSpeed;
bool _addingScreen;
int? _addingScreenId;
bool _addingSound; bool _addingSound;
int? _addingSoundId; int? _addingSoundId;
bool _addingStorage;
int _addingStorageInterface;
long? _addingStorageSize;
int _addingStorageType;
List<CompanyViewModel> _companies; List<CompanyViewModel> _companies;
List<ProcessorViewModel> _cpus; List<ProcessorViewModel> _cpus;
bool _creating; bool _creating;
ProcessorByMachineViewModel _currentCpuByMachine; ProcessorByMachineViewModel _currentCpuByMachine;
GpuByMachineViewModel _currentGpuByMachine; GpuByMachineViewModel _currentGpuByMachine;
MemoryByMachineViewModel _currentMemoryByMachine; MemoryByMachineViewModel _currentMemoryByMachine;
ScreenByMachineViewModel _currentScreenByMachine;
SoundSynthByMachineViewModel _currentSoundByMachine; SoundSynthByMachineViewModel _currentSoundByMachine;
StorageByMachineViewModel _currentStorageByMachine; StorageByMachineViewModel _currentStorageByMachine;
bool _deleteInProgress; bool _deleteInProgress;
@@ -42,8 +45,9 @@ namespace Marechai.Pages.Admin.Details
bool _deletingCpuByMachine; bool _deletingCpuByMachine;
bool _deletingGpuByMachine; bool _deletingGpuByMachine;
bool _deletingMemoryByMachine; bool _deletingMemoryByMachine;
bool _deletingStorageByMachine; bool _deletingScreenByMachine;
bool _deletingSoundByMachine; bool _deletingSoundByMachine;
bool _deletingStorageByMachine;
bool _editing; bool _editing;
List<MachineFamilyViewModel> _families; List<MachineFamilyViewModel> _families;
Modal _frmDelete; Modal _frmDelete;
@@ -52,6 +56,7 @@ namespace Marechai.Pages.Admin.Details
List<ProcessorByMachineViewModel> _machineCpus; List<ProcessorByMachineViewModel> _machineCpus;
List<GpuByMachineViewModel> _machineGpus; List<GpuByMachineViewModel> _machineGpus;
List<MemoryByMachineViewModel> _machineMemories; List<MemoryByMachineViewModel> _machineMemories;
List<ScreenByMachineViewModel> _machineScreens;
List<SoundSynthByMachineViewModel> _machineSound; List<SoundSynthByMachineViewModel> _machineSound;
List<StorageByMachineViewModel> _machineStorage; List<StorageByMachineViewModel> _machineStorage;
MachineViewModel _model; MachineViewModel _model;
@@ -60,8 +65,10 @@ namespace Marechai.Pages.Admin.Details
bool _savingCpu; bool _savingCpu;
bool _savingGpu; bool _savingGpu;
bool _savingMemory; bool _savingMemory;
bool _savingScreen;
bool _savingSound; bool _savingSound;
bool _savingStorage; bool _savingStorage;
List<ScreenViewModel> _screens;
List<SoundSynthViewModel> _soundSynths; List<SoundSynthViewModel> _soundSynths;
bool _unknownIntroduced; bool _unknownIntroduced;
bool _unknownMemorySize; bool _unknownMemorySize;
@@ -98,10 +105,12 @@ namespace Marechai.Pages.Admin.Details
_machineGpus = await GpusByMachineService.GetByMachine(Id); _machineGpus = await GpusByMachineService.GetByMachine(Id);
_machineCpus = await ProcessorsByMachineService.GetByMachine(Id); _machineCpus = await ProcessorsByMachineService.GetByMachine(Id);
_gpus = await GpusService.GetAsync(); _gpus = await GpusService.GetAsync();
_screens = await ScreensService.GetAsync();
_cpus = await ProcessorsService.GetAsync(); _cpus = await ProcessorsService.GetAsync();
_soundSynths = await SoundSynthsService.GetAsync(); _soundSynths = await SoundSynthsService.GetAsync();
_machineMemories = await MemoriesByMachineService.GetByMachine(Id); _machineMemories = await MemoriesByMachineService.GetByMachine(Id);
_machineStorage = await StorageByMachineService.GetByMachine(Id); _machineStorage = await StorageByMachineService.GetByMachine(Id);
_machineScreens = await ScreensByMachineService.GetByMachine(Id);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machines/edit/", StartsWith("admin/machines/edit/",
@@ -210,6 +219,8 @@ namespace Marechai.Pages.Admin.Details
await ConfirmDeleteMemoryByMachine(); await ConfirmDeleteMemoryByMachine();
else if(_deletingStorageByMachine) else if(_deletingStorageByMachine)
await ConfirmDeleteStorageByMachine(); await ConfirmDeleteStorageByMachine();
else if(_deletingScreenByMachine)
await ConfirmDeleteScreenByMachine();
} }
async Task ConfirmDeleteGpuByMachine() async Task ConfirmDeleteGpuByMachine()
@@ -244,6 +255,10 @@ namespace Marechai.Pages.Admin.Details
_currentSoundByMachine = null; _currentSoundByMachine = null;
_deletingCpuByMachine = false; _deletingCpuByMachine = false;
_currentCpuByMachine = null; _currentCpuByMachine = null;
_deletingScreenByMachine = false;
_currentScreenByMachine = null;
_deletingMemoryByMachine = false;
_deletingStorageByMachine = false;
} }
void OnAddGpuClick() void OnAddGpuClick()
@@ -656,5 +671,81 @@ namespace Marechai.Pages.Admin.Details
e.Status = item > 0 ? ValidationStatus.Success : ValidationStatus.Error; e.Status = item > 0 ? ValidationStatus.Success : ValidationStatus.Error;
} }
void ShowScreenDeleteModal(long itemId)
{
_currentScreenByMachine = _machineScreens.FirstOrDefault(n => n.Id == itemId);
_deletingScreenByMachine = true;
_deleteTitle = L["Delete screen from this machine"];
_deleteText = L["Are you sure you want to delete the selected screen from this machine?"];
_frmDelete.Show();
}
async Task ConfirmDeleteScreenByMachine()
{
if(_currentScreenByMachine is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await ScreensByMachineService.DeleteAsync(_currentScreenByMachine.Id);
_machineScreens = await ScreensByMachineService.GetByMachine(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
void OnAddScreenClick()
{
_addingScreen = true;
_savingScreen = false;
_addingScreenId = _screens.First(s => _machineScreens.All(m => m.ScreenId == s.Id)).Id;
}
void CancelAddScreen()
{
_addingScreen = false;
_savingScreen = false;
_addingScreenId = null;
}
async Task ConfirmAddScreen()
{
if(_addingScreenId is null ||
_addingScreenId <= 0)
{
CancelAddScreen();
return;
}
_savingScreen = true;
// Yield thread to let UI to update
await Task.Yield();
await ScreensByMachineService.CreateAsync(_addingScreenId.Value, Id);
_machineScreens = await ScreensByMachineService.GetByMachine(Id);
_addingScreen = false;
_savingScreen = false;
_addingScreenId = null;
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
} }
} }

View File

@@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Blazorise; using Blazorise;
using Marechai.Database.Models; using Marechai.Database.Models;
using Marechai.ViewModels;
namespace Marechai.Pages.Admin namespace Marechai.Pages.Admin
{ {
@@ -10,8 +11,8 @@ namespace Marechai.Pages.Admin
{ {
bool _deleteInProgress; bool _deleteInProgress;
Modal _frmDelete; Modal _frmDelete;
Screen _screen; ScreenViewModel _screen;
List<Screen> _screens; List<ScreenViewModel> _screens;
void ShowModal(int itemId) void ShowModal(int itemId)
{ {
@@ -33,7 +34,7 @@ namespace Marechai.Pages.Admin
await Task.Yield(); await Task.Yield();
await Service.DeleteAsync(_screen.Id); await Service.DeleteAsync(_screen.Id);
_screens = Service.Get(); _screens = await Service.GetAsync();
_deleteInProgress = false; _deleteInProgress = false;
_frmDelete.Hide(); _frmDelete.Hide();
@@ -47,6 +48,6 @@ namespace Marechai.Pages.Admin
void ModalClosing(ModalClosingEventArgs obj) => _screen = null; void ModalClosing(ModalClosingEventArgs obj) => _screen = null;
protected override void OnInitialized() => _screens = Service.Get(); protected override async Task OnInitializedAsync() => _screens = await Service.GetAsync();
} }
} }

View File

@@ -67,6 +67,7 @@ namespace Marechai.Services
services.AddScoped<ProcessorsByMachineService>(); services.AddScoped<ProcessorsByMachineService>();
services.AddScoped<MemoriesByMachineService>(); services.AddScoped<MemoriesByMachineService>();
services.AddScoped<StorageByMachineService>(); services.AddScoped<StorageByMachineService>();
services.AddScoped<ScreensByMachineService>();
} }
} }
} }

View File

@@ -0,0 +1,66 @@
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 ScreensByMachineService
{
readonly MarechaiContext _context;
public ScreensByMachineService(MarechaiContext context) => _context = context;
public async Task<List<ScreenByMachineViewModel>> GetByMachine(int machineId) =>
await _context.ScreensByMachine.Where(s => s.MachineId == machineId).
Select(s => new ScreenByMachineViewModel
{
Id = s.Id, ScreenId = s.ScreenId, MachineId = s.MachineId, Screen = new ScreenViewModel
{
Diagonal = s.Screen.Diagonal, EffectiveColors = s.Screen.EffectiveColors,
Height = s.Screen.Height, Id = s.Screen.Id, NativeResolution =
new ResolutionViewModel
{
Chars = s.Screen.NativeResolution.Chars,
Colors = s.Screen.NativeResolution.Colors,
Grayscale = s.Screen.NativeResolution.Grayscale,
Height = s.Screen.NativeResolution.Height, Id = s.Screen.NativeResolutionId,
Palette = s.Screen.NativeResolution.Palette,
Width = s.Screen.NativeResolution.Width
},
NativeResolutionId = s.Screen.NativeResolutionId, Type = s.Screen.Type,
Width = s.Screen.Width
}
}).ToListAsync();
public async Task DeleteAsync(long id)
{
ScreensByMachine item = await _context.ScreensByMachine.FindAsync(id);
if(item is null)
return;
_context.ScreensByMachine.Remove(item);
await _context.SaveChangesAsync();
}
public async Task<long> CreateAsync(int machineId, int screenId)
{
if(_context.ScreensByMachine.Any(s => s.MachineId == machineId && s.ScreenId == screenId))
return 0;
var item = new ScreensByMachine
{
ScreenId = screenId, MachineId = machineId
};
await _context.ScreensByMachine.AddAsync(item);
await _context.SaveChangesAsync();
return item.Id;
}
}
}

View File

@@ -13,9 +13,38 @@ namespace Marechai.Services
public ScreensService(MarechaiContext context) => _context = context; public ScreensService(MarechaiContext context) => _context = context;
public List<Screen> Get() => _context.Screens.AsEnumerable().OrderBy(s => s.Diagonal). public async Task<List<ScreenViewModel>> GetAsync() => await _context.Screens.Select(s => new ScreenViewModel
ThenBy(s => s.EffectiveColors).ThenBy(s => s.NativeResolution.ToString()). {
ThenBy(s => s.Type).ThenBy(s => s.Size).ToList(); Diagonal = s.Diagonal,
EffectiveColors = s.EffectiveColors,
Height = s.Height, Id = s.Id,
Type = s.Type, Width = s.Width,
NativeResolutionId =
s.NativeResolutionId,
NativeResolution =
new ResolutionViewModel
{
Chars = s.NativeResolution.
Chars,
Colors = s.NativeResolution.
Colors,
Grayscale = s.
NativeResolution.
Grayscale,
Height = s.NativeResolution.
Height,
Id = s.NativeResolution.Id,
Palette = s.NativeResolution.
Palette,
Width = s.NativeResolution.
Width
}
}).OrderBy(s => s.Diagonal).
ThenBy(s => s.EffectiveColors).
ThenBy(s => s.NativeResolution.
ToString()).
ThenBy(s => s.Type).ThenBy(s => s.Size).
ToListAsync();
public async Task<ScreenViewModel> GetAsync(int id) => public async Task<ScreenViewModel> GetAsync(int id) =>
await _context.Screens.Where(s => s.Id == id).Select(s => new ScreenViewModel await _context.Screens.Where(s => s.Id == id).Select(s => new ScreenViewModel

View File

@@ -0,0 +1,9 @@
namespace Marechai.ViewModels
{
public class ScreenByMachineViewModel : BaseViewModel<long>
{
public int MachineId { get; set; }
public ScreenViewModel Screen { get; set; }
public int ScreenId { get; set; }
}
}