2018-08-11 10:50:22 +01:00
|
|
|
|
/******************************************************************************
|
2020-02-10 01:52:56 +00:00
|
|
|
|
// MARECHAI: Master repository of computing history artifacts information
|
2018-08-11 10:50:22 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : MachinesController.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Machines 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
|
2018-08-11 10:50:22 +01:00
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
using System;
|
2018-08-11 10:50:22 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
using Marechai.Areas.Admin.Models;
|
2020-02-10 02:10:18 +00:00
|
|
|
|
using Marechai.Database;
|
|
|
|
|
|
using Marechai.Database.Models;
|
2019-05-18 03:02:49 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-08-11 10:50:22 +01:00
|
|
|
|
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
|
2018-08-11 10:50:22 +01:00
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[Area("Admin"), Authorize]
|
2018-08-11 10:50:22 +01:00
|
|
|
|
public class MachinesController : Controller
|
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
|
readonly MarechaiContext _context;
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
public MachinesController(MarechaiContext context) => _context = context;
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Machines
|
|
|
|
|
|
public async Task<IActionResult> Index()
|
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
|
IIncludableQueryable<Machine, MachineFamily> marechaiContext =
|
2018-08-11 10:50:22 +01:00
|
|
|
|
_context.Machines.Include(m => m.Company).Include(m => m.Family);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
|
|
|
|
|
return View(await marechaiContext.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
|
|
|
|
|
|
ThenBy(m => m.Family.Name).Select(m => new MachineViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = m.Id, Company = m.Company.Name, Name = m.Name,
|
|
|
|
|
|
Model = m.Model,
|
|
|
|
|
|
Introduced = m.Introduced, Type = m.Type, Family = m.Family.Name
|
|
|
|
|
|
}).ToListAsync());
|
2018-08-11 10:50:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Machines/Details/5
|
|
|
|
|
|
public async Task<IActionResult> Details(int? id)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
Machine machine = await _context.Machines.Include(m => m.Company).Include(m => m.Family).
|
|
|
|
|
|
FirstOrDefaultAsync(m => m.Id == id);
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(machine == null)
|
|
|
|
|
|
return NotFound();
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
|
|
|
|
|
return View(machine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Machines/Create
|
|
|
|
|
|
public IActionResult Create()
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name");
|
2018-08-11 10:50:22 +01:00
|
|
|
|
ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name");
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2018-08-11 10:50:22 +01:00
|
|
|
|
return View();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Admin/Machines/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
|
2018-08-11 10:50:22 +01:00
|
|
|
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[HttpPost, ValidateAntiForgeryToken]
|
2018-08-11 10:50:22 +01:00
|
|
|
|
public async Task<IActionResult> Create([Bind("Id,CompanyId,Name,Type,Introduced,FamilyId,Model")]
|
|
|
|
|
|
Machine machine)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(ModelState.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.Add(machine);
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
2019-05-19 17:18:10 +01:00
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
var newsType = new NewsType();
|
|
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
switch(machine.Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case MachineType.Computer:
|
|
|
|
|
|
newsType = NewsType.NewComputerInDb;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case MachineType.Console:
|
|
|
|
|
|
newsType = NewsType.NewConsoleInDb;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
_context.News.Add(new News
|
|
|
|
|
|
{
|
|
|
|
|
|
AddedId = machine.Id, Date = DateTime.UtcNow, Type = newsType
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
2018-08-11 10:50:22 +01:00
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machine.CompanyId);
|
2018-08-11 10:50:22 +01:00
|
|
|
|
ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name", machine.FamilyId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2018-08-11 10:50:22 +01:00
|
|
|
|
return View(machine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Machines/Edit/5
|
|
|
|
|
|
public async Task<IActionResult> Edit(int? id)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id == null)
|
|
|
|
|
|
return NotFound();
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
|
|
|
|
|
Machine machine = await _context.Machines.FindAsync(id);
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(machine == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machine.CompanyId);
|
2018-08-11 10:50:22 +01:00
|
|
|
|
ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name", machine.FamilyId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2018-08-11 10:50:22 +01:00
|
|
|
|
return View(machine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Admin/Machines/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
|
2018-08-11 10:50:22 +01:00
|
|
|
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[HttpPost, ValidateAntiForgeryToken]
|
2018-08-11 10:50:22 +01:00
|
|
|
|
public async Task<IActionResult> Edit(int id, [Bind("Id,CompanyId,Name,Type,Introduced,FamilyId,Model")]
|
|
|
|
|
|
Machine machine)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id != machine.Id)
|
|
|
|
|
|
return NotFound();
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
|
|
|
|
|
if(ModelState.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.Update(machine);
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
2019-05-19 17:18:10 +01:00
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
var newsType = new NewsType();
|
|
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
switch(machine.Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case MachineType.Computer:
|
|
|
|
|
|
newsType = NewsType.UpdatedComputerInDb;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case MachineType.Console:
|
|
|
|
|
|
newsType = NewsType.UpdatedConsoleInDb;
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
_context.News.Add(new News
|
|
|
|
|
|
{
|
|
|
|
|
|
AddedId = machine.Id, Date = DateTime.UtcNow, Type = newsType
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2019-05-19 17:18:10 +01:00
|
|
|
|
await _context.SaveChangesAsync();
|
2018-08-11 10:50:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch(DbUpdateConcurrencyException)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(!MachineExists(machine.Id))
|
|
|
|
|
|
return NotFound();
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machine.CompanyId);
|
2018-08-11 10:50:22 +01:00
|
|
|
|
ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name", machine.FamilyId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2018-08-11 10:50:22 +01:00
|
|
|
|
return View(machine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/Machines/Delete/5
|
|
|
|
|
|
public async Task<IActionResult> Delete(int? id)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
Machine machine = await _context.Machines.Include(m => m.Company).Include(m => m.Family).
|
|
|
|
|
|
FirstOrDefaultAsync(m => m.Id == id);
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(machine == null)
|
|
|
|
|
|
return NotFound();
|
2018-08-11 10:50:22 +01:00
|
|
|
|
|
|
|
|
|
|
return View(machine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Admin/Machines/Delete/5
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
2018-08-11 10:50:22 +01:00
|
|
|
|
public async Task<IActionResult> DeleteConfirmed(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
Machine machine = await _context.Machines.FindAsync(id);
|
|
|
|
|
|
_context.Machines.Remove(machine);
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2018-08-11 10:50:22 +01:00
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
bool MachineExists(int id) => _context.Machines.Any(e => e.Id == id);
|
2018-08-11 10:50:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|