mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
157 lines
5.0 KiB
C#
157 lines
5.0 KiB
C#
/******************************************************************************
|
|
// Canary Islands Computer Museum Website
|
|
// ----------------------------------------------------------------------------
|
|
//
|
|
// 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-2018 Natalia Portillo
|
|
*******************************************************************************/
|
|
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Cicm.Database.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace cicm_web.Areas.Admin.Controllers
|
|
{
|
|
[Area("Admin")]
|
|
public class NewsController : Controller
|
|
{
|
|
readonly cicmContext _context;
|
|
|
|
public NewsController(cicmContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: Admin/News
|
|
public async Task<IActionResult> Index()
|
|
{
|
|
return 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()
|
|
{
|
|
return 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);
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
return _context.News.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
} |