2019-05-19 02:24:01 +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-19 02:24:01 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2019-05-19 22:08:15 +01:00
|
|
|
using Microsoft.EntityFrameworkCore.Query;
|
2019-05-19 02:24:01 +01:00
|
|
|
|
2020-02-10 02:20:48 +00:00
|
|
|
namespace Marechai.Areas.Admin.Controllers
|
2019-05-19 02:24:01 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
[Area("Admin"), Authorize]
|
2019-05-19 02:24:01 +01:00
|
|
|
public class GpusByMachineController : Controller
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
readonly MarechaiContext _context;
|
2019-05-19 02:24:01 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
public GpusByMachineController(MarechaiContext context) => _context = context;
|
2019-05-19 02:24:01 +01:00
|
|
|
|
|
|
|
|
// GET: GpusByMachine
|
|
|
|
|
public async Task<IActionResult> Index()
|
|
|
|
|
{
|
2020-02-10 02:47:39 +00:00
|
|
|
IIncludableQueryable<GpusByMachine, Machine> marechaiContext =
|
2019-05-19 22:08:15 +01:00
|
|
|
_context.GpusByMachine.Include(g => g.Gpu).Include(g => g.Machine);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
|
|
|
return View(await marechaiContext.OrderBy(g => g.Machine.Name).ThenBy(g => g.Gpu.Name).
|
|
|
|
|
Select(g => new GpusByMachineViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = g.Id, Gpu = g.Gpu.Name, Machine = g.Machine.Name
|
|
|
|
|
}).ToListAsync());
|
2019-05-19 02:24:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: GpusByMachine/Details/5
|
|
|
|
|
public async Task<IActionResult> Details(long? id)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(id == null)
|
|
|
|
|
return NotFound();
|
2019-05-19 02:24:01 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
GpusByMachine gpusByMachine = await _context.
|
|
|
|
|
GpusByMachine.Include(g => g.Gpu).Include(g => g.Machine).
|
|
|
|
|
FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(gpusByMachine == null)
|
|
|
|
|
return NotFound();
|
2019-05-19 02:24:01 +01:00
|
|
|
|
|
|
|
|
return View(gpusByMachine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: GpusByMachine/Create
|
|
|
|
|
public IActionResult Create()
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name");
|
2019-05-19 22:08:15 +01:00
|
|
|
ViewData["MachineId"] = new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name");
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 02:24:01 +01:00
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: GpusByMachine/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.
|
2020-02-10 22:44:18 +00:00
|
|
|
[HttpPost, ValidateAntiForgeryToken]
|
2019-05-19 02:24:01 +01:00
|
|
|
public async Task<IActionResult> Create([Bind("GpuId,MachineId,Id")] GpusByMachine gpusByMachine)
|
|
|
|
|
{
|
2019-05-19 22:08:15 +01:00
|
|
|
if(ModelState.IsValid)
|
2019-05-19 02:24:01 +01:00
|
|
|
{
|
|
|
|
|
_context.Add(gpusByMachine);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 02:24:01 +01:00
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-05-19 22:08:15 +01:00
|
|
|
|
|
|
|
|
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByMachine.GpuId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 22:08:15 +01:00
|
|
|
ViewData["MachineId"] =
|
|
|
|
|
new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name", gpusByMachine.MachineId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 02:24:01 +01:00
|
|
|
return View(gpusByMachine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: GpusByMachine/Edit/5
|
|
|
|
|
public async Task<IActionResult> Edit(long? id)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(id == null)
|
|
|
|
|
return NotFound();
|
2019-05-19 02:24:01 +01:00
|
|
|
|
2019-05-19 22:08:15 +01:00
|
|
|
GpusByMachine gpusByMachine = await _context.GpusByMachine.FindAsync(id);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
|
|
|
|
if(gpusByMachine == null)
|
|
|
|
|
return NotFound();
|
2019-05-19 22:08:15 +01:00
|
|
|
|
|
|
|
|
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByMachine.GpuId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 22:08:15 +01:00
|
|
|
ViewData["MachineId"] =
|
|
|
|
|
new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name", gpusByMachine.MachineId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 02:24:01 +01:00
|
|
|
return View(gpusByMachine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: GpusByMachine/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.
|
2020-02-10 22:44:18 +00:00
|
|
|
[HttpPost, ValidateAntiForgeryToken]
|
2019-05-19 02:24:01 +01:00
|
|
|
public async Task<IActionResult> Edit(long id, [Bind("GpuId,MachineId,Id")] GpusByMachine gpusByMachine)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(id != gpusByMachine.Id)
|
|
|
|
|
return NotFound();
|
2019-05-19 02:24:01 +01:00
|
|
|
|
2019-05-19 22:08:15 +01:00
|
|
|
if(ModelState.IsValid)
|
2019-05-19 02:24:01 +01:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_context.Update(gpusByMachine);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
2019-05-19 22:08:15 +01:00
|
|
|
catch(DbUpdateConcurrencyException)
|
2019-05-19 02:24:01 +01:00
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(!GpusByMachineExists(gpusByMachine.Id))
|
|
|
|
|
return NotFound();
|
2019-05-19 22:08:15 +01:00
|
|
|
|
|
|
|
|
throw;
|
2019-05-19 02:24:01 +01:00
|
|
|
}
|
2019-05-19 22:08:15 +01:00
|
|
|
|
2019-05-19 02:24:01 +01:00
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2019-05-19 22:08:15 +01:00
|
|
|
|
|
|
|
|
ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByMachine.GpuId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 22:08:15 +01:00
|
|
|
ViewData["MachineId"] =
|
|
|
|
|
new SelectList(_context.Machines.OrderBy(m => m.Name), "Id", "Name", gpusByMachine.MachineId);
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 02:24:01 +01:00
|
|
|
return View(gpusByMachine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: GpusByMachine/Delete/5
|
|
|
|
|
public async Task<IActionResult> Delete(long? id)
|
|
|
|
|
{
|
2020-02-10 22:44:18 +00:00
|
|
|
if(id == null)
|
|
|
|
|
return NotFound();
|
2019-05-19 02:24:01 +01:00
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
GpusByMachine gpusByMachine = await _context.
|
|
|
|
|
GpusByMachine.Include(g => g.Gpu).Include(g => g.Machine).
|
|
|
|
|
FirstOrDefaultAsync(m => m.Id == id);
|
|
|
|
|
|
|
|
|
|
if(gpusByMachine == null)
|
|
|
|
|
return NotFound();
|
2019-05-19 02:24:01 +01:00
|
|
|
|
|
|
|
|
return View(gpusByMachine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: GpusByMachine/Delete/5
|
2020-02-10 22:44:18 +00:00
|
|
|
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
2019-05-19 02:24:01 +01:00
|
|
|
public async Task<IActionResult> DeleteConfirmed(long id)
|
|
|
|
|
{
|
2019-05-19 22:08:15 +01:00
|
|
|
GpusByMachine gpusByMachine = await _context.GpusByMachine.FindAsync(id);
|
2019-05-19 02:24:01 +01:00
|
|
|
_context.GpusByMachine.Remove(gpusByMachine);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2020-02-10 22:44:18 +00:00
|
|
|
|
2019-05-19 02:24:01 +01:00
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 22:44:18 +00:00
|
|
|
bool GpusByMachineExists(long id) => _context.GpusByMachine.Any(e => e.Id == id);
|
2019-05-19 02:24:01 +01:00
|
|
|
}
|
2019-05-19 22:08:15 +01:00
|
|
|
}
|