From b7df0998aa00e0aad0b4578c490f1797eae881c1 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 20 May 2019 00:38:13 +0100 Subject: [PATCH] Optimize view of sound synthetizers by machine admin page. --- .../Controllers/SoundByMachineController.cs | 107 ++++++++---------- .../Admin/Models/SoundByMachineViewModel.cs | 42 +++++++ .../Admin/Views/SoundByMachine/Index.cshtml | 55 +++++---- cicm_web/cicm_web.csproj | 2 +- 4 files changed, 126 insertions(+), 80 deletions(-) create mode 100644 cicm_web/Areas/Admin/Models/SoundByMachineViewModel.cs diff --git a/cicm_web/Areas/Admin/Controllers/SoundByMachineController.cs b/cicm_web/Areas/Admin/Controllers/SoundByMachineController.cs index 8dacd8c8..7ad19e74 100644 --- a/cicm_web/Areas/Admin/Controllers/SoundByMachineController.cs +++ b/cicm_web/Areas/Admin/Controllers/SoundByMachineController.cs @@ -1,10 +1,12 @@ 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 { @@ -12,7 +14,7 @@ namespace cicm_web.Areas.Admin.Controllers [Authorize] public class SoundByMachineController : Controller { - private readonly cicmContext _context; + readonly cicmContext _context; public SoundByMachineController(cicmContext context) { @@ -22,26 +24,26 @@ namespace cicm_web.Areas.Admin.Controllers // GET: SoundByMachine public async Task Index() { - var cicmContext = _context.SoundByMachine.Include(s => s.Machine).Include(s => s.SoundSynth); - return View(await cicmContext.OrderBy(s => s.Machine.Name).ThenBy(s => s.SoundSynth.Name).ToListAsync()); + IIncludableQueryable cicmContext = + _context.SoundByMachine.Include(s => s.Machine).Include(s => s.SoundSynth); + return View(await cicmContext.OrderBy(s => s.Machine.Name).ThenBy(s => s.SoundSynth.Name) + .Select(s => new SoundByMachineViewModel + { + Id = s.Id, + Machine = s.Machine.Name, + SoundSynth = s.SoundSynth.Name + }).ToListAsync()); } // GET: SoundByMachine/Details/5 public async Task Details(long? id) { - if (id == null) - { - return NotFound(); - } + if(id == null) return NotFound(); - var soundByMachine = await _context.SoundByMachine - .Include(s => s.Machine) - .Include(s => s.SoundSynth) - .FirstOrDefaultAsync(m => m.Id == id); - if (soundByMachine == null) - { - return NotFound(); - } + SoundByMachine soundByMachine = await _context.SoundByMachine + .Include(s => s.Machine).Include(s => s.SoundSynth) + .FirstOrDefaultAsync(m => m.Id == id); + if(soundByMachine == null) return NotFound(); return View(soundByMachine); } @@ -49,7 +51,7 @@ namespace cicm_web.Areas.Admin.Controllers // GET: SoundByMachine/Create public IActionResult Create() { - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name"); + ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name"); ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name"); return View(); } @@ -61,13 +63,14 @@ namespace cicm_web.Areas.Admin.Controllers [ValidateAntiForgeryToken] public async Task Create([Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine) { - if (ModelState.IsValid) + if(ModelState.IsValid) { _context.Add(soundByMachine); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId); + + ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId); ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId); return View(soundByMachine); } @@ -75,17 +78,12 @@ namespace cicm_web.Areas.Admin.Controllers // GET: SoundByMachine/Edit/5 public async Task Edit(long? id) { - if (id == null) - { - return NotFound(); - } + if(id == null) return NotFound(); - var soundByMachine = await _context.SoundByMachine.FindAsync(id); - if (soundByMachine == null) - { - return NotFound(); - } - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId); + SoundByMachine soundByMachine = await _context.SoundByMachine.FindAsync(id); + if(soundByMachine == null) return NotFound(); + + ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId); ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId); return View(soundByMachine); } @@ -95,34 +93,29 @@ namespace cicm_web.Areas.Admin.Controllers // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public async Task Edit(long id, [Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine) + public async Task Edit( + long id, [Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine) { - if (id != soundByMachine.Id) - { - return NotFound(); - } + if(id != soundByMachine.Id) return NotFound(); - if (ModelState.IsValid) + if(ModelState.IsValid) { try { _context.Update(soundByMachine); await _context.SaveChangesAsync(); } - catch (DbUpdateConcurrencyException) + catch(DbUpdateConcurrencyException) { - if (!SoundByMachineExists(soundByMachine.Id)) - { - return NotFound(); - } - else - { - throw; - } + if(!SoundByMachineExists(soundByMachine.Id)) return NotFound(); + + throw; } + return RedirectToAction(nameof(Index)); } - ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId); + + ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId); ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId); return View(soundByMachine); } @@ -130,37 +123,31 @@ namespace cicm_web.Areas.Admin.Controllers // GET: SoundByMachine/Delete/5 public async Task Delete(long? id) { - if (id == null) - { - return NotFound(); - } + if(id == null) return NotFound(); - var soundByMachine = await _context.SoundByMachine - .Include(s => s.Machine) - .Include(s => s.SoundSynth) - .FirstOrDefaultAsync(m => m.Id == id); - if (soundByMachine == null) - { - return NotFound(); - } + SoundByMachine soundByMachine = await _context.SoundByMachine + .Include(s => s.Machine).Include(s => s.SoundSynth) + .FirstOrDefaultAsync(m => m.Id == id); + if(soundByMachine == null) return NotFound(); return View(soundByMachine); } // POST: SoundByMachine/Delete/5 - [HttpPost, ActionName("Delete")] + [HttpPost] + [ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(long id) { - var soundByMachine = await _context.SoundByMachine.FindAsync(id); + SoundByMachine soundByMachine = await _context.SoundByMachine.FindAsync(id); _context.SoundByMachine.Remove(soundByMachine); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } - private bool SoundByMachineExists(long id) + bool SoundByMachineExists(long id) { return _context.SoundByMachine.Any(e => e.Id == id); } } -} +} \ No newline at end of file diff --git a/cicm_web/Areas/Admin/Models/SoundByMachineViewModel.cs b/cicm_web/Areas/Admin/Models/SoundByMachineViewModel.cs new file mode 100644 index 00000000..03993ce5 --- /dev/null +++ b/cicm_web/Areas/Admin/Models/SoundByMachineViewModel.cs @@ -0,0 +1,42 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : SoundByMachine.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Junction of sound synth and machines. +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System.ComponentModel; + +namespace cicm_web.Areas.Admin.Models +{ + public class SoundByMachineViewModel + { + public long Id; + public string Machine { get; set; } + [DisplayName("Sound synthetizer")] + public string SoundSynth { get; set; } + } +} \ No newline at end of file diff --git a/cicm_web/Areas/Admin/Views/SoundByMachine/Index.cshtml b/cicm_web/Areas/Admin/Views/SoundByMachine/Index.cshtml index b17f110c..67785590 100644 --- a/cicm_web/Areas/Admin/Views/SoundByMachine/Index.cshtml +++ b/cicm_web/Areas/Admin/Views/SoundByMachine/Index.cshtml @@ -1,4 +1,5 @@ -@model IEnumerable +@using cicm_web.Areas.Admin.Models +@model IEnumerable @{ ViewData["Title"] = "Index"; @@ -7,35 +8,51 @@

Sound synthetizers by machine

- Create New + + Create New +

- - - - - + + + + + -@foreach (var item in Model) { + @foreach(SoundByMachineViewModel item in Model) + { -} + } -
- @Html.DisplayNameFor(model => model.Machine) - - @Html.DisplayNameFor(model => model.SoundSynth) -
+ @Html.DisplayNameFor(model => model.Machine) + + @Html.DisplayNameFor(model => model.SoundSynth) +
- @Html.DisplayFor(modelItem => item.Machine.Name) + @Html.DisplayFor(modelItem => item.Machine) - @Html.DisplayFor(modelItem => item.SoundSynth.Name) + @Html.DisplayFor(modelItem => item.SoundSynth) - Details - Edit - Delete + + Details + + + Edit + + + Delete +
+ \ No newline at end of file diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj index da2f6c1f..c3803bb7 100644 --- a/cicm_web/cicm_web.csproj +++ b/cicm_web/cicm_web.csproj @@ -2,7 +2,7 @@ netcoreapp2.2 - 3.0.99.543 + 3.0.99.544 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website