From 8051391bb1d67f21d87b2b431866b68c0051635b Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 24 May 2020 21:33:59 +0100 Subject: [PATCH] Add code to delete instruction sets. --- .../Admin/Views/InstructionSets/Delete.cshtml | 57 ------------------- Marechai/Pages/Admin/InstructionSets.razor | 30 +++++----- Marechai/Pages/Admin/InstructionSets.razor.cs | 52 +++++++++++++++++ .../Services/InstructionSetsService.es.resx | 12 ++++ Marechai/Services/InstructionSetsService.cs | 12 ++++ 5 files changed, 93 insertions(+), 70 deletions(-) delete mode 100644 Marechai/Areas/Admin/Views/InstructionSets/Delete.cshtml create mode 100644 Marechai/Pages/Admin/InstructionSets.razor.cs diff --git a/Marechai/Areas/Admin/Views/InstructionSets/Delete.cshtml b/Marechai/Areas/Admin/Views/InstructionSets/Delete.cshtml deleted file mode 100644 index dfaf35cd..00000000 --- a/Marechai/Areas/Admin/Views/InstructionSets/Delete.cshtml +++ /dev/null @@ -1,57 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Delete.cshtml -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// View start -// -// --[ 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.InstructionSet - -@{ - ViewData["Title"] = "Delete"; -} -

Delete

-

Are you sure you want to delete this?

-
-

Instruction set

-
-
-
- @Html.DisplayNameFor(model => model.Name) -
-
- @Html.DisplayFor(model => model.Name) -
-
-
- - - - Back to List - -
-
\ No newline at end of file diff --git a/Marechai/Pages/Admin/InstructionSets.razor b/Marechai/Pages/Admin/InstructionSets.razor index e8e317e9..ae0807df 100644 --- a/Marechai/Pages/Admin/InstructionSets.razor +++ b/Marechai/Pages/Admin/InstructionSets.razor @@ -31,7 +31,6 @@ } @page "/admin/instruction_sets" -@using Marechai.Database.Models @inherits OwningComponentBase @inject IStringLocalizer L @attribute [Authorize(Roles = "UberAdmin, Admin")] @@ -70,21 +69,26 @@ @L["Edit"] - - @L["Delete"] - + } -@code -{ - List _instructionSets; - - protected override async Task OnInitializedAsync() - { - _instructionSets = await Service.GetAsync(); - } -} \ No newline at end of file + + + + + @L["Delete instruction set"] + + + + @string.Format(@L["Are you sure you want to delete {0}?"], _instructionSet?.Name) + + + + + + + diff --git a/Marechai/Pages/Admin/InstructionSets.razor.cs b/Marechai/Pages/Admin/InstructionSets.razor.cs new file mode 100644 index 00000000..217de669 --- /dev/null +++ b/Marechai/Pages/Admin/InstructionSets.razor.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Blazorise; +using Marechai.Database.Models; + +namespace Marechai.Pages.Admin +{ + public partial class InstructionSets + { + bool _deleteInProgress; + Modal _frmDelete; + InstructionSet _instructionSet; + List _instructionSets; + + void ShowModal(int itemId) + { + _instructionSet = _instructionSets.FirstOrDefault(n => n.Id == itemId); + _frmDelete.Show(); + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_instructionSet is null) + return; + + _deleteInProgress = true; + _instructionSets = null; + + // Yield thread to let UI to update + await Task.Yield(); + + await Service.DeleteAsync(_instructionSet.Id); + _instructionSets = await Service.GetAsync(); + + _deleteInProgress = false; + _frmDelete.Hide(); + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ModalClosing(ModalClosingEventArgs obj) => _instructionSet = null; + + protected override async Task OnInitializedAsync() => _instructionSets = await Service.GetAsync(); + } +} \ No newline at end of file diff --git a/Marechai/Resources/Services/InstructionSetsService.es.resx b/Marechai/Resources/Services/InstructionSetsService.es.resx index f6c17ffd..18a25675 100644 --- a/Marechai/Resources/Services/InstructionSetsService.es.resx +++ b/Marechai/Resources/Services/InstructionSetsService.es.resx @@ -146,4 +146,16 @@ Eliminar Delete + + Eliminar arquitectura + Delete instruction set + + + ¿Está seguro de que desea borrar {0}? + {0} instruction set name + + + Cancelar + Cancel + \ No newline at end of file diff --git a/Marechai/Services/InstructionSetsService.cs b/Marechai/Services/InstructionSetsService.cs index 260c78a3..75988999 100644 --- a/Marechai/Services/InstructionSetsService.cs +++ b/Marechai/Services/InstructionSetsService.cs @@ -14,5 +14,17 @@ namespace Marechai.Services public async Task> GetAsync() => await _context.InstructionSets.OrderBy(s => s.Name).ToListAsync(); + + public async Task DeleteAsync(int id) + { + InstructionSet item = await _context.InstructionSets.FindAsync(id); + + if(item is null) + return; + + _context.InstructionSets.Remove(item); + + await _context.SaveChangesAsync(); + } } } \ No newline at end of file