diff --git a/Marechai/Areas/Admin/Controllers/NewsController.cs b/Marechai/Areas/Admin/Controllers/NewsController.cs deleted file mode 100644 index 5358fbaa..00000000 --- a/Marechai/Areas/Admin/Controllers/NewsController.cs +++ /dev/null @@ -1,78 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : NewsController.cs -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// News admin controller -// -// --[ 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 -*******************************************************************************/ - -using System.Linq; -using System.Threading.Tasks; -using Marechai.Database.Models; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; - -namespace Marechai.Areas.Admin.Controllers -{ - [Area("Admin"), Authorize] - public class NewsController : Controller - { - readonly MarechaiContext _context; - - public NewsController(MarechaiContext context) => _context = context; - - // GET: Admin/News - public async Task Index() => - View(await _context.News.OrderByDescending(n => n.Date).ToListAsync()); - - // GET: Admin/News/Delete/5 - public async Task Delete(int? id) - { - if(id == null) - return NotFound(); - - News news = await _context.News.FirstOrDefaultAsync(m => m.Id == id); - - if(news == null) - return NotFound(); - - return View(news); - } - - // POST: Admin/News/Delete/5 - [HttpPost, ActionName("Delete"), ValidateAntiForgeryToken] - public async Task DeleteConfirmed(int id) - { - News news = await _context.News.FindAsync(id); - _context.News.Remove(news); - await _context.SaveChangesAsync(); - - return RedirectToAction(nameof(Index)); - } - - bool NewsExists(int id) => _context.News.Any(e => e.Id == id); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/News/Delete.cshtml b/Marechai/Areas/Admin/Views/News/Delete.cshtml deleted file mode 100644 index 4a0c0d52..00000000 --- a/Marechai/Areas/Admin/Views/News/Delete.cshtml +++ /dev/null @@ -1,69 +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.News - -@{ - ViewData["Title"] = "Delete"; -} -

Delete

-

Are you sure you want to delete this?

-
-

News

-
-
-
- @Html.DisplayNameFor(model => model.Date) -
-
- @Html.DisplayFor(model => model.Date) -
-
- @Html.DisplayNameFor(model => model.Type) -
-
- @Html.DisplayFor(model => model.Type) -
-
- @Html.DisplayNameFor(model => model.AddedId) -
-
- @Html.DisplayFor(model => model.AddedId) -
-
-
- - - - Back to List - -
-
\ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 2500317b..c1e08b2d 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1069 + 3.0.99.1098 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website @@ -88,5 +88,6 @@ <_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Details.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Edit.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Index.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/News.razor b/Marechai/Pages/Admin/News.razor index 4a0f8fde..12b8cf05 100644 --- a/Marechai/Pages/Admin/News.razor +++ b/Marechai/Pages/Admin/News.razor @@ -31,7 +31,6 @@ } @page "/admin/news" -@using Marechai.Database.Models @inherits OwningComponentBase @inject IStringLocalizer L @attribute [Authorize(Roles = "UberAdmin, Admin")] @@ -71,21 +70,26 @@ @item.AffectedId - - @L["Delete"] - + } -@code -{ - List _news; - - protected override async Task OnInitializedAsync() - { - _news = await Service.GetAsync(); - } -} \ No newline at end of file + + + + + Delete news + + + + @string.Format(@L["Are you sure you want to delete the news type {0} from {1} that affect artifact ID {2}?"], _currentNews?.Type, _currentNews?.Timestamp, _currentNews?.AffectedId) + + + + + + + diff --git a/Marechai/Pages/Admin/News.razor.cs b/Marechai/Pages/Admin/News.razor.cs new file mode 100644 index 00000000..15aa8640 --- /dev/null +++ b/Marechai/Pages/Admin/News.razor.cs @@ -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 News + { + NewsViewModel _currentNews; + bool _deleteInProgress; + Modal _frmDelete; + List _news; + + protected override async Task OnInitializedAsync() => _news = await Service.GetAsync(); + + void ShowModal(int itemId) + { + _currentNews = _news.FirstOrDefault(n => n.Id == itemId); + _frmDelete.Show(); + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_currentNews is null) + return; + + _deleteInProgress = true; + _news = null; + + // Yield thread to let UI to update + await Task.Yield(); + + await Service.DeleteAsync(_currentNews.Id); + _news = 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) => _currentNews = null; + } +} \ No newline at end of file diff --git a/Marechai/Services/NewsService.cs b/Marechai/Services/NewsService.cs index 8d905ba0..be8efd45 100644 --- a/Marechai/Services/NewsService.cs +++ b/Marechai/Services/NewsService.cs @@ -28,8 +28,10 @@ // Copyright © 2003-2020 Natalia Portillo *******************************************************************************/ +using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Marechai.Database; using Marechai.Database.Models; @@ -126,5 +128,17 @@ namespace Marechai.Services return news; } + + public async Task DeleteAsync(int id) + { + News item = await _context.News.FindAsync(id); + + if(item is null) + return; + + _context.News.Remove(item); + + await _context.SaveChangesAsync(); + } } } \ No newline at end of file