Add code to delete news.

This commit is contained in:
2020-05-24 20:36:56 +01:00
parent 92137d477b
commit 9f780713c7
6 changed files with 85 additions and 161 deletions

View File

@@ -1,78 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : NewsController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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<IActionResult> Index() =>
View(await _context.News.OrderByDescending(n => n.Date).ToListAsync());
// GET: Admin/News/Delete/5
public async Task<IActionResult> 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<IActionResult> 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);
}
}

View File

@@ -1,69 +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.News
@{
ViewData["Title"] = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>News</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Date)
</dt>
<dd>
@Html.DisplayFor(model => model.Date)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Type)
</dt>
<dd>
@Html.DisplayFor(model => model.Type)
</dd>
<dt>
@Html.DisplayNameFor(model => model.AddedId)
</dt>
<dd>
@Html.DisplayFor(model => model.AddedId)
</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

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1069</Version> <Version>3.0.99.1098</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>
@@ -88,5 +88,6 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Details.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Edit.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Index.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Index.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -31,7 +31,6 @@
} }
@page "/admin/news" @page "/admin/news"
@using Marechai.Database.Models
@inherits OwningComponentBase<NewsService> @inherits OwningComponentBase<NewsService>
@inject IStringLocalizer<NewsService> L @inject IStringLocalizer<NewsService> L
@attribute [Authorize(Roles = "UberAdmin, Admin")] @attribute [Authorize(Roles = "UberAdmin, Admin")]
@@ -71,21 +70,26 @@
@item.AffectedId @item.AffectedId
</td> </td>
<td> <td>
<span class="btn btn-danger"> <Button Color="Color.Danger" Clicked="() => {ShowModal(item.Id);}">@L["Delete"]</Button>
@L["Delete"]
</span>
</td> </td>
</tr> </tr>
} }
</tbody> </tbody>
</table> </table>
@code <Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
{ <ModalBackdrop/>
List<NewsViewModel> _news; <ModalContent Centered="true">
<ModalHeader>
protected override async Task OnInitializedAsync() <ModalTitle>Delete news</ModalTitle>
{ <CloseButton Clicked="@HideModal"/>
_news = await Service.GetAsync(); </ModalHeader>
} <ModalBody>
} <Text>@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)</Text>
</ModalBody>
<ModalFooter>
<Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">Close</Button>
<Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">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 News
{
NewsViewModel _currentNews;
bool _deleteInProgress;
Modal _frmDelete;
List<NewsViewModel> _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;
}
}

View File

@@ -28,8 +28,10 @@
// Copyright © 2003-2020 Natalia Portillo // Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/ *******************************************************************************/
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Marechai.Database; using Marechai.Database;
using Marechai.Database.Models; using Marechai.Database.Models;
@@ -126,5 +128,17 @@ namespace Marechai.Services
return news; 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();
}
} }
} }