Allow to add and remove sound synthesizers from machine details in admin view.

This commit is contained in:
2020-05-29 02:35:09 +01:00
parent 7cf5058f9b
commit fdc371bee3
12 changed files with 236 additions and 350 deletions

View File

@@ -1,158 +0,0 @@
using System.Linq;
using System.Threading.Tasks;
using Marechai.Areas.Admin.Models;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace Marechai.Areas.Admin.Controllers
{
[Area("Admin"), Authorize]
public class SoundByMachineController : Controller
{
readonly MarechaiContext _context;
public SoundByMachineController(MarechaiContext context) => _context = context;
// GET: SoundByMachine
public async Task<IActionResult> Index()
{
IIncludableQueryable<SoundByMachine, SoundSynth> marechaiContext =
_context.SoundByMachine.Include(s => s.Machine).Include(s => s.SoundSynth);
return View(await marechaiContext.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<IActionResult> Details(long? id)
{
if(id == 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);
}
// GET: SoundByMachine/Create
public IActionResult Create()
{
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name");
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name");
return View();
}
// POST: SoundByMachine/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<IActionResult> Create([Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine)
{
if(ModelState.IsValid)
{
_context.Add(soundByMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
return View(soundByMachine);
}
// GET: SoundByMachine/Edit/5
public async Task<IActionResult> Edit(long? id)
{
if(id == null)
return NotFound();
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);
}
// POST: SoundByMachine/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<IActionResult> Edit(
long id, [Bind("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine)
{
if(id != soundByMachine.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(soundByMachine);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!SoundByMachineExists(soundByMachine.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
return View(soundByMachine);
}
// GET: SoundByMachine/Delete/5
public async Task<IActionResult> Delete(long? id)
{
if(id == 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"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(long id)
{
SoundByMachine soundByMachine = await _context.SoundByMachine.FindAsync(id);
_context.SoundByMachine.Remove(soundByMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool SoundByMachineExists(long id) => _context.SoundByMachine.Any(e => e.Id == id);
}
}

View File

@@ -1,27 +0,0 @@
@model Marechai.Database.Models.SoundByMachine
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Sound by machine</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="SoundSynth" class="control-label"></label>
<select asp-for="SoundSynthId" class="form-control" asp-items="ViewBag.SoundSynthId"></select>
</div>
<div class="form-group">
<label asp-for="Machine" class="control-label"></label>
<select asp-for="MachineId" class="form-control" asp-items="ViewBag.MachineId"></select>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Create" />
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</div>
</form>
</div>
</div>

View File

@@ -1,30 +0,0 @@
@model Marechai.Database.Models.SoundByMachine
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Sound by machine</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Machine)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Machine.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.SoundSynth)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.SoundSynth.Name)
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input class="btn btn-danger" type="submit" value="Delete" />
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</form>
</div>

View File

@@ -1,28 +0,0 @@
@model Marechai.Database.Models.SoundByMachine
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>Sound by machine</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Machine)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Machine.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.SoundSynth)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.SoundSynth.Name)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id" class="btn btn-primary">Edit</a>
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</div>

View File

@@ -1,30 +0,0 @@
@model Marechai.Database.Models.SoundByMachine
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Sound by machine</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="SoundSynth" class="control-label"></label>
<select asp-for="SoundSynthId" class="form-control" asp-items="ViewBag.SoundSynthId"></select>
<span asp-validation-for="SoundSynthId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Machine" class="control-label"></label>
<select asp-for="MachineId" class="form-control" asp-items="ViewBag.MachineId"></select>
<span asp-validation-for="MachineId" class="text-danger"></span>
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Save" />
<a asp-action="Index" class="btn btn-secondary">Back to List</a>
</div>
</form>
</div>
</div>

View File

@@ -1,48 +0,0 @@
@model IEnumerable<Marechai.Areas.Admin.Models.SoundByMachineViewModel>
@{
ViewData["Title"] = "Index";
}
<h1>Sound synthesizers by machine</h1>
<p>
<a asp-action="Create" class="btn btn-primary">
Create new
</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Machine)
</th>
<th>
@Html.DisplayNameFor(model => model.SoundSynth)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Machine)
</td>
<td>
@Html.DisplayFor(modelItem => item.SoundSynth)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.Id" 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>
</tr>
}
</tbody>
</table>

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1373</Version>
<Version>3.0.99.1374</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>
@@ -149,5 +149,10 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\GpusByMachine\Index.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundByMachine\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundByMachine\Delete.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundByMachine\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundByMachine\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundByMachine\Index.cshtml" />
</ItemGroup>
</Project>

View File

@@ -41,6 +41,8 @@
@inject NavigationManager NavigationManager
@inject GpuByMachineService GpuByMachineService
@inject GpusService GpusService
@inject SoundSynthsByMachineService SoundSynthsByMachineService
@inject SoundSynthsService SoundSynthsService
@attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Machine details"]</h3>
<hr />
@@ -204,7 +206,61 @@
@item.Name
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowGpuDeleteModal(item.Id);}" Disabled="@_addingGpu">@L["Delete"]</Button>
<Button Color="Color.Danger" Clicked="() => {ShowSoundDeleteModal(item.Id);}" Disabled="@_addingGpu">@L["Delete"]</Button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
<hr />
<h3>@L["Sound synthesizers belonging to this machine"]</h3>
<Button Color="Color.Success" Clicked="OnAddSoundClick" Disabled="_addingSound">@L["Add new (sound by machine)"]</Button>
@if (_addingSound)
{
<div>
<Field>
<FieldLabel>@L["Sound synthesizers"]</FieldLabel>
<Select Disabled="_savingSound" TValue="int?" @bind-SelectedValue="@_addingSoundId">
@foreach (var soundSynth in _soundSynths)
{
<SelectItem TValue="int?" Value="@soundSynth.Id">@soundSynth.CompanyName - @soundSynth.Name</SelectItem>
}
</Select>
<Button Color="Color.Primary" Clicked="@CancelAddSound" Disabled="@_savingSound">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@ConfirmAddSound" Disabled="@_savingSound">@L["Add"]</Button>
</Field>
</div>
}
@if (_machineSound?.Count > 0)
{
<div>
<table class="table table-striped">
<thead>
<tr>
<th>
@L["Company"]
</th>
<th>
@L["Name"]
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in _machineSound)
{
<tr>
<td>
@item.CompanyName
</td>
<td>
@item.Name
</td>
<td>
<Button Color="Color.Danger" Clicked="() => {ShowGpuDeleteModal(item.Id);}" Disabled="@_addingSound">@L["Delete"]</Button>
</td>
</tr>
}

View File

@@ -12,30 +12,34 @@ namespace Marechai.Pages.Admin.Details
{
public partial class Machine
{
bool _addingGpu;
int? _addingGpuId;
List<CompanyViewModel> _companies;
bool _creating;
GpuByMachineViewModel _currentGpuByMachine;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingGpuByMachine;
bool _editing;
List<MachineFamilyViewModel> _families;
Modal _frmDelete;
List<GpuViewModel> _gpus;
bool _loaded;
List<GpuByMachineViewModel> _machineGpus;
MachineViewModel _model;
bool _noFamily;
bool _prototype;
bool _savingGpu;
bool _unknownIntroduced;
bool _unknownModel;
bool _addingGpu;
int? _addingGpuId;
bool _addingSound;
int? _addingSoundId;
List<CompanyViewModel> _companies;
bool _creating;
GpuByMachineViewModel _currentGpuByMachine;
SoundSynthByMachineViewModel _currentSoundByMachine;
bool _deleteInProgress;
string _deleteText;
string _deleteTitle;
bool _deletingGpuByMachine;
bool _deletingSoundByMachine;
bool _editing;
List<MachineFamilyViewModel> _families;
Modal _frmDelete;
List<GpuViewModel> _gpus;
bool _loaded;
List<GpuByMachineViewModel> _machineGpus;
List<SoundSynthByMachineViewModel> _machineSound;
MachineViewModel _model;
bool _noFamily;
bool _prototype;
bool _savingGpu;
bool _savingSound;
List<SoundSynthViewModel> _soundSynths;
bool _unknownIntroduced;
bool _unknownModel;
[Parameter]
public int Id { get; set; }
@@ -64,6 +68,7 @@ namespace Marechai.Pages.Admin.Details
_model = _creating ? new MachineViewModel() : await Service.GetAsync(Id);
_machineGpus = await GpuByMachineService.GetByMachine(Id);
_gpus = await GpusService.GetAsync();
_soundSynths = await SoundSynthsService.GetAsync();
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machines/edit/",
@@ -164,6 +169,8 @@ namespace Marechai.Pages.Admin.Details
{
if(_deletingGpuByMachine)
await ConfirmDeleteGpuByMachine();
else if(_deletingSoundByMachine)
await ConfirmDeleteSoundByMachine();
}
async Task ConfirmDeleteGpuByMachine()
@@ -191,9 +198,10 @@ namespace Marechai.Pages.Admin.Details
void ModalClosing(ModalClosingEventArgs obj)
{
_deleteInProgress = false;
_deletingGpuByMachine = false;
_currentGpuByMachine = null;
_deleteInProgress = false;
_deletingGpuByMachine = false;
_currentGpuByMachine = null;
_deletingSoundByMachine = false;
}
async Task OnAddGpuClick()
@@ -237,5 +245,83 @@ namespace Marechai.Pages.Admin.Details
// Tell we finished loading
StateHasChanged();
}
void ShowSoundDeleteModal(long itemId)
{
_currentSoundByMachine = _machineSound.FirstOrDefault(n => n.Id == itemId);
_deletingSoundByMachine = true;
_deleteTitle = L["Delete sound synthesizer from this machine"];
_deleteText =
string.Format(L["Are you sure you want to delete the sound synthesizer {0} manufactured by {1} from this machine?"],
_currentSoundByMachine?.Name, _currentSoundByMachine?.CompanyName);
_frmDelete.Show();
}
async Task ConfirmDeleteSoundByMachine()
{
if(_currentSoundByMachine is null)
return;
_deleteInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await SoundSynthsByMachineService.DeleteAsync(_currentSoundByMachine.Id);
_machineSound = await SoundSynthsByMachineService.GetByMachine(Id);
_deleteInProgress = false;
_frmDelete.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
async Task OnAddSoundClick()
{
_addingSound = true;
_savingSound = false;
_addingSoundId = null;
}
void CancelAddSound()
{
_addingSound = false;
_savingSound = false;
_addingSoundId = null;
}
async Task ConfirmAddSound()
{
if(_addingSoundId is null)
{
CancelAddSound();
return;
}
_savingSound = true;
// Yield thread to let UI to update
await Task.Yield();
await SoundSynthsByMachineService.CreateAsync(_addingSoundId.Value, Id);
_machineSound = await SoundSynthsByMachineService.GetByMachine(Id);
_addingSound = false;
_savingSound = false;
_addingSoundId = null;
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
}
}

View File

@@ -63,6 +63,7 @@ namespace Marechai.Services
services.AddScoped<ResolutionsService>();
services.AddScoped<CompanyLogosService>();
services.AddScoped<GpuByMachineService>();
services.AddScoped<SoundSynthsByMachineService>();
}
}
}

