Add machines creation in admin view.

This commit is contained in:
2020-05-28 02:57:49 +01:00
parent 8c9a0c9111
commit 8a9b6eb292
7 changed files with 44 additions and 343 deletions

View File

@@ -1,233 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : MachinesController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// 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<IActionResult> Index()
{
IIncludableQueryable<Machine, MachineFamily> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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);
}
}

View File

@@ -1,99 +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
*******************************************************************************/
}
@using Marechai.Database
@model Marechai.Database.Models.Machine
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>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="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="Family" class="control-label">
</label>
<select asp-for="FamilyId" class="form-control" asp-items="ViewBag.FamilyId">
</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="Model" class="control-label">
</label>
<input asp-for="Model" class="form-control" />
<span asp-validation-for="Model" 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>
<select asp-for="Type" class="form-control" asp-items="Html.GetEnumSelectList<MachineType>().OrderBy(s => s.Text)">
</select>
<span asp-validation-for="Type" 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

@@ -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" />
</ItemGroup>
</Project>

View File

@@ -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<MachinesService>
@inject IStringLocalizer<MachinesService> L

View File

@@ -12,6 +12,7 @@ namespace Marechai.Pages.Admin.Details
public partial class Machine
{
List<CompanyViewModel> _companies;
bool _creating;
bool _editing;
List<MachineFamilyViewModel> _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();
}

View File

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

View File

@@ -61,6 +61,20 @@ namespace Marechai.Services
await _context.SaveChangesAsync();
}
public async Task<int> 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<MachineViewModel> GetMachine(int id)
{
Machine machine = await _context.Machines.FindAsync(id);