diff --git a/Marechai/Areas/Admin/Controllers/StorageByMachinesController.cs b/Marechai/Areas/Admin/Controllers/StorageByMachinesController.cs deleted file mode 100644 index 7e6084d1..00000000 --- a/Marechai/Areas/Admin/Controllers/StorageByMachinesController.cs +++ /dev/null @@ -1,186 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : StorageByMachinesController.cs -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// Storage by machines 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 StorageByMachinesController : Controller - { - readonly MarechaiContext _context; - - public StorageByMachinesController(MarechaiContext context) => _context = context; - - // GET: Admin/StorageByMachines - public async Task Index() - { - IIncludableQueryable marechaiContext = - _context.StorageByMachine.Include(s => s.Machine); - - return View(await marechaiContext.OrderBy(s => s.Machine.Company.Name).ThenBy(s => s.Machine.Name). - Select(s => new StorageByMachineViewModel - { - Id = s.Id, Company = s.Machine.Company.Name, - Machine = s.Machine.Name, - Type = s.Type, Interface = s.Interface, - Capacity = s.Capacity - }).ToListAsync()); - } - - // GET: Admin/StorageByMachines/Details/5 - public async Task Details(long? id) - { - if(id == null) - return NotFound(); - - StorageByMachine storageByMachine = - await _context.StorageByMachine.Include(s => s.Machine).FirstOrDefaultAsync(m => m.Id == id); - - if(storageByMachine == null) - return NotFound(); - - return View(storageByMachine); - } - - // GET: Admin/StorageByMachines/Create - public IActionResult Create() - { - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name"); - - return View(); - } - - // POST: Admin/StorageByMachines/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("MachineId,Type,Interface,Capacity,Id")] - StorageByMachine storageByMachine) - { - if(ModelState.IsValid) - { - _context.Add(storageByMachine); - await _context.SaveChangesAsync(); - - return RedirectToAction(nameof(Index)); - } - - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", storageByMachine.MachineId); - - return View(storageByMachine); - } - - // GET: Admin/StorageByMachines/Edit/5 - public async Task Edit(long? id) - { - if(id == null) - return NotFound(); - - StorageByMachine storageByMachine = await _context.StorageByMachine.FindAsync(id); - - if(storageByMachine == null) - return NotFound(); - - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", storageByMachine.MachineId); - - return View(storageByMachine); - } - - // POST: Admin/StorageByMachines/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(long id, [Bind("MachineId,Type,Interface,Capacity,Id")] - StorageByMachine storageByMachine) - { - if(id != storageByMachine.Id) - return NotFound(); - - if(ModelState.IsValid) - { - try - { - _context.Update(storageByMachine); - await _context.SaveChangesAsync(); - } - catch(DbUpdateConcurrencyException) - { - if(!StorageByMachineExists(storageByMachine.Id)) - return NotFound(); - - throw; - } - - return RedirectToAction(nameof(Index)); - } - - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", storageByMachine.MachineId); - - return View(storageByMachine); - } - - // GET: Admin/StorageByMachines/Delete/5 - public async Task Delete(long? id) - { - if(id == null) - return NotFound(); - - StorageByMachine storageByMachine = - await _context.StorageByMachine.Include(s => s.Machine).FirstOrDefaultAsync(m => m.Id == id); - - if(storageByMachine == null) - return NotFound(); - - return View(storageByMachine); - } - - // POST: Admin/StorageByMachines/Delete/5 - [HttpPost, ActionName("Delete"), ValidateAntiForgeryToken] - public async Task DeleteConfirmed(long id) - { - StorageByMachine storageByMachine = await _context.StorageByMachine.FindAsync(id); - _context.StorageByMachine.Remove(storageByMachine); - await _context.SaveChangesAsync(); - - return RedirectToAction(nameof(Index)); - } - - bool StorageByMachineExists(long id) => _context.StorageByMachine.Any(e => e.Id == id); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/StorageByMachines/Create.cshtml b/Marechai/Areas/Admin/Views/StorageByMachines/Create.cshtml deleted file mode 100644 index 168d1a6c..00000000 --- a/Marechai/Areas/Admin/Views/StorageByMachines/Create.cshtml +++ /dev/null @@ -1,87 +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 -*******************************************************************************/ -} -@using Marechai.Database -@model Marechai.Database.Models.StorageByMachine - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

Storage by machine

