Files
marechai/Marechai/Areas/Admin/Controllers/GpusController.cs

187 lines
6.5 KiB
C#
Raw Normal View History

/******************************************************************************
2020-02-10 01:52:56 +00:00
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : GpusController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// GPUs 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/>.
//
// ----------------------------------------------------------------------------
2020-02-10 03:05:39 +00:00
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
using System.Linq;
using System.Threading.Tasks;
2020-02-10 02:20:48 +00:00
using Marechai.Areas.Admin.Models;
2020-02-10 22:44:18 +00:00
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
{
2020-02-10 22:44:18 +00:00
[Area("Admin"), Authorize]
public class GpusController : Controller
{
2020-02-10 02:47:39 +00:00
readonly MarechaiContext _context;
2020-02-10 22:44:18 +00:00
public GpusController(MarechaiContext context) => _context = context;
// GET: Admin/Gpus
public async Task<IActionResult> Index()
{
2020-02-10 02:47:39 +00:00
IIncludableQueryable<Gpu, Company> marechaiContext = _context.Gpus.Include(g => g.Company);
2020-02-10 22:44:18 +00:00
return View(await marechaiContext.OrderBy(g => g.Company.Name).ThenBy(g => g.Name).
ThenBy(g => g.Introduced).Select(g => new GpuViewModel
{
Id = g.Id, Company = g.Company.Name,
Introduced = g.Introduced,
ModelCode = g.ModelCode, Name = g.Name
}).ToListAsync());
}
// GET: Admin/Gpus/Details/5
public async Task<IActionResult> Details(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
Gpu gpu = await _context.Gpus.Include(g => g.Company).FirstOrDefaultAsync(m => m.Id == id);
2020-02-10 22:44:18 +00:00
if(gpu == null)
return NotFound();
return View(gpu);
}
// GET: Admin/Gpus/Create
public IActionResult Create()
{
2019-05-19 21:55:47 +01:00
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name");
2020-02-10 22:44:18 +00:00
return View();
}
// POST: Admin/Gpus/Create
2020-02-10 03:05:39 +00:00
// 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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create(
[Bind("Id,Name,CompanyId,ModelCode,Introduced,Package,Process,ProcessNm,DieSize,Transistors")]
Gpu gpu)
{
if(ModelState.IsValid)
{
_context.Add(gpu);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
return RedirectToAction(nameof(Index));
}
2019-05-19 21:55:47 +01:00
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", gpu.CompanyId);
2020-02-10 22:44:18 +00:00
return View(gpu);
}
// GET: Admin/Gpus/Edit/5
public async Task<IActionResult> Edit(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
Gpu gpu = await _context.Gpus.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(gpu == null)
return NotFound();
2019-05-19 21:55:47 +01:00
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", gpu.CompanyId);
2020-02-10 22:44:18 +00:00
return View(gpu);
}
// POST: Admin/Gpus/Edit/5
2020-02-10 03:05:39 +00:00
// 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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
int id, [Bind("Id,Name,CompanyId,ModelCode,Introduced,Package,Process,ProcessNm,DieSize,Transistors")]
Gpu gpu)
{
2020-02-10 22:44:18 +00:00
if(id != gpu.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(gpu);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
2020-02-10 22:44:18 +00:00
if(!GpuExists(gpu.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
2019-05-19 21:55:47 +01:00
ViewData["CompanyId"] =
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", gpu.CompanyId);
2020-02-10 22:44:18 +00:00
return View(gpu);
}
// GET: Admin/Gpus/Delete/5
public async Task<IActionResult> Delete(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
Gpu gpu = await _context.Gpus.Include(g => g.Company).FirstOrDefaultAsync(m => m.Id == id);
2020-02-10 22:44:18 +00:00
if(gpu == null)
return NotFound();
return View(gpu);
}
// POST: Admin/Gpus/Delete/5
2020-02-10 22:44:18 +00:00
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
Gpu gpu = await _context.Gpus.FindAsync(id);
_context.Gpus.Remove(gpu);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
bool GpuExists(int id) => _context.Gpus.Any(e => e.Id == id);
}
}