Add sound synthesizer creation in admin view.

This commit is contained in:
2020-05-28 03:22:10 +01:00
parent dadcedd307
commit b5263b67ba
7 changed files with 46 additions and 322 deletions

View File

@@ -1,185 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : SoundSynthsController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Sound synths admin controller
//
// --[ 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-2020 Natalia Portillo
*******************************************************************************/
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 SoundSynthsController : Controller
{
readonly MarechaiContext _context;
public SoundSynthsController(MarechaiContext context) => _context = context;
// GET: Admin/SoundSynths
public async Task<IActionResult> Index()
{
IIncludableQueryable<SoundSynth, Company> marechaiContext = _context.SoundSynths.Include(s => s.Company);
return View(await marechaiContext.OrderBy(s => s.Company).ThenBy(s => s.Name).ThenBy(s => s.ModelCode).
Select(s => new SoundSynthViewModel
{
Company = s.Company.Name, Id = s.Id, Introduced = s.Introduced,
ModelCode = s.ModelCode, Name = s.Name, Type = s.Type
}).ToListAsync());
}
// GET: Admin/SoundSynths/Details/5
public async Task<IActionResult> Details(int? id)
{
if(id == null)
return NotFound();
SoundSynth soundSynth =
await _context.SoundSynths.Include(s => s.Company).FirstOrDefaultAsync(m => m.Id == id);
if(soundSynth == null)
return NotFound();
return View(soundSynth);
}
// GET: Admin/SoundSynths/Create
public IActionResult Create()
{
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name");
return View();
}
// POST: Admin/SoundSynths/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("Id,Name,CompanyId,ModelCode,Introduced,Voices,Frequency,Depth,SquareWave,WhiteNoise,Type")]
SoundSynth soundSynth)
{
if(ModelState.IsValid)
{
_context.Add(soundSynth);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", soundSynth.CompanyId);
return View(soundSynth);
}
// GET: Admin/SoundSynths/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null)
return NotFound();
SoundSynth soundSynth = await _context.SoundSynths.FindAsync(id);
if(soundSynth == null)
return NotFound();
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", soundSynth.CompanyId);
return View(soundSynth);
}
// POST: Admin/SoundSynths/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(
int id, [Bind("Id,Name,CompanyId,ModelCode,Introduced,Voices,Frequency,Depth,SquareWave,WhiteNoise,Type")]
SoundSynth soundSynth)
{
if(id != soundSynth.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(soundSynth);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!SoundSynthExists(soundSynth.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", soundSynth.CompanyId);
return View(soundSynth);
}
// GET: Admin/SoundSynths/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null)
return NotFound();
SoundSynth soundSynth =
await _context.SoundSynths.Include(s => s.Company).FirstOrDefaultAsync(m => m.Id == id);
if(soundSynth == null)
return NotFound();
return View(soundSynth);
}
// POST: Admin/SoundSynths/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
SoundSynth soundSynth = await _context.SoundSynths.FindAsync(id);
_context.SoundSynths.Remove(soundSynth);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool SoundSynthExists(int id) => _context.SoundSynths.Any(e => e.Id == id);
}
}

View File

@@ -1,126 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Create.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ 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-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.SoundSynth
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>Sound synthetizer</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="Company" class="control-label">
</label>
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
</select>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label">
</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="ModelCode" class="control-label">
</label>
<input asp-for="ModelCode" class="form-control" />
<span asp-validation-for="ModelCode" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Introduced" class="control-label">
</label>
<input asp-for="Introduced" class="form-control" />
<span asp-validation-for="Introduced" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Type" class="control-label">
</label>
<input asp-for="Type" class="form-control" />
<span asp-validation-for="Type" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Voices" class="control-label">
</label>
<input asp-for="Voices" class="form-control" />
<span asp-validation-for="Voices" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Frequency" class="control-label">
</label>
<input asp-for="Frequency" class="form-control" />
<span asp-validation-for="Frequency" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Depth" class="control-label">
</label>
<input asp-for="Depth" class="form-control" />
<span asp-validation-for="Depth" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="SquareWave" class="control-label">
</label>
<input asp-for="SquareWave" class="form-control" />
<span asp-validation-for="SquareWave" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="WhiteNoise" class="control-label">
</label>
<input asp-for="WhiteNoise" class="form-control" />
<span asp-validation-for="WhiteNoise" class="text-danger">
</span>
</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>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -141,5 +141,6 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\People\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Processors\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Screens\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\SoundSynths\Create.cshtml" />
</ItemGroup>
</Project>

View File

@@ -32,6 +32,7 @@
@page "/admin/sound_synths/details/{Id:int}"
@page "/admin/sound_synths/edit/{Id:int}"
@page "/admin/sound_synths/create"
@inherits OwningComponentBase<SoundSynthsService>
@inject IStringLocalizer<SoundSynthsService> L
@inject CompaniesService CompaniesService

View File

@@ -11,6 +11,7 @@ namespace Marechai.Pages.Admin.Details
public partial class SoundSynth
{
List<CompanyViewModel> _companies;
bool _creating;
bool _editing;
bool _loaded;
SoundSynthViewModel _model;
@@ -34,14 +35,19 @@ namespace Marechai.Pages.Admin.Details
_loaded = true;
if(Id <= 0)
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/sound_synths/create", StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
return;
_companies = await CompaniesService.GetAsync();
_model = await Service.GetAsync(Id);
_model = _creating ? new SoundSynthViewModel() : await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/sound_synths/edit/", StringComparison.InvariantCulture);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/sound_synths/edit/",
StringComparison.InvariantCulture);
if(_editing)
SetCheckboxes();
@@ -73,6 +79,14 @@ namespace Marechai.Pages.Admin.Details
async void OnCancelClicked()
{
_editing = false;
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/sound_synths");
return;
}
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();
@@ -127,8 +141,13 @@ namespace Marechai.Pages.Admin.Details
else if(_model.Type < 0)
return;
_editing = false;
if(_creating)
Id = await Service.CreateAsync(_model);
else
await Service.UpdateAsync(_model);
_editing = false;
_creating = false;
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();

View File

@@ -42,9 +42,7 @@
return;
}
<p>
<span class="btn btn-primary">
@L["Create new"]
</span>
<a class="btn btn-primary" href="/admin/sound_synths/create">@L["Create new"]</a>
</p>
<table class="table table-striped">
<thead>

View File

@@ -71,6 +71,22 @@ namespace Marechai.Services
await _context.SaveChangesAsync();
}
public async Task<int> CreateAsync(SoundSynthViewModel viewModel)
{
var model = new SoundSynth
{
Depth = viewModel.Depth, Frequency = viewModel.Frequency, Introduced = viewModel.Introduced,
Name = viewModel.Name, Type = viewModel.Type, Voices = viewModel.Voices,
CompanyId = viewModel.CompanyId, ModelCode = viewModel.ModelCode, SquareWave = viewModel.SquareWave,
WhiteNoise = viewModel.WhiteNoise
};
await _context.SoundSynths.AddAsync(model);
await _context.SaveChangesAsync();
return model.Id;
}
public async Task DeleteAsync(int id)
{
SoundSynth item = await _context.SoundSynths.FindAsync(id);