Add code to delete licenses.

This commit is contained in:
2020-05-24 21:37:15 +01:00
parent 8051391bb1
commit c31b4d3fb3
5 changed files with 98 additions and 70 deletions

View File

@@ -1,56 +0,0 @@
@model Marechai.Database.Models.License
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>License</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.SPDX)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.SPDX)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.FsfApproved)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.FsfApproved)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.OsiApproved)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.OsiApproved)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Link)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Link)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Text)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Text)
</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/licenses"
@using Marechai.Database.Models
@inherits OwningComponentBase<LicensesService>
@inject IStringLocalizer<LicensesService> L
@attribute [Authorize(Roles = "UberAdmin, Admin")]
@@ -99,21 +98,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<License> _licenses;
protected override async Task OnInitializedAsync()
{
_licenses = await Service.GetAsync();
}
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop/>
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@L["Delete license"]</ModalTitle>
<CloseButton Clicked="@HideModal"/>
</ModalHeader>
<ModalBody>
<Text>@string.Format(@L["Are you sure you want to delete {0}?"], _license?.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 Licenses
{
bool _deleteInProgress;
Modal _frmDelete;
License _license;
List<License> _licenses;
void ShowModal(int itemId)
{
_license = _licenses.FirstOrDefault(n => n.Id == itemId);
_frmDelete.Show();
}
void HideModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
if(_license is null)
return;
_deleteInProgress = true;
_licenses = null;
// Yield thread to let UI to update
await Task.Yield();
await Service.DeleteAsync(_license.Id);
_licenses = 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) => _license = null;
protected override async Task OnInitializedAsync() => _licenses = await Service.GetAsync();
}
}

View File

@@ -162,4 +162,16 @@
<value>Eliminar</value>
<comment>Delete</comment>
</data>
<data name="Delete license" xml:space="preserve">
<value>Eliminar licencia</value>
<comment>Delete license</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} license name</comment>
</data>
<data name="Cancel" xml:space="preserve">
<value>Cancelar</value>
<comment>Cancel</comment>
</data>
</root>

View File

@@ -13,6 +13,22 @@ namespace Marechai.Services
public LicensesService(MarechaiContext context) => _context = context;
public async Task<List<License>> GetAsync() =>
await _context.Licenses.OrderBy(l => l.Name).Select(l => new License(){FsfApproved = l.FsfApproved, Id= l.Id, Link = l.Link, Name = l.Name, OsiApproved = l.OsiApproved, SPDX = l.SPDX}).ToListAsync();
await _context.Licenses.OrderBy(l => l.Name).Select(l => new License
{
FsfApproved = l.FsfApproved, Id = l.Id, Link = l.Link, Name = l.Name,
OsiApproved = l.OsiApproved, SPDX = l.SPDX
}).ToListAsync();
public async Task DeleteAsync(int id)
{
License item = await _context.Licenses.FindAsync(id);
if(item is null)
return;
_context.Licenses.Remove(item);
await _context.SaveChangesAsync();
}
}
}