2019-05-30 21:26:44 +01:00
|
|
|
|
/******************************************************************************
|
2020-02-10 01:52:56 +00:00
|
|
|
|
// MARECHAI: Master repository of computing history artifacts information
|
2019-05-30 21:26:44 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : MemoryByOwnedMachinesController.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Memory by 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
|
2019-05-30 21:26:44 +01:00
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2019-05-30 21:26:44 +01:00
|
|
|
|
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
|
2019-05-30 21:26:44 +01:00
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[Area("Admin"), Authorize]
|
2019-05-30 21:26:44 +01:00
|
|
|
|
public class MemoryByOwnedMachinesController : Controller
|
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
|
readonly MarechaiContext _context;
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
public MemoryByOwnedMachinesController(MarechaiContext context) => _context = context;
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
|
// GET: Admin/MemoryByOwnedMachines
|
|
|
|
|
|
public async Task<IActionResult> Index()
|
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
|
IIncludableQueryable<MemoryByOwnedMachine, OwnedMachine> marechaiContext =
|
2019-05-30 21:26:44 +01:00
|
|
|
|
_context.MemoryByOwnedMachine.Include(m => m.OwnedMachine);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
|
|
|
|
|
return View(await marechaiContext.OrderBy(m => m.OwnedMachine.Machine.Company.Name).
|
|
|
|
|
|
ThenBy(m => m.OwnedMachine.Machine.Name).
|
|
|
|
|
|
ThenBy(m => m.OwnedMachine.User.UserName).ThenBy(m => m.Usage).
|
|
|
|
|
|
ThenBy(m => m.Size).ThenBy(m => m.Type).
|
|
|
|
|
|
Select(m => new MemoryByMachineViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = m.Id,
|
|
|
|
|
|
Machine =
|
|
|
|
|
|
$"{m.OwnedMachine.Machine.Company.Name} {m.OwnedMachine.Machine.Name} <{m.OwnedMachine.User.UserName}>",
|
|
|
|
|
|
Size = m.Size, Speed = m.Speed, Type = m.Type, Usage = m.Usage
|
|
|
|
|
|
}).ToListAsync());
|
2019-05-30 21:26:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/MemoryByOwnedMachines/Details/5
|
|
|
|
|
|
public async Task<IActionResult> Details(long? id)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id == null)
|
|
|
|
|
|
return NotFound();
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
2020-02-10 02:47:39 +00:00
|
|
|
|
IIncludableQueryable<MemoryByOwnedMachine, OwnedMachine> marechaiContext =
|
2019-05-30 21:26:44 +01:00
|
|
|
|
_context.MemoryByOwnedMachine.Include(m => m.OwnedMachine);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
|
|
|
|
|
MemoryByMachineViewModel memoryByOwnedMachine =
|
|
|
|
|
|
await marechaiContext.OrderBy(m => m.OwnedMachine.Machine.Company.Name).
|
|
|
|
|
|
ThenBy(m => m.OwnedMachine.Machine.Name).
|
|
|
|
|
|
ThenBy(m => m.OwnedMachine.User.UserName).ThenBy(m => m.Usage).
|
|
|
|
|
|
ThenBy(m => m.Size).ThenBy(m => m.Type).Select(m => new MemoryByMachineViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = m.Id,
|
|
|
|
|
|
Machine =
|
|
|
|
|
|
$"{m.OwnedMachine.Machine.Company.Name} {m.OwnedMachine.Machine.Name} <{m.OwnedMachine.User.UserName}>",
|
|
|
|
|
|
Size = m.Size, Speed = m.Speed, Type = m.Type, Usage = m.Usage
|
|
|
|
|
|
}).FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
|
|
if(memoryByOwnedMachine == null)
|
|
|
|
|
|
return NotFound();
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
|
return View(memoryByOwnedMachine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/MemoryByOwnedMachines/Create
|
|
|
|
|
|
public IActionResult Create()
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
ViewData["OwnedMachineId"] = new SelectList(_context.OwnedMachines.OrderBy(m => m.Machine.Company.Name).
|
|
|
|
|
|
ThenBy(m => m.Machine.Name).
|
|
|
|
|
|
ThenBy(m => m.User.UserName).Select(m => new
|
|
|
|
|
|
{
|
|
|
|
|
|
m.Id,
|
|
|
|
|
|
Name =
|
|
|
|
|
|
$"{m.Machine.Company.Name} {m.Machine.Name} <{m.User.UserName}>"
|
|
|
|
|
|
}), "Id", "Name");
|
|
|
|
|
|
|
2019-05-30 21:26:44 +01:00
|
|
|
|
return View();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Admin/MemoryByOwnedMachines/Create
|
2020-02-10 22:44:18 +00:00
|
|
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
2019-05-30 21:26:44 +01:00
|
|
|
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[HttpPost, ValidateAntiForgeryToken]
|
2019-05-30 21:26:44 +01:00
|
|
|
|
public async Task<IActionResult> Create([Bind("OwnedMachineId,Type,Usage,Size,Speed,Id")]
|
|
|
|
|
|
MemoryByOwnedMachine memoryByOwnedMachine)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(ModelState.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.Add(memoryByOwnedMachine);
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2019-05-30 21:26:44 +01:00
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
ViewData["OwnedMachineId"] = new SelectList(_context.OwnedMachines.OrderBy(m => m.Machine.Company.Name).
|
|
|
|
|
|
ThenBy(m => m.Machine.Name).
|
|
|
|
|
|
ThenBy(m => m.User.UserName).Select(m => new
|
|
|
|
|
|
{
|
|
|
|
|
|
m.Id,
|
|
|
|
|
|
Name =
|
|
|
|
|
|
$"{m.Machine.Company.Name} {m.Machine.Name} <{m.User.UserName}>"
|
|
|
|
|
|
}), "Id", "Name", memoryByOwnedMachine.OwnedMachineId);
|
|
|
|
|
|
|
2019-05-30 21:26:44 +01:00
|
|
|
|
return View(memoryByOwnedMachine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/MemoryByOwnedMachines/Edit/5
|
|
|
|
|
|
public async Task<IActionResult> Edit(long? id)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id == null)
|
|
|
|
|
|
return NotFound();
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
|
MemoryByOwnedMachine memoryByOwnedMachine = await _context.MemoryByOwnedMachine.FindAsync(id);
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(memoryByOwnedMachine == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
ViewData["OwnedMachineId"] = new SelectList(_context.OwnedMachines.OrderBy(m => m.Machine.Company.Name).
|
|
|
|
|
|
ThenBy(m => m.Machine.Name).
|
|
|
|
|
|
ThenBy(m => m.User.UserName).Select(m => new
|
|
|
|
|
|
{
|
|
|
|
|
|
m.Id,
|
|
|
|
|
|
Name =
|
|
|
|
|
|
$"{m.Machine.Company.Name} {m.Machine.Name} <{m.User.UserName}>"
|
|
|
|
|
|
}), "Id", "Name", memoryByOwnedMachine.OwnedMachineId);
|
|
|
|
|
|
|
2019-05-30 21:26:44 +01:00
|
|
|
|
return View(memoryByOwnedMachine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Admin/MemoryByOwnedMachines/Edit/5
|
2020-02-10 22:44:18 +00:00
|
|
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
2019-05-30 21:26:44 +01:00
|
|
|
|
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[HttpPost, ValidateAntiForgeryToken]
|
2019-05-30 21:26:44 +01:00
|
|
|
|
public async Task<IActionResult> Edit(long id, [Bind("OwnedMachineId,Type,Usage,Size,Speed,Id")]
|
|
|
|
|
|
MemoryByOwnedMachine memoryByOwnedMachine)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id != memoryByOwnedMachine.Id)
|
|
|
|
|
|
return NotFound();
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
|
if(ModelState.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_context.Update(memoryByOwnedMachine);
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(DbUpdateConcurrencyException)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(!MemoryByOwnedMachineExists(memoryByOwnedMachine.Id))
|
|
|
|
|
|
return NotFound();
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
ViewData["OwnedMachineId"] = new SelectList(_context.OwnedMachines.OrderBy(m => m.Machine.Company.Name).
|
|
|
|
|
|
ThenBy(m => m.Machine.Name).
|
|
|
|
|
|
ThenBy(m => m.User.UserName).Select(m => new
|
|
|
|
|
|
{
|
|
|
|
|
|
m.Id,
|
|
|
|
|
|
Name =
|
|
|
|
|
|
$"{m.Machine.Company.Name} {m.Machine.Name} <{m.User.UserName}>"
|
|
|
|
|
|
}), "Id", "Name", memoryByOwnedMachine.OwnedMachineId);
|
|
|
|
|
|
|
2019-05-30 21:26:44 +01:00
|
|
|
|
return View(memoryByOwnedMachine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET: Admin/MemoryByOwnedMachines/Delete/5
|
|
|
|
|
|
public async Task<IActionResult> Delete(long? id)
|
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
|
if(id == null)
|
|
|
|
|
|
return NotFound();
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
|
MemoryByOwnedMachine memoryByOwnedMachine =
|
|
|
|
|
|
await _context.MemoryByOwnedMachine.Include(m => m.OwnedMachine).FirstOrDefaultAsync(m => m.Id == id);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
|
|
|
|
|
if(memoryByOwnedMachine == null)
|
|
|
|
|
|
return NotFound();
|
2019-05-30 21:26:44 +01:00
|
|
|
|
|
|
|
|
|
|
return View(memoryByOwnedMachine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST: Admin/MemoryByOwnedMachines/Delete/5
|
2020-02-10 22:44:18 +00:00
|
|
|
|
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
2019-05-30 21:26:44 +01:00
|
|
|
|
public async Task<IActionResult> DeleteConfirmed(long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
MemoryByOwnedMachine memoryByOwnedMachine = await _context.MemoryByOwnedMachine.FindAsync(id);
|
|
|
|
|
|
_context.MemoryByOwnedMachine.Remove(memoryByOwnedMachine);
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
2019-05-30 21:26:44 +01:00
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
|
bool MemoryByOwnedMachineExists(long id) => _context.MemoryByOwnedMachine.Any(e => e.Id == id);
|
2019-05-30 21:26:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|