View File

@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Marechai.ViewModels;
using Microsoft.EntityFrameworkCore;
namespace Marechai.Services
{
public class SoundSynthsByMachineService
{
readonly MarechaiContext _context;
public SoundSynthsByMachineService(MarechaiContext context) => _context = context;
public async Task<List<SoundSynthByMachineViewModel>> GetByMachine(int machineId) =>
await _context.SoundByMachine.Where(g => g.MachineId == machineId).
Select(g => new SoundSynthByMachineViewModel
{
Id = g.Id, Name = g.SoundSynth.Name, CompanyName = g.SoundSynth.Company.Name,
SoundSynthId = g.SoundSynthId, MachineId = g.MachineId
}).OrderBy(g => g.CompanyName).ThenBy(g => g.Name).ToListAsync();
public async Task DeleteAsync(long id)
{
SoundByMachine item = await _context.SoundByMachine.FindAsync(id);
if(item is null)
return;
_context.SoundByMachine.Remove(item);
await _context.SaveChangesAsync();
}
public async Task<long> CreateAsync(int soundSynthId, int machineId)
{
var item = new SoundByMachine
{
SoundSynthId = soundSynthId, MachineId = machineId
};
await _context.SoundByMachine.AddAsync(item);
await _context.SaveChangesAsync();
return item.Id;
}
}
}

View File

@@ -0,0 +1,10 @@
namespace Marechai.ViewModels
{
public class SoundSynthByMachineViewModel : BaseViewModel<long>
{
public int SoundSynthId { get; set; }
public int MachineId { get; set; }
public string CompanyName { get; set; }
public string Name { get; set; }
}
}