Add code to delete companies.

This commit is contained in:
2020-05-24 20:53:29 +01:00
parent d554552a90
commit 14fe17f04b
6 changed files with 107 additions and 154 deletions

View File

@@ -1,129 +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.Company
@{
ViewData["Title"] = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Company</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Founded)
</dt>
<dd>
@Html.DisplayFor(model => model.Founded)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Website)
</dt>
<dd>
@Html.DisplayFor(model => model.Website)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Twitter)
</dt>
<dd>
@Html.DisplayFor(model => model.Twitter)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Facebook)
</dt>
<dd>
@Html.DisplayFor(model => model.Facebook)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Sold)
</dt>
<dd>
@Html.DisplayFor(model => model.Sold)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
<dt>
@Html.DisplayNameFor(model => model.City)
</dt>
<dd>
@Html.DisplayFor(model => model.City)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Province)
</dt>
<dd>
@Html.DisplayFor(model => model.Province)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PostalCode)
</dt>
<dd>
@Html.DisplayFor(model => model.PostalCode)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Status)
</dt>
<dd>
@Html.DisplayFor(model => model.Status)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Country)
</dt>
<dd>
@Html.DisplayFor(model => model.Country.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.SoldTo)
</dt>
<dd>
@Html.DisplayFor(model => model.SoldTo.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

@@ -94,21 +94,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<CompanyViewModel> _companies;
protected override async Task OnInitializedAsync()
{
_companies = await Service.GetCompaniesAsync();
}
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<ModalBackdrop/>
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@L["Delete company"]</ModalTitle>
<CloseButton Clicked="@HideModal"/>
</ModalHeader>
<ModalBody>
<Text>@string.Format(@L["Are you sure you want to delete the company {0}?"], _currentCompany?.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,53 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blazorise;
using Marechai.ViewModels;
namespace Marechai.Pages.Admin
{
public partial class Companies
{
List<CompanyViewModel> _companies;
CompanyViewModel _currentCompany;
bool _deleteInProgress;
Modal _frmDelete;
protected override async Task OnInitializedAsync() => _companies = await Service.GetAsync();
void ShowModal(int itemId)
{
_currentCompany = _companies.FirstOrDefault(n => n.Id == itemId);
_frmDelete.Show();
}
void HideModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
if(_currentCompany is null)
return;
_deleteInProgress = true;
_companies = null;
// Yield thread to let UI to update
await Task.Yield();
await Service.DeleteAsync(_currentCompany.Id);
_companies = 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) => _currentCompany = null;
}
}

View File

@@ -150,6 +150,6 @@
CountryId = null;
}
_companies ??= await Service.GetCompaniesAsync();
_companies ??= await Service.GetAsync();
}
}

View File

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

View File

@@ -49,18 +49,18 @@ namespace Marechai.Services
_l = localizer;
}
public Task<List<CompanyViewModel>> GetCompaniesAsync() => _context.
Companies.Include(c => c.Logos).OrderBy(c => c.Name).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.
Logos.OrderByDescending(l => l.Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded,
Sold = c.Sold, SoldTo = c.SoldTo.Name,
Country = c.Country.Name, Status = c.Status
}).ToListAsync();
public Task<List<CompanyViewModel>> GetAsync() => _context.
Companies.Include(c => c.Logos).OrderBy(c => c.Name).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.
Logos.OrderByDescending(l => l.Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded, Sold = c.Sold,
SoldTo = c.SoldTo.Name, Country = c.Country.Name,
Status = c.Status
}).ToListAsync();
public Task<Company> GetCompanyAsync(int id) => _context.Companies.FirstOrDefaultAsync(c => c.Id == id);
@@ -122,5 +122,17 @@ namespace Marechai.Services
FirstOrDefault().Guid,
Name = c.Name
}).ToListAsync();
public async Task DeleteAsync(int id)
{
Company item = await _context.Companies.FindAsync(id);
if(item is null)
return;
_context.Companies.Remove(item);
await _context.SaveChangesAsync();
}
}
}