Add code to delete machines.

This commit is contained in:
2020-05-24 21:50:17 +01:00
parent 3d0bfe41dd
commit d787eca0d5
5 changed files with 93 additions and 99 deletions

View File

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

@@ -99,21 +99,26 @@
<span class="btn btn-secondary">
@L["Edit"]
</span>
<span class="btn btn-danger">
@L["Delete"]
</span>
<Button Color="Color.Danger" Clicked="() => {ShowModal(item.Id);}">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
@code
{
List<MachineViewModel> _machines;
protected override async Task OnInitializedAsync()
{
_machines = await Service.GetAsync();
}
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop/>
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@L["Delete machine"]</ModalTitle>
<CloseButton Clicked="@HideModal"/>
</ModalHeader>
<ModalBody>
<Text>@string.Format(@L["Are you sure you want to delete {0} {1}?"], _machine?.Company, _machine?.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.ViewModels;
namespace Marechai.Pages.Admin
{
public partial class Machines
{
bool _deleteInProgress;
Modal _frmDelete;
MachineViewModel _machine;
List<MachineViewModel> _machines;
void ShowModal(int itemId)
{
_machine = _machines.FirstOrDefault(n => n.Id == itemId);
_frmDelete.Show();
}
void HideModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
if(_machine is null)
return;
_deleteInProgress = true;
_machines = null;
// Yield thread to let UI to update
await Task.Yield();
await Service.DeleteAsync(_machine.Id);
_machines = 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) => _machine = null;
protected override async Task OnInitializedAsync() => _machines = await Service.GetAsync();
}
}

View File

@@ -446,4 +446,16 @@
<value>Compañía</value>
<comment>Company</comment>
</data>
<data name="Delete machine" xml:space="preserve">
<value>Eliminar máquina</value>
<comment>Delete machine</comment>
</data>
<data name="Are you sure you want to delete {0} {1}?" xml:space="preserve">
<value>¿Está seguro de que desea borrar {0} {1}?</value>
<comment>{0} company name, {1} machine name</comment>
</data>
<data name="Cancel" xml:space="preserve">
<value>Cancelar</value>
<comment>Cancel</comment>
</data>
</root>

View File

@@ -93,5 +93,17 @@ namespace Marechai.Services
return model;
}
public async Task DeleteAsync(int id)
{
Machine item = await _context.Machines.FindAsync(id);
if(item is null)
return;
_context.Machines.Remove(item);
await _context.SaveChangesAsync();
}
}
}