-
-
-
-
-
-
-
- - -
-
- - - - -
-
- - - - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/StorageByMachines/Delete.cshtml b/Marechai/Areas/Admin/Views/StorageByMachines/Delete.cshtml deleted file mode 100644 index 875032b1..00000000 --- a/Marechai/Areas/Admin/Views/StorageByMachines/Delete.cshtml +++ /dev/null @@ -1,75 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Delete.cshtml -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// Admin view delete -// -// --[ 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.StorageByMachine - -@{ - ViewData["Title"] = "Delete"; -} -

Delete

-

Are you sure you want to delete this?

-
-

Storage by machine

-
-
-
- @Html.DisplayNameFor(model => model.Type) -
-
- @Html.DisplayFor(model => model.Type) -
-
- @Html.DisplayNameFor(model => model.Interface) -
-
- @Html.DisplayFor(model => model.Interface) -
-
- @Html.DisplayNameFor(model => model.Capacity) -
-
- @Html.DisplayFor(model => model.Capacity) -
-
- @Html.DisplayNameFor(model => model.Machine) -
-
- @Html.DisplayFor(model => model.Machine.Name) -
-
-
- - - - Back to List - -
-
\ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/StorageByMachines/Details.cshtml b/Marechai/Areas/Admin/Views/StorageByMachines/Details.cshtml deleted file mode 100644 index 5cee3173..00000000 --- a/Marechai/Areas/Admin/Views/StorageByMachines/Details.cshtml +++ /dev/null @@ -1,71 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Details.cshtml -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// Admin view details -// -// --[ 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.StorageByMachine - -@{ - ViewData["Title"] = "Details"; -} -

Details

-
-

Storage by machine

-
-
-
- @Html.DisplayNameFor(model => model.Type) -
-
- @Html.DisplayFor(model => model.Type) -
-
- @Html.DisplayNameFor(model => model.Interface) -
-
- @Html.DisplayFor(model => model.Interface) -
-
- @Html.DisplayNameFor(model => model.Capacity) -
-
- @Html.DisplayFor(model => model.Capacity) -
-
- @Html.DisplayNameFor(model => model.Machine) -
-
- @Html.DisplayFor(model => model.Machine.Name) -
-
-
- \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/StorageByMachines/Edit.cshtml b/Marechai/Areas/Admin/Views/StorageByMachines/Edit.cshtml deleted file mode 100644 index 67070f37..00000000 --- a/Marechai/Areas/Admin/Views/StorageByMachines/Edit.cshtml +++ /dev/null @@ -1,90 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Edit.cshtml -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// Admin view edit -// -// --[ 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 Marechai.Database -@model Marechai.Database.Models.StorageByMachine - -@{ - ViewData["Title"] = "Edit"; -} -

Edit

-

Storage by machine

-
-
-
-
-
-
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
- - -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/StorageByMachines/Index.cshtml b/Marechai/Areas/Admin/Views/StorageByMachines/Index.cshtml deleted file mode 100644 index 089109b6..00000000 --- a/Marechai/Areas/Admin/Views/StorageByMachines/Index.cshtml +++ /dev/null @@ -1,97 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Index.cshtml -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// Admin view index -// -// --[ 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 IEnumerable - -@{ - ViewData["Title"] = "Storage by machines (Admin)"; -} -

Storage by machines

-

- - Create new - -

- - - - - - - - - - - - - @foreach (var item in Model) - { - - - - - - - - - } - -
- @Html.DisplayNameFor(model => model.Company) - - @Html.DisplayNameFor(model => model.Machine) - - @Html.DisplayNameFor(model => model.Type) - - @Html.DisplayNameFor(model => model.Interface) - - @Html.DisplayNameFor(model => model.Capacity) -
- @Html.DisplayFor(modelItem => item.Company) - - @Html.DisplayFor(modelItem => item.Machine) - - @Html.DisplayFor(modelItem => item.Type) - - @Html.DisplayFor(modelItem => item.Interface) - - @Html.DisplayFor(modelItem => item.Capacity) - - - Details - - - Edit - - - Delete - -
\ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index ff2f634a..29a04e8f 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1387 + 3.0.99.1390 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website @@ -164,5 +164,10 @@ <_ContentIncludedByDefault Remove="Areas\Admin\Views\MemoryByMachines\Details.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\MemoryByMachines\Edit.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\MemoryByMachines\Index.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Create.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Delete.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Details.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Edit.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\StorageByMachines\Index.cshtml" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Machine.razor b/Marechai/Pages/Admin/Details/Machine.razor index 3dfb7447..19747d67 100644 --- a/Marechai/Pages/Admin/Details/Machine.razor +++ b/Marechai/Pages/Admin/Details/Machine.razor @@ -46,6 +46,7 @@ @inject ProcessorsByMachineService ProcessorsByMachineService @inject ProcessorsService ProcessorsService @inject MemoriesByMachineService MemoriesByMachineService +@inject StorageByMachineService StorageByMachineService @attribute [Authorize(Roles = "UberAdmin, Admin")]

