Add code to delete instruction sets.

This commit is contained in:
2020-05-24 21:33:59 +01:00
parent 69451a9eed
commit 8051391bb1
5 changed files with 93 additions and 70 deletions

View File

@@ -1,57 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Delete.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.InstructionSet
@{
ViewData["Title"] = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Instruction set</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.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

@@ -31,7 +31,6 @@
} }
@page "/admin/instruction_sets" @page "/admin/instruction_sets"
@using Marechai.Database.Models
@inherits OwningComponentBase<InstructionSetsService> @inherits OwningComponentBase<InstructionSetsService>
@inject IStringLocalizer<InstructionSetsService> L @inject IStringLocalizer<InstructionSetsService> L
@attribute [Authorize(Roles = "UberAdmin, Admin")] @attribute [Authorize(Roles = "UberAdmin, Admin")]
@@ -70,21 +69,26 @@
<span class="btn btn-secondary"> <span class="btn btn-secondary">
@L["Edit"] @L["Edit"]
</span> </span>
<span class="btn btn-danger"> <Button Color="Color.Danger" Clicked="() => {ShowModal(item.Id);}">@L["Delete"]</Button>
@L["Delete"]
</span>
</td> </td>
</tr> </tr>
} }
</tbody> </tbody>
</table> </table>
@code <Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
{ <ModalBackdrop/>
List<InstructionSet> _instructionSets; <ModalContent Centered="true">
<ModalHeader>
protected override async Task OnInitializedAsync() <ModalTitle>@L["Delete instruction set"]</ModalTitle>
{ <CloseButton Clicked="@HideModal"/>
_instructionSets = await Service.GetAsync(); </ModalHeader>
} <ModalBody>
} <Text>@string.Format(@L["Are you sure you want to delete {0}?"], _instructionSet?.Name)</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

@@ -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<InstructionSet> _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();
}
}

View File

@@ -146,4 +146,16 @@
<value>Eliminar</value> <value>Eliminar</value>
<comment>Delete</comment> <comment>Delete</comment>
</data> </data>
<data name="Delete instruction set" xml:space="preserve">
<value>Eliminar arquitectura</value>
<comment>Delete instruction set</comment>
</data>
<data name="Are you sure you want to delete {0}?" xml:space="preserve">
<value>¿Está seguro de que desea borrar {0}?</value>
<comment>{0} instruction set name</comment>
</data>
<data name="Cancel" xml:space="preserve">
<value>Cancelar</value>
<comment>Cancel</comment>
</data>
</root> </root>

View File

@@ -14,5 +14,17 @@ namespace Marechai.Services
public async Task<List<InstructionSet>> GetAsync() => public async Task<List<InstructionSet>> GetAsync() =>
await _context.InstructionSets.OrderBy(s => s.Name).ToListAsync(); 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();
}
} }
} }