diff --git a/Marechai/Areas/Admin/Views/Companies/Delete.cshtml b/Marechai/Areas/Admin/Views/Companies/Delete.cshtml deleted file mode 100644 index ed61eaec..00000000 --- a/Marechai/Areas/Admin/Views/Companies/Delete.cshtml +++ /dev/null @@ -1,129 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Delete.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model Marechai.Database.Models.Company - -@{ - ViewData["Title"] = "Delete"; -} -

Delete

-

Are you sure you want to delete this?

-
-

Company

-
-
-
- @Html.DisplayNameFor(model => model.Name) -
-
- @Html.DisplayFor(model => model.Name) -
-
- @Html.DisplayNameFor(model => model.Founded) -
-
- @Html.DisplayFor(model => model.Founded) -
-
- @Html.DisplayNameFor(model => model.Website) -
-
- @Html.DisplayFor(model => model.Website) -
-
- @Html.DisplayNameFor(model => model.Twitter) -
-
- @Html.DisplayFor(model => model.Twitter) -
-
- @Html.DisplayNameFor(model => model.Facebook) -
-
- @Html.DisplayFor(model => model.Facebook) -
-
- @Html.DisplayNameFor(model => model.Sold) -
-
- @Html.DisplayFor(model => model.Sold) -
-
- @Html.DisplayNameFor(model => model.Address) -
-
- @Html.DisplayFor(model => model.Address) -
-
- @Html.DisplayNameFor(model => model.City) -
-
- @Html.DisplayFor(model => model.City) -
-
- @Html.DisplayNameFor(model => model.Province) -
-
- @Html.DisplayFor(model => model.Province) -
-
- @Html.DisplayNameFor(model => model.PostalCode) -
-
- @Html.DisplayFor(model => model.PostalCode) -
-
- @Html.DisplayNameFor(model => model.Status) -
-
- @Html.DisplayFor(model => model.Status) -
-
- @Html.DisplayNameFor(model => model.Country) -
-
- @Html.DisplayFor(model => model.Country.Name) -
-
- @Html.DisplayNameFor(model => model.SoldTo) -
-
- @Html.DisplayFor(model => model.SoldTo.Name) -
-
-
- - - - Back to List - -
-
\ No newline at end of file diff --git a/Marechai/Pages/Admin/Companies.razor b/Marechai/Pages/Admin/Companies.razor index ebe3d824..8c6d7f00 100644 --- a/Marechai/Pages/Admin/Companies.razor +++ b/Marechai/Pages/Admin/Companies.razor @@ -94,21 +94,26 @@ @L["Edit"] - - @L["Delete"] - + } -@code -{ - List _companies; - - protected override async Task OnInitializedAsync() - { - _companies = await Service.GetCompaniesAsync(); - } -} \ No newline at end of file + + + + + @L["Delete company"] + + + + @string.Format(@L["Are you sure you want to delete the company {0}?"], _currentCompany?.Name) + + + + + + + diff --git a/Marechai/Pages/Admin/Companies.razor.cs b/Marechai/Pages/Admin/Companies.razor.cs new file mode 100644 index 00000000..3d97a941 --- /dev/null +++ b/Marechai/Pages/Admin/Companies.razor.cs @@ -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 _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; + } +} \ No newline at end of file diff --git a/Marechai/Pages/Companies/Index.razor b/Marechai/Pages/Companies/Index.razor index d0c5d88c..f54fb935 100644 --- a/Marechai/Pages/Companies/Index.razor +++ b/Marechai/Pages/Companies/Index.razor @@ -150,6 +150,6 @@ CountryId = null; } - _companies ??= await Service.GetCompaniesAsync(); + _companies ??= await Service.GetAsync(); } } \ No newline at end of file diff --git a/Marechai/Resources/Services/CompaniesService.es.resx b/Marechai/Resources/Services/CompaniesService.es.resx index 171c12b3..0f351826 100644 --- a/Marechai/Resources/Services/CompaniesService.es.resx +++ b/Marechai/Resources/Services/CompaniesService.es.resx @@ -278,4 +278,16 @@ Eliminar Delete + + Eliminar compañía + Delete company + + + ¿Está seguro de que desea borrar la compañía {0}? + {0} company name + + + Cancelar + Cancel + \ No newline at end of file diff --git a/Marechai/Services/CompaniesService.cs b/Marechai/Services/CompaniesService.cs index 3396fea7..ccbdbd12 100644 --- a/Marechai/Services/CompaniesService.cs +++ b/Marechai/Services/CompaniesService.cs @@ -49,18 +49,18 @@ namespace Marechai.Services _l = localizer; } - public Task> 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> 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 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(); + } } } \ No newline at end of file