@L["Machine details"]


@@ -463,6 +464,96 @@ } +
+

@L["Storage belonging to this machine"]

+ + @if (_addingStorage) + { +
+ + @L["Storage type"] + + + + @L["Storage interface"] + + + + @L["Nominal capacity (bytes)"] + @L["Unknown (storage by machine nominal capacity)"] + @if (!_unknownStorageSize) + { + + + + @L["Please enter a valid size for this storage."] + + + + } + + + +
+ } + @if (_machineStorage?.Count > 0) + { +
+ + + + + + + + + + + @foreach (var item in _machineStorage) + { + + + + + + + } + +
+ @L["Type"] + + @L["Interface"] + + @L["Nominal capacity"] +
+ @item.Type + + @item.Interface + + @if (item.Capacity.HasValue) + { + @string.Format(L["{0} bytes"], item.Capacity) + } + else + { + @L["Unknown (storage by machine nominal capacity)"] + } + + +
+
+ } + diff --git a/Marechai/Pages/Admin/Details/Machine.razor.cs b/Marechai/Pages/Admin/Details/Machine.razor.cs index 3dcae168..14de7e32 100644 --- a/Marechai/Pages/Admin/Details/Machine.razor.cs +++ b/Marechai/Pages/Admin/Details/Machine.razor.cs @@ -12,16 +12,19 @@ namespace Marechai.Pages.Admin.Details { public partial class Machine { - bool _addingCpu; - int? _addingCpuId; - bool _addingGpu; - int? _addingGpuId; - bool _addingMemory; - long? _addingMemorySize; - double? _addingMemorySpeed; - + bool _addingCpu; + int? _addingCpuId; + bool _addingGpu; + int? _addingGpuId; + bool _addingMemory; + long? _addingMemorySize; + double? _addingMemorySpeed; int _addingMemoryType; int _addingMemoryUsage; + bool _addingStorage; + long? _addingStorageSize; + int _addingStorageType; + int _addingStorageInterface; float? _addingProcessorSpeed; bool _addingSound; int? _addingSoundId; @@ -31,13 +34,15 @@ namespace Marechai.Pages.Admin.Details ProcessorByMachineViewModel _currentCpuByMachine; GpuByMachineViewModel _currentGpuByMachine; MemoryByMachineViewModel _currentMemoryByMachine; - SoundSynthByMachineViewModel _currentSoundByMachine; + SoundSynthByMachineViewModel _currentSoundByMachine; + StorageByMachineViewModel _currentStorageByMachine; bool _deleteInProgress; string _deleteText; string _deleteTitle; bool _deletingCpuByMachine; bool _deletingGpuByMachine; - bool _deletingMemoryByMachine; + bool _deletingMemoryByMachine; + bool _deletingStorageByMachine; bool _deletingSoundByMachine; bool _editing; List _families; @@ -48,19 +53,22 @@ namespace Marechai.Pages.Admin.Details List _machineGpus; List _machineMemories; List _machineSound; + List _machineStorage; MachineViewModel _model; bool _noFamily; bool _prototype; bool _savingCpu; bool _savingGpu; bool _savingMemory; - bool _savingSound; + bool _savingSound; + bool _savingStorage; List _soundSynths; bool _unknownIntroduced; bool _unknownMemorySize; bool _unknownMemorySpeed; bool _unknownModel; bool _unknownProcessorSpeed; + bool _unknownStorageSize; [Parameter] public int Id { get; set; } @@ -93,6 +101,7 @@ namespace Marechai.Pages.Admin.Details _cpus = await ProcessorsService.GetAsync(); _soundSynths = await SoundSynthsService.GetAsync(); _machineMemories = await MemoriesByMachineService.GetByMachine(Id); + _machineStorage = await StorageByMachineService.GetByMachine(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/machines/edit/", @@ -197,6 +206,10 @@ namespace Marechai.Pages.Admin.Details await ConfirmDeleteSoundByMachine(); else if(_deletingCpuByMachine) await ConfirmDeleteCpuByMachine(); + else if(_deletingMemoryByMachine) + await ConfirmDeleteMemoryByMachine(); + else if(_deletingStorageByMachine) + await ConfirmDeleteStorageByMachine(); } async Task ConfirmDeleteGpuByMachine() @@ -556,5 +569,92 @@ namespace Marechai.Pages.Admin.Details e.Status = item > 0 ? ValidationStatus.Success : ValidationStatus.Error; } + + void ShowStorageDeleteModal(long itemId) + { + _currentStorageByMachine = _machineStorage.FirstOrDefault(n => n.Id == itemId); + _deletingStorageByMachine = true; + _deleteTitle = L["Delete storage from this machine"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the storage type {0} with interface {1} from this machine?"], + _currentStorageByMachine?.Type, _currentStorageByMachine?.Interface); + + _frmDelete.Show(); + } + + async Task ConfirmDeleteStorageByMachine() + { + if(_currentStorageByMachine is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await StorageByMachineService.DeleteAsync(_currentStorageByMachine.Id); + _machineStorage = await StorageByMachineService.GetByMachine(Id); + + _deleteInProgress = false; + _frmDelete.Hide(); + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void OnAddStorageClick() + { + _addingStorage = true; + _savingStorage = false; + _addingStorageSize = 0; + _unknownStorageSize = true; + } + + void CancelAddStorage() + { + _addingStorage = false; + _savingStorage = false; + } + + async Task ConfirmAddStorage() + { + // TODO: Validation + + _savingStorage = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await StorageByMachineService.CreateAsync(Id, (StorageType)_addingStorageType, + (StorageInterface)_addingStorageInterface, + _unknownStorageSize ? null : _addingStorageSize); + + _machineStorage = await StorageByMachineService.GetByMachine(Id); + + _addingStorage = false; + _savingStorage = false; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ValidateStorageSize(ValidatorEventArgs e) + { + if(!(e.Value is long item)) + { + e.Status = ValidationStatus.Error; + + return; + } + + e.Status = item > 0 ? ValidationStatus.Success : ValidationStatus.Error; + } } } \ No newline at end of file diff --git a/Marechai/Services/Register.cs b/Marechai/Services/Register.cs index 180bbefc..0339f9c4 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -66,6 +66,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/Services/StorageByMachineService.cs b/Marechai/Services/StorageByMachineService.cs new file mode 100644 index 00000000..5d0a7e32 --- /dev/null +++ b/Marechai/Services/StorageByMachineService.cs @@ -0,0 +1,51 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Marechai.Database; +using Marechai.Database.Models; +using Marechai.ViewModels; +using Microsoft.EntityFrameworkCore; + +namespace Marechai.Services +{ + public class StorageByMachineService + { + readonly MarechaiContext _context; + + public StorageByMachineService(MarechaiContext context) => _context = context; + + public async Task> GetByMachine(int machineId) => + await _context.StorageByMachine.Where(s => s.MachineId == machineId). + Select(s => new StorageByMachineViewModel + { + Id = s.Id, Type = s.Type, Interface = s.Interface, Capacity = s.Capacity, + MachineId = s.MachineId + }).OrderBy(s => s.Type).ThenBy(s => s.Interface).ThenBy(s => s.Capacity).ToListAsync(); + + public async Task DeleteAsync(long id) + { + StorageByMachine item = await _context.StorageByMachine.FindAsync(id); + + if(item is null) + return; + + _context.StorageByMachine.Remove(item); + + await _context.SaveChangesAsync(); + } + + public async Task CreateAsync(int machineId, StorageType type, StorageInterface @interface, + long? capacity) + { + var item = new StorageByMachine + { + MachineId = machineId, Type = type, Interface = @interface, Capacity = capacity + }; + + await _context.StorageByMachine.AddAsync(item); + await _context.SaveChangesAsync(); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/ViewModels/StorageByMachineViewModel.cs b/Marechai/ViewModels/StorageByMachineViewModel.cs new file mode 100644 index 00000000..f073d4a3 --- /dev/null +++ b/Marechai/ViewModels/StorageByMachineViewModel.cs @@ -0,0 +1,12 @@ +using Marechai.Database; + +namespace Marechai.ViewModels +{ + public class StorageByMachineViewModel : BaseViewModel + { + public int MachineId { get; set; } + public StorageType Type { get; set; } + public StorageInterface Interface { get; set; } + public long? Capacity { get; set; } + } +} \ No newline at end of file