diff --git a/Marechai/Areas/Admin/Controllers/MachinesController.cs b/Marechai/Areas/Admin/Controllers/MachinesController.cs deleted file mode 100644 index c72528b0..00000000 --- a/Marechai/Areas/Admin/Controllers/MachinesController.cs +++ /dev/null @@ -1,233 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : MachinesController.cs -// Author(s) : Natalia Portillo -// -// --[ Description ] ---------------------------------------------------------- -// -// Machines 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; -using System.Linq; -using System.Threading.Tasks; -using Marechai.Areas.Admin.Models; -using Marechai.Database; -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 MachinesController : Controller - { - readonly MarechaiContext _context; - - public MachinesController(MarechaiContext context) => _context = context; - - // GET: Admin/Machines - public async Task Index() - { - IIncludableQueryable marechaiContext = - _context.Machines.Include(m => m.Company).Include(m => m.Family); - - return View(await marechaiContext.OrderBy(m => m.Company.Name).ThenBy(m => m.Name). - ThenBy(m => m.Family.Name).Select(m => new MachineViewModel - { - Id = m.Id, Company = m.Company.Name, Name = m.Name, - Model = m.Model, - Introduced = m.Introduced, Type = m.Type, Family = m.Family.Name - }).ToListAsync()); - } - - // GET: Admin/Machines/Details/5 - public async Task Details(int? id) - { - if(id == null) - return NotFound(); - - Machine machine = await _context.Machines.Include(m => m.Company).Include(m => m.Family). - FirstOrDefaultAsync(m => m.Id == id); - - if(machine == null) - return NotFound(); - - return View(machine); - } - - // GET: Admin/Machines/Create - public IActionResult Create() - { - ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name"); - ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name"); - - return View(); - } - - // POST: Admin/Machines/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,CompanyId,Name,Type,Introduced,FamilyId,Model")] - Machine machine) - { - if(ModelState.IsValid) - { - _context.Add(machine); - await _context.SaveChangesAsync(); - - var newsType = new NewsType(); - - switch(machine.Type) - { - case MachineType.Computer: - newsType = NewsType.NewComputerInDb; - - break; - case MachineType.Console: - newsType = NewsType.NewConsoleInDb; - - break; - } - - _context.News.Add(new News - { - AddedId = machine.Id, Date = DateTime.UtcNow, Type = newsType - }); - - await _context.SaveChangesAsync(); - - return RedirectToAction(nameof(Index)); - } - - ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machine.CompanyId); - ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name", machine.FamilyId); - - return View(machine); - } - - // GET: Admin/Machines/Edit/5 - public async Task Edit(int? id) - { - if(id == null) - return NotFound(); - - Machine machine = await _context.Machines.FindAsync(id); - - if(machine == null) - return NotFound(); - - ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machine.CompanyId); - ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name", machine.FamilyId); - - return View(machine); - } - - // POST: Admin/Machines/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,CompanyId,Name,Type,Introduced,FamilyId,Model")] - Machine machine) - { - if(id != machine.Id) - return NotFound(); - - if(ModelState.IsValid) - { - try - { - _context.Update(machine); - await _context.SaveChangesAsync(); - - var newsType = new NewsType(); - - switch(machine.Type) - { - case MachineType.Computer: - newsType = NewsType.UpdatedComputerInDb; - - break; - case MachineType.Console: - newsType = NewsType.UpdatedConsoleInDb; - - break; - } - - _context.News.Add(new News - { - AddedId = machine.Id, Date = DateTime.UtcNow, Type = newsType - }); - - await _context.SaveChangesAsync(); - } - catch(DbUpdateConcurrencyException) - { - if(!MachineExists(machine.Id)) - return NotFound(); - - throw; - } - - return RedirectToAction(nameof(Index)); - } - - ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machine.CompanyId); - ViewData["FamilyId"] = new SelectList(_context.MachineFamilies, "Id", "Name", machine.FamilyId); - - return View(machine); - } - - // GET: Admin/Machines/Delete/5 - public async Task Delete(int? id) - { - if(id == null) - return NotFound(); - - Machine machine = await _context.Machines.Include(m => m.Company).Include(m => m.Family). - FirstOrDefaultAsync(m => m.Id == id); - - if(machine == null) - return NotFound(); - - return View(machine); - } - - // POST: Admin/Machines/Delete/5 - [HttpPost, ActionName("Delete"), ValidateAntiForgeryToken] - public async Task DeleteConfirmed(int id) - { - Machine machine = await _context.Machines.FindAsync(id); - _context.Machines.Remove(machine); - await _context.SaveChangesAsync(); - - return RedirectToAction(nameof(Index)); - } - - bool MachineExists(int id) => _context.Machines.Any(e => e.Id == id); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/Machines/Create.cshtml b/Marechai/Areas/Admin/Views/Machines/Create.cshtml deleted file mode 100644 index 752e7c41..00000000 --- a/Marechai/Areas/Admin/Views/Machines/Create.cshtml +++ /dev/null @@ -1,99 +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 -*******************************************************************************/ -} -@using Marechai.Database -@model Marechai.Database.Models.Machine - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

