Optimize view of sound synthetizers by machine admin page.

This commit is contained in:
2019-05-20 00:38:13 +01:00
parent a51f93ea20
commit b7df0998aa
4 changed files with 126 additions and 80 deletions

View File

@@ -1,10 +1,12 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cicm.Database.Models; using Cicm.Database.Models;
using cicm_web.Areas.Admin.Models;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace cicm_web.Areas.Admin.Controllers namespace cicm_web.Areas.Admin.Controllers
{ {
@@ -12,7 +14,7 @@ namespace cicm_web.Areas.Admin.Controllers
[Authorize] [Authorize]
public class SoundByMachineController : Controller public class SoundByMachineController : Controller
{ {
private readonly cicmContext _context; readonly cicmContext _context;
public SoundByMachineController(cicmContext context) public SoundByMachineController(cicmContext context)
{ {
@@ -22,26 +24,26 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: SoundByMachine // GET: SoundByMachine
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
var cicmContext = _context.SoundByMachine.Include(s => s.Machine).Include(s => s.SoundSynth); IIncludableQueryable<SoundByMachine, SoundSynth> cicmContext =
return View(await cicmContext.OrderBy(s => s.Machine.Name).ThenBy(s => s.SoundSynth.Name).ToListAsync()); _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 // GET: SoundByMachine/Details/5
public async Task<IActionResult> Details(long? id) public async Task<IActionResult> Details(long? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound();
}
var soundByMachine = await _context.SoundByMachine SoundByMachine soundByMachine = await _context.SoundByMachine
.Include(s => s.Machine) .Include(s => s.Machine).Include(s => s.SoundSynth)
.Include(s => s.SoundSynth)
.FirstOrDefaultAsync(m => m.Id == id); .FirstOrDefaultAsync(m => m.Id == id);
if (soundByMachine == null) if(soundByMachine == null) return NotFound();
{
return NotFound();
}
return View(soundByMachine); return View(soundByMachine);
} }
@@ -61,12 +63,13 @@ namespace cicm_web.Areas.Admin.Controllers
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine) public async Task<IActionResult> Create([Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine)
{ {
if (ModelState.IsValid) if(ModelState.IsValid)
{ {
_context.Add(soundByMachine); _context.Add(soundByMachine);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); 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); ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
return View(soundByMachine); return View(soundByMachine);
@@ -75,16 +78,11 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: SoundByMachine/Edit/5 // GET: SoundByMachine/Edit/5
public async Task<IActionResult> Edit(long? id) public async Task<IActionResult> Edit(long? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound(); SoundByMachine soundByMachine = await _context.SoundByMachine.FindAsync(id);
} if(soundByMachine == 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); ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId); ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
return View(soundByMachine); return View(soundByMachine);
@@ -95,33 +93,28 @@ namespace cicm_web.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(long id, [Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine) public async Task<IActionResult> Edit(
long id, [Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine)
{ {
if (id != soundByMachine.Id) if(id != soundByMachine.Id) return NotFound();
{
return NotFound();
}
if (ModelState.IsValid) if(ModelState.IsValid)
{ {
try try
{ {
_context.Update(soundByMachine); _context.Update(soundByMachine);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
catch (DbUpdateConcurrencyException) catch(DbUpdateConcurrencyException)
{
if (!SoundByMachineExists(soundByMachine.Id))
{
return NotFound();
}
else
{ {
if(!SoundByMachineExists(soundByMachine.Id)) return NotFound();
throw; throw;
} }
}
return RedirectToAction(nameof(Index)); 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); ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
return View(soundByMachine); return View(soundByMachine);
@@ -130,35 +123,29 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: SoundByMachine/Delete/5 // GET: SoundByMachine/Delete/5
public async Task<IActionResult> Delete(long? id) public async Task<IActionResult> Delete(long? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound();
}
var soundByMachine = await _context.SoundByMachine SoundByMachine soundByMachine = await _context.SoundByMachine
.Include(s => s.Machine) .Include(s => s.Machine).Include(s => s.SoundSynth)
.Include(s => s.SoundSynth)
.FirstOrDefaultAsync(m => m.Id == id); .FirstOrDefaultAsync(m => m.Id == id);
if (soundByMachine == null) if(soundByMachine == null) return NotFound();
{
return NotFound();
}
return View(soundByMachine); return View(soundByMachine);
} }
// POST: SoundByMachine/Delete/5 // POST: SoundByMachine/Delete/5
[HttpPost, ActionName("Delete")] [HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(long id) public async Task<IActionResult> DeleteConfirmed(long id)
{ {
var soundByMachine = await _context.SoundByMachine.FindAsync(id); SoundByMachine soundByMachine = await _context.SoundByMachine.FindAsync(id);
_context.SoundByMachine.Remove(soundByMachine); _context.SoundByMachine.Remove(soundByMachine);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
private bool SoundByMachineExists(long id) bool SoundByMachineExists(long id)
{ {
return _context.SoundByMachine.Any(e => e.Id == id); return _context.SoundByMachine.Any(e => e.Id == id);
} }

View File

@@ -0,0 +1,42 @@
/******************************************************************************
// Canary Islands Computer Museum Website
// ----------------------------------------------------------------------------
//
// Filename : SoundByMachine.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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; }
}
}

View File

@@ -1,4 +1,5 @@
@model IEnumerable<Cicm.Database.Models.SoundByMachine> @using cicm_web.Areas.Admin.Models
@model IEnumerable<cicm_web.Areas.Admin.Models.SoundByMachineViewModel>
@{ @{
ViewData["Title"] = "Index"; ViewData["Title"] = "Index";
@@ -7,7 +8,10 @@
<h1>Sound synthetizers by machine</h1> <h1>Sound synthetizers by machine</h1>
<p> <p>
<a asp-action="Create" class="btn btn-primary">Create New</a> <a asp-action="Create"
class="btn btn-primary">
Create New
</a>
</p> </p>
<table class="table"> <table class="table">
<thead> <thead>
@@ -22,20 +26,33 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach (var item in Model) { @foreach(SoundByMachineViewModel item in Model)
{
<tr> <tr>
<td> <td>
@Html.DisplayFor(modelItem => item.Machine.Name) @Html.DisplayFor(modelItem => item.Machine)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.SoundSynth.Name) @Html.DisplayFor(modelItem => item.SoundSynth)
</td> </td>
<td> <td>
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary">Details</a> <a asp-action="Details"
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-secondary">Edit</a> asp-route-id="@item.Id"
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">Delete</a> class="btn btn-primary">
Details
</a>
<a asp-action="Edit"
asp-route-id="@item.Id"
class="btn btn-secondary">
Edit
</a>
<a asp-action="Delete"
asp-route-id="@item.Id"
class="btn btn-danger">
Delete
</a>
</td> </td>
</tr> </tr>
} }
</tbody> </tbody>
</table> </table>

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
<Version>3.0.99.543</Version> <Version>3.0.99.544</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>