Do not allow news to be edited or created manually.

This commit is contained in:
2019-05-19 00:01:35 +01:00
parent c93e29b181
commit 9e3c0e7ea7
7 changed files with 9 additions and 315 deletions

View File

@@ -29,6 +29,7 @@
*******************************************************************************/
using System;
using System.ComponentModel;
namespace Cicm.Database.Models
{
@@ -37,6 +38,7 @@ namespace Cicm.Database.Models
public int Id { get; set; }
public DateTime Date { get; set; }
public NewsType Type { get; set; }
[DisplayName("Affected ID")]
public int AddedId { get; set; }
}
}

View File

@@ -49,78 +49,8 @@ namespace cicm_web.Areas.Admin.Controllers
}
// GET: Admin/News
public async Task<IActionResult> Index() => View(await _context.News.ToListAsync());
// GET: Admin/News/Details/5
public async Task<IActionResult> Details(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);
}
// GET: Admin/News/Create
public IActionResult Create() => View();
// POST: Admin/News/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Date,Type,AddedId")] News news)
{
if(ModelState.IsValid)
{
_context.Add(news);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(news);
}
// GET: Admin/News/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null) return NotFound();
News news = await _context.News.FindAsync(id);
if(news == null) return NotFound();
return View(news);
}
// POST: Admin/News/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Date,Type,AddedId")] News news)
{
if(id != news.Id) return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(news);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!NewsExists(news.Id)) return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
return View(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)

View File

@@ -1,59 +0,0 @@
@{
/******************************************************************************
// Canary Islands Computer Museum Website
// ----------------------------------------------------------------------------
//
// Filename : Create.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ 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-2018 Natalia Portillo
*******************************************************************************/
}
@model Cicm.Database.Models.News
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>News</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View File

@@ -1,67 +0,0 @@
@{
/******************************************************************************
// Canary Islands Computer Museum Website
// ----------------------------------------------------------------------------
//
// Filename : Details.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view details
//
// --[ 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-2018 Natalia Portillo
*******************************************************************************/
}
@model Cicm.Database.Models.News
@{
ViewData["Title"] = "Details";
}
<h2>Details</h2>
<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>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id" class="btn btn-primary">Edit</a>
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</div>

View File

@@ -1,96 +0,0 @@
@{
/******************************************************************************
// Canary Islands Computer Museum Website
// ----------------------------------------------------------------------------
//
// Filename : Edit.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view edit
//
// --[ 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-2018 Natalia Portillo
*******************************************************************************/
}
@model Cicm.Database.Models.News
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<h4>News</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly"
class="text-danger">
</div>
<input type="hidden"
asp-for="Id" />
<div class="form-group">
<label asp-for="Date"
class="control-label">
</label>
<input asp-for="Date"
class="form-control" />
<span asp-validation-for="Date"
class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Type"
class="control-label">
</label>
<select asp-for="Type"
class="form-control">
</select>
<span asp-validation-for="Type"
class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="AddedId"
class="control-label">
</label>
<input asp-for="AddedId"
class="form-control" />
<span asp-validation-for="AddedId"
class="text-danger">
</span>
</div>
<div class="form-group">
<input class="btn btn-primary"
type="submit"
value="Save" />
<a asp-action="Index"
class="btn btn-secondary">
Back to List
</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -38,12 +38,6 @@
<h2>News</h2>
<p>
<a asp-action="Create"
class="btn btn-primary">
Create New
</a>
</p>
<table class="table">
<thead>
<tr>
@@ -73,16 +67,6 @@
@Html.DisplayFor(modelItem => item.AddedId)
</td>
<td>
<a asp-action="Details"
asp-route-id="@item.Id"
class="btn btn-primary">
Details
</a>
<a asp-action="Edit"
asp-route-id="@item.Id"
class="btn btn-secondary">
Edit
</a>
<a asp-action="Delete"
asp-route-id="@item.Id"
class="btn btn-danger">

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<Version>3.0.99.441</Version>
<Version>3.0.99.443</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>