Machine

-
-
-
-
-
-
-
- - -
-
- - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index b4fb573d..1595e953 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -137,5 +137,6 @@ <_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Edit.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\MachineFamilies\Create.cshtml" /> + <_ContentIncludedByDefault Remove="Areas\Admin\Views\Machines\Create.cshtml" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Machine.razor b/Marechai/Pages/Admin/Details/Machine.razor index b6ba8c0c..0114e8c0 100644 --- a/Marechai/Pages/Admin/Details/Machine.razor +++ b/Marechai/Pages/Admin/Details/Machine.razor @@ -32,6 +32,7 @@ @page "/admin/machines/details/{Id:int}" @page "/admin/machines/edit/{Id:int}" +@page "/admin/machines/create" @using Marechai.Database @inherits OwningComponentBase @inject IStringLocalizer L diff --git a/Marechai/Pages/Admin/Details/Machine.razor.cs b/Marechai/Pages/Admin/Details/Machine.razor.cs index 02f4736e..fcd5f89e 100644 --- a/Marechai/Pages/Admin/Details/Machine.razor.cs +++ b/Marechai/Pages/Admin/Details/Machine.razor.cs @@ -12,6 +12,7 @@ namespace Marechai.Pages.Admin.Details public partial class Machine { List _companies; + bool _creating; bool _editing; List _families; bool _loaded; @@ -36,15 +37,20 @@ namespace Marechai.Pages.Admin.Details _loaded = true; - if(Id <= 0) + _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/machines/create", StringComparison.InvariantCulture); + + if(Id <= 0 && + !_creating) return; _companies = await CompaniesService.GetAsync(); _families = await MachineFamiliesService.GetAsync(); - _model = await Service.GetAsync(Id); + _model = _creating ? new MachineViewModel() : await Service.GetAsync(Id); - _editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). - StartsWith("admin/machines/edit/", StringComparison.InvariantCulture); + _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/machines/edit/", + StringComparison.InvariantCulture); if(_editing) SetCheckboxes(); @@ -70,7 +76,15 @@ namespace Marechai.Pages.Admin.Details async void OnCancelClicked() { _editing = false; - _model = await Service.GetAsync(Id); + + if(_creating) + { + NavigationManager.ToBaseRelativePath("admin/machines"); + + return; + } + + _model = await Service.GetAsync(Id); SetCheckboxes(); StateHasChanged(); } @@ -94,9 +108,14 @@ namespace Marechai.Pages.Admin.Details else if(string.IsNullOrWhiteSpace(_model.Model)) 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/Machines.razor b/Marechai/Pages/Admin/Machines.razor index ad7381ed..09537740 100644 --- a/Marechai/Pages/Admin/Machines.razor +++ b/Marechai/Pages/Admin/Machines.razor @@ -42,9 +42,7 @@ return; }

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

diff --git a/Marechai/Services/MachinesService.cs b/Marechai/Services/MachinesService.cs index de8c637e..d371ca2c 100644 --- a/Marechai/Services/MachinesService.cs +++ b/Marechai/Services/MachinesService.cs @@ -61,6 +61,20 @@ namespace Marechai.Services await _context.SaveChangesAsync(); } + public async Task CreateAsync(MachineViewModel viewModel) + { + var model = new Machine + { + CompanyId = viewModel.CompanyId, Name = viewModel.Name, Model = viewModel.Model, + Introduced = viewModel.Introduced, Type = viewModel.Type, FamilyId = viewModel.FamilyId + }; + + await _context.Machines.AddAsync(model); + await _context.SaveChangesAsync(); + + return model.Id; + } + public async Task GetMachine(int id) { Machine machine = await _context.Machines.FindAsync(id);