Add machine families creation in admin view.

This commit is contained in:
2020-05-28 02:50:45 +01:00
parent cb6cde600d
commit 8c9a0c9111
7 changed files with 44 additions and 263 deletions

View File

@@ -1,182 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : MachineFamiliesController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Machine families 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 Marechai.ViewModels;
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 MachineFamiliesController : Controller
{
readonly MarechaiContext _context;
public MachineFamiliesController(MarechaiContext context) => _context = context;
// GET: Admin/MachineFamilies
public async Task<IActionResult> Index()
{
IIncludableQueryable<MachineFamily, Company> marechaiContext =
_context.MachineFamilies.Include(m => m.Company);
return View(await marechaiContext.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
Select(m => new MachineFamilyViewModel
{
Id = m.Id, Company = m.Company.Name, Name = m.Name
}).ToListAsync());
}
// GET: Admin/MachineFamilies/Details/5
public async Task<IActionResult> Details(int? id)
{
if(id == null)
return NotFound();
MachineFamily machineFamily =
await _context.MachineFamilies.Include(m => m.Company).FirstOrDefaultAsync(m => m.Id == id);
if(machineFamily == null)
return NotFound();
return View(machineFamily);
}
// GET: Admin/MachineFamilies/Create
public IActionResult Create()
{
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name");
return View();
}
// POST: Admin/MachineFamilies/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")] MachineFamily machineFamily)
{
if(ModelState.IsValid)
{
_context.Add(machineFamily);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machineFamily.CompanyId);
return View(machineFamily);
}
// GET: Admin/MachineFamilies/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null)
return NotFound();
MachineFamily machineFamily = await _context.MachineFamilies.FindAsync(id);
if(machineFamily == null)
return NotFound();
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machineFamily.CompanyId);
return View(machineFamily);
}
// POST: Admin/MachineFamilies/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")] MachineFamily machineFamily)
{
if(id != machineFamily.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(machineFamily);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!MachineFamilyExists(machineFamily.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] = new SelectList(_context.Companies, "Id", "Name", machineFamily.CompanyId);
return View(machineFamily);
}
// GET: Admin/MachineFamilies/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null)
return NotFound();
MachineFamily machineFamily =
await _context.MachineFamilies.Include(m => m.Company).FirstOrDefaultAsync(m => m.Id == id);
if(machineFamily == null)
return NotFound();
return View(machineFamily);
}
// POST: Admin/MachineFamilies/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
MachineFamily machineFamily = await _context.MachineFamilies.FindAsync(id);
_context.MachineFamilies.Remove(machineFamily);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool MachineFamilyExists(int id) => _context.MachineFamilies.Any(e => e.Id == id);
}
}

View File

@@ -1,70 +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.MachineFamily
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>Machine family</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">
<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

@@ -136,5 +136,6 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSets\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\MachineFamilies\Create.cshtml" />
</ItemGroup>
</Project>

View File

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

View File

@@ -11,6 +11,7 @@ namespace Marechai.Pages.Admin.Details
public partial class MachineFamily
{
List<CompanyViewModel> _companies;
bool _creating;
bool _editing;
bool _loaded;
MachineFamilyViewModel _model;
@@ -24,14 +25,20 @@ namespace Marechai.Pages.Admin.Details
_loaded = true;
if(Id <= 0)
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machine_families/create",
StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
return;
_companies = await CompaniesService.GetAsync();
_model = await Service.GetAsync(Id);
_model = _creating ? new MachineFamilyViewModel() : await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machine_families/edit/", StringComparison.InvariantCulture);
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machine_families/edit/",
StringComparison.InvariantCulture);
StateHasChanged();
}
@@ -45,7 +52,15 @@ namespace Marechai.Pages.Admin.Details
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/machine_families");
return;
}
_model = await Service.GetAsync(Id);
StateHasChanged();
}
@@ -55,9 +70,14 @@ namespace Marechai.Pages.Admin.Details
_model.Name?.Length > 255)
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);
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/machine_families/create">@L["Create new"]</a>
</p>
<table class="table table-striped">
<thead>

View File

@@ -39,6 +39,19 @@ namespace Marechai.Services
await _context.SaveChangesAsync();
}
public async Task<int> CreateAsync(MachineFamilyViewModel viewModel)
{
var model = new MachineFamily
{
Name = viewModel.Name, CompanyId = viewModel.CompanyId
};
await _context.MachineFamilies.AddAsync(model);
await _context.SaveChangesAsync();
return model.Id;
}
public async Task DeleteAsync(int id)
{
MachineFamily item = await _context.MachineFamilies.FindAsync(id);