From b5263b67ba408f747d78f3982d7717bea51d5f1f Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 28 May 2020 03:22:10 +0100 Subject: [PATCH] Add sound synthesizer creation in admin view. --- .../Controllers/SoundSynthsController.cs | 185 ------------------ .../Admin/Views/SoundSynths/Create.cshtml | 126 ------------ Marechai/Marechai.csproj | 1 + Marechai/Pages/Admin/Details/SoundSynth.razor | 1 + .../Pages/Admin/Details/SoundSynth.razor.cs | 35 +++- Marechai/Pages/Admin/SoundSynths.razor | 4 +- Marechai/Services/SoundSynthsService.cs | 16 ++ 7 files changed, 46 insertions(+), 322 deletions(-) delete mode 100644 Marechai/Areas/Admin/Controllers/SoundSynthsController.cs delete mode 100644 Marechai/Areas/Admin/Views/SoundSynths/Create.cshtml diff --git a/Marechai/Areas/Admin/Controllers/SoundSynthsController.cs b/Marechai/Areas/Admin/Controllers/SoundSynthsController.cs deleted file mode 100644 index 0adf6cd4..00000000 --- a/Marechai/Areas/Admin/Controllers/SoundSynthsController.cs +++ /dev/null @@ -1,185 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : SoundSynthsController.cs -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// 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 Index() - { - IIncludableQueryable 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 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 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 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 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 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 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); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/SoundSynths/Create.cshtml b/Marechai/Areas/Admin/Views/SoundSynths/Create.cshtml deleted file mode 100644 index 45b07193..00000000 --- a/Marechai/Areas/Admin/Views/SoundSynths/Create.cshtml +++ /dev/null @@ -1,126 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Create.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model Marechai.Database.Models.SoundSynth - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

Sound synthetizer

-
-
-
-
-
-
-
- - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 30b320b3..0a19cc46 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -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" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/SoundSynth.razor b/Marechai/Pages/Admin/Details/SoundSynth.razor index 9fc9ad89..c0dffc3f 100644 --- a/Marechai/Pages/Admin/Details/SoundSynth.razor +++ b/Marechai/Pages/Admin/Details/SoundSynth.razor @@ -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 @inject IStringLocalizer L @inject CompaniesService CompaniesService diff --git a/Marechai/Pages/Admin/Details/SoundSynth.razor.cs b/Marechai/Pages/Admin/Details/SoundSynth.razor.cs index 6feab9fc..9bf123c6 100644 --- a/Marechai/Pages/Admin/Details/SoundSynth.razor.cs +++ b/Marechai/Pages/Admin/Details/SoundSynth.razor.cs @@ -11,6 +11,7 @@ namespace Marechai.Pages.Admin.Details public partial class SoundSynth { List _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,7 +79,15 @@ namespace Marechai.Pages.Admin.Details async void OnCancelClicked() { _editing = false; - _model = await Service.GetAsync(Id); + + if(_creating) + { + NavigationManager.ToBaseRelativePath("admin/sound_synths"); + + return; + } + + _model = await Service.GetAsync(Id); SetCheckboxes(); StateHasChanged(); } @@ -127,9 +141,14 @@ namespace Marechai.Pages.Admin.Details else if(_model.Type < 0) return; - _editing = false; - await Service.UpdateAsync(_model); - _model = await Service.GetAsync(Id); + if(_creating) + Id = await Service.CreateAsync(_model); + else + await Service.UpdateAsync(_model); + + _editing = false; + _creating = false; + _model = await Service.GetAsync(Id); SetCheckboxes(); StateHasChanged(); } diff --git a/Marechai/Pages/Admin/SoundSynths.razor b/Marechai/Pages/Admin/SoundSynths.razor index dc19f97d..f6c01f99 100644 --- a/Marechai/Pages/Admin/SoundSynths.razor +++ b/Marechai/Pages/Admin/SoundSynths.razor @@ -42,9 +42,7 @@ return; }

- - @L["Create new"] - + @L["Create new"]

diff --git a/Marechai/Services/SoundSynthsService.cs b/Marechai/Services/SoundSynthsService.cs index 6f6f4736..570b6671 100644 --- a/Marechai/Services/SoundSynthsService.cs +++ b/Marechai/Services/SoundSynthsService.cs @@ -71,6 +71,22 @@ namespace Marechai.Services await _context.SaveChangesAsync(); } + public async Task 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);