From ea68eeb589c0d901fc629979b5d64729148437a0 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 30 May 2019 01:35:22 +0100 Subject: [PATCH] Implement GPUs by owned machine admin pages. --- .../GpusByOwnedMachineController.cs | 183 ++++++++++++++++++ .../Views/GpusByOwnedMachine/Create.cshtml | 46 +++++ .../Views/GpusByOwnedMachine/Delete.cshtml | 39 ++++ .../Views/GpusByOwnedMachine/Details.cshtml | 37 ++++ .../Views/GpusByOwnedMachine/Edit.cshtml | 54 ++++++ .../Views/GpusByOwnedMachine/Index.cshtml | 58 ++++++ 6 files changed, 417 insertions(+) create mode 100644 cicm_web/Areas/Admin/Controllers/GpusByOwnedMachineController.cs create mode 100644 cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Create.cshtml create mode 100644 cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Delete.cshtml create mode 100644 cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Details.cshtml create mode 100644 cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Edit.cshtml create mode 100644 cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Index.cshtml diff --git a/cicm_web/Areas/Admin/Controllers/GpusByOwnedMachineController.cs b/cicm_web/Areas/Admin/Controllers/GpusByOwnedMachineController.cs new file mode 100644 index 00000000..d9cdd79b --- /dev/null +++ b/cicm_web/Areas/Admin/Controllers/GpusByOwnedMachineController.cs @@ -0,0 +1,183 @@ +using System.Linq; +using System.Threading.Tasks; +using Cicm.Database.Models; +using cicm_web.Areas.Admin.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query; + +namespace cicm_web.Areas.Admin.Controllers +{ + [Area("Admin")] + [Authorize] + public class GpusByOwnedMachineController : Controller + { + readonly cicmContext _context; + + public GpusByOwnedMachineController(cicmContext context) + { + _context = context; + } + + // GET: GpusByOwnedMachine + public async Task Index() + { + IIncludableQueryable cicmContext = + _context.GpusByOwnedMachine.Include(g => g.Gpu).Include(g => g.OwnedMachine); + return View(await cicmContext.OrderBy(g => g.OwnedMachine.Machine.Name).ThenBy(g => g.Gpu.Name) + .Select(g => new GpusByMachineViewModel + { + Id = g.Id, + Gpu = g.Gpu.Name, + Machine = + $"{g.OwnedMachine.Machine.Company.Name} {g.OwnedMachine.Machine.Name} <{g.OwnedMachine.User.UserName}>" + }).ToListAsync()); + } + + // GET: GpusByOwnedMachine/Details/5 + public async Task Details(long? id) + { + if(id == null) return NotFound(); + + GpusByMachineViewModel gpusByOwnedMachine = await _context.GpusByOwnedMachine + .Include(g => g.Gpu).Include(g => g.OwnedMachine) + .Select(o => new GpusByMachineViewModel + { + Gpu = o.Gpu.Name, + Id = o.Id, + Machine = + $"{o.OwnedMachine.Machine.Company.Name} {o.OwnedMachine.Machine.Name} <{o.OwnedMachine.User.UserName}>" + }).FirstOrDefaultAsync(m => m.Id == + id); + if(gpusByOwnedMachine == null) return NotFound(); + + return View(gpusByOwnedMachine); + } + + // GET: GpusByOwnedMachine/Create + public IActionResult Create() + { + ViewData["GpuId"] = new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name"); + 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"); + return View(); + } + + // POST: GpusByOwnedMachine/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 Create([Bind("GpuId,OwnedMachineId,Id")] GpusByOwnedMachine gpusByOwnedMachine) + { + if(ModelState.IsValid) + { + _context.Add(gpusByOwnedMachine); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + + ViewData["GpuId"] = + new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByOwnedMachine.GpuId); + 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", gpusByOwnedMachine.OwnedMachineId); + return View(gpusByOwnedMachine); + } + + // GET: GpusByOwnedMachine/Edit/5 + public async Task Edit(long? id) + { + if(id == null) return NotFound(); + + GpusByOwnedMachine gpusByOwnedMachine = await _context.GpusByOwnedMachine.FindAsync(id); + if(gpusByOwnedMachine == null) return NotFound(); + + ViewData["GpuId"] = + new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByOwnedMachine.GpuId); + 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", gpusByOwnedMachine.OwnedMachineId); + return View(gpusByOwnedMachine); + } + + // POST: GpusByOwnedMachine/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 Edit( + long id, [Bind("GpuId,OwnedMachineId,Id")] GpusByOwnedMachine gpusByOwnedMachine) + { + if(id != gpusByOwnedMachine.Id) return NotFound(); + + if(ModelState.IsValid) + { + try + { + _context.Update(gpusByOwnedMachine); + await _context.SaveChangesAsync(); + } + catch(DbUpdateConcurrencyException) + { + if(!GpusByOwnedMachineExists(gpusByOwnedMachine.Id)) return NotFound(); + + throw; + } + + return RedirectToAction(nameof(Index)); + } + + ViewData["GpuId"] = + new SelectList(_context.Gpus.OrderBy(g => g.Name), "Id", "Name", gpusByOwnedMachine.GpuId); + 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", gpusByOwnedMachine.OwnedMachineId); + return View(gpusByOwnedMachine); + } + + // GET: GpusByOwnedMachine/Delete/5 + public async Task Delete(long? id) + { + if(id == null) return NotFound(); + + GpusByMachineViewModel gpusByOwnedMachine = await _context.GpusByOwnedMachine + .Include(g => g.Gpu).Include(g => g.OwnedMachine) + .Select(o => new GpusByMachineViewModel + { + Gpu = o.Gpu.Name, + Id = o.Id, + Machine = + $"{o.OwnedMachine.Machine.Company.Name} {o.OwnedMachine.Machine.Name} <{o.OwnedMachine.User.UserName}>" + }).FirstOrDefaultAsync(m => m.Id == + id); + if(gpusByOwnedMachine == null) return NotFound(); + + return View(gpusByOwnedMachine); + } + + // POST: GpusByOwnedMachine/Delete/5 + [HttpPost] + [ActionName("Delete")] + [ValidateAntiForgeryToken] + public async Task DeleteConfirmed(long id) + { + GpusByOwnedMachine gpusByOwnedMachine = await _context.GpusByOwnedMachine.FindAsync(id); + _context.GpusByOwnedMachine.Remove(gpusByOwnedMachine); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + + bool GpusByOwnedMachineExists(long id) + { + return _context.GpusByOwnedMachine.Any(e => e.Id == id); + } + } +} \ No newline at end of file diff --git a/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Create.cshtml b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Create.cshtml new file mode 100644 index 00000000..ab7bf457 --- /dev/null +++ b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Create.cshtml @@ -0,0 +1,46 @@ +@model Cicm.Database.Models.GpusByOwnedMachine + +@{ + ViewData["Title"] = "Create"; +} + +

Create

+ +

GPU by owned machine

+
+
+
+
+
+
+
+ + +
+
+ + +
+ +
+
+
\ No newline at end of file diff --git a/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Delete.cshtml b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Delete.cshtml new file mode 100644 index 00000000..e5846f46 --- /dev/null +++ b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Delete.cshtml @@ -0,0 +1,39 @@ +@model cicm_web.Areas.Admin.Models.GpusByMachineViewModel + +@{ + ViewData["Title"] = "Delete"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

GPU by owned machine

+
+
+
+ @Html.DisplayNameFor(model => model.Machine) +
+
+ @Html.DisplayFor(model => model.Machine) +
+
+ @Html.DisplayNameFor(model => model.Gpu) +
+
+ @Html.DisplayFor(model => model.Gpu) +
+
+ +
+ + + + Back to List + +
+
\ No newline at end of file diff --git a/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Details.cshtml b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Details.cshtml new file mode 100644 index 00000000..a9d36379 --- /dev/null +++ b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Details.cshtml @@ -0,0 +1,37 @@ +@model cicm_web.Areas.Admin.Models.GpusByMachineViewModel + +@{ + ViewData["Title"] = "Details"; +} + +

Details

+ +
+

GPU by owned machine

+
+
+
+ @Html.DisplayNameFor(model => model.Machine) +
+
+ @Html.DisplayFor(model => model.Machine) +
+
+ @Html.DisplayNameFor(model => model.Gpu) +
+
+ @Html.DisplayFor(model => model.Gpu) +
+
+
+ \ No newline at end of file diff --git a/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Edit.cshtml b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Edit.cshtml new file mode 100644 index 00000000..9e298b93 --- /dev/null +++ b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Edit.cshtml @@ -0,0 +1,54 @@ +@model Cicm.Database.Models.GpusByOwnedMachine + +@{ + ViewData["Title"] = "Edit"; +} + +

Edit

+ +

Gpus by owned machine

+
+
+
+
+
+
+
+ + + + +
+
+ + + + +
+ + +
+
+
\ No newline at end of file diff --git a/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Index.cshtml b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Index.cshtml new file mode 100644 index 00000000..47177744 --- /dev/null +++ b/cicm_web/Areas/Admin/Views/GpusByOwnedMachine/Index.cshtml @@ -0,0 +1,58 @@ +@using cicm_web.Areas.Admin.Models +@model IEnumerable + +@{ + ViewData["Title"] = "Index"; +} + +

GPUs by machine

+ +

+ + Create New + +

+ + + + + + + + + + @foreach(GpusByMachineViewModel item in Model) + { + + + + + + } + +
+ @Html.DisplayNameFor(model => model.Machine) + + @Html.DisplayNameFor(model => model.Gpu) +
+ @Html.DisplayFor(modelItem => item.Machine) + + @Html.DisplayFor(modelItem => item.Gpu) + + + Details + + + Edit + + + Delete + +
\ No newline at end of file