Add code to delete document people.

This commit is contained in:
2020-05-24 21:12:09 +01:00
parent e0ee49c6c2
commit 6d3d183e0c
5 changed files with 93 additions and 63 deletions

View File

@@ -1,51 +0,0 @@
@model Marechai.Database.Models.DocumentPerson
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Document person</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Surname)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Surname)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Alias)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Alias)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.DisplayName)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.DisplayName)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Person)
</dt>
<dd class="col-sm-10">
<a asp-action="Details" asp-route-id="@Model.PersonId" asp-controller="People">
@Html.DisplayFor(modelItem => Model.Person.FullName)</a>
</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

@@ -77,21 +77,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<DocumentPersonViewModel> _people;
protected override async Task OnInitializedAsync()
{
_people = await Service.GetAsync();
}
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop/>
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@L["Delete person"]</ModalTitle>
<CloseButton Clicked="@HideModal"/>
</ModalHeader>
<ModalBody>
<Text>@string.Format(@L["Are you sure you want to delete {0}?"], _person?.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 DocumentPeople
{
bool _deleteInProgress;
Modal _frmDelete;
List<DocumentPersonViewModel> _people;
DocumentPersonViewModel _person;
void ShowModal(int itemId)
{
_person = _people.FirstOrDefault(n => n.Id == itemId);
_frmDelete.Show();
}
void HideModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
if(_person is null)
return;
_deleteInProgress = true;
_people = null;
// Yield thread to let UI to update
await Task.Yield();
await Service.DeleteAsync(_person.Id);
_people = 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) => _person = null;
protected override async Task OnInitializedAsync() => _people = await Service.GetAsync();
}
}

View File

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

View File

@@ -23,5 +23,17 @@ namespace Marechai.Services
Person = d.Person.FullName,
PersonId = d.PersonId
}).ToListAsync();
public async Task DeleteAsync(int id)
{
DocumentPerson item = await _context.DocumentPeople.FindAsync(id);
if(item is null)
return;
_context.DocumentPeople.Remove(item);
await _context.SaveChangesAsync();
}
}
}