mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add machine families creation in admin view.
This commit is contained in:
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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"); }
|
|
||||||
}
|
|
||||||
@@ -136,5 +136,6 @@
|
|||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSets\Create.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSets\Create.cshtml" />
|
||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Create.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Create.cshtml" />
|
||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Edit.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Edit.cshtml" />
|
||||||
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\MachineFamilies\Create.cshtml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
@page "/admin/machine_families/details/{Id:int}"
|
@page "/admin/machine_families/details/{Id:int}"
|
||||||
@page "/admin/machine_families/edit/{Id:int}"
|
@page "/admin/machine_families/edit/{Id:int}"
|
||||||
|
@page "/admin/machine_families/create"
|
||||||
@inherits OwningComponentBase<MachineFamiliesService>
|
@inherits OwningComponentBase<MachineFamiliesService>
|
||||||
@inject IStringLocalizer<MachineFamiliesService> L
|
@inject IStringLocalizer<MachineFamiliesService> L
|
||||||
@inject CompaniesService CompaniesService
|
@inject CompaniesService CompaniesService
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
public partial class MachineFamily
|
public partial class MachineFamily
|
||||||
{
|
{
|
||||||
List<CompanyViewModel> _companies;
|
List<CompanyViewModel> _companies;
|
||||||
|
bool _creating;
|
||||||
bool _editing;
|
bool _editing;
|
||||||
bool _loaded;
|
bool _loaded;
|
||||||
MachineFamilyViewModel _model;
|
MachineFamilyViewModel _model;
|
||||||
@@ -24,14 +25,20 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
|
|
||||||
_loaded = true;
|
_loaded = true;
|
||||||
|
|
||||||
if(Id <= 0)
|
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||||
|
StartsWith("admin/machine_families/create",
|
||||||
|
StringComparison.InvariantCulture);
|
||||||
|
|
||||||
|
if(Id <= 0 &&
|
||||||
|
!_creating)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_companies = await CompaniesService.GetAsync();
|
_companies = await CompaniesService.GetAsync();
|
||||||
_model = await Service.GetAsync(Id);
|
_model = _creating ? new MachineFamilyViewModel() : await Service.GetAsync(Id);
|
||||||
|
|
||||||
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||||
StartsWith("admin/machine_families/edit/", StringComparison.InvariantCulture);
|
StartsWith("admin/machine_families/edit/",
|
||||||
|
StringComparison.InvariantCulture);
|
||||||
|
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
@@ -45,7 +52,15 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
async void OnCancelClicked()
|
async void OnCancelClicked()
|
||||||
{
|
{
|
||||||
_editing = false;
|
_editing = false;
|
||||||
_model = await Service.GetAsync(Id);
|
|
||||||
|
if(_creating)
|
||||||
|
{
|
||||||
|
NavigationManager.ToBaseRelativePath("admin/machine_families");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_model = await Service.GetAsync(Id);
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +70,14 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
_model.Name?.Length > 255)
|
_model.Name?.Length > 255)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_editing = false;
|
if(_creating)
|
||||||
await Service.UpdateAsync(_model);
|
Id = await Service.CreateAsync(_model);
|
||||||
_model = await Service.GetAsync(Id);
|
else
|
||||||
|
await Service.UpdateAsync(_model);
|
||||||
|
|
||||||
|
_editing = false;
|
||||||
|
_creating = false;
|
||||||
|
_model = await Service.GetAsync(Id);
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,9 +42,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
<p>
|
<p>
|
||||||
<span class="btn btn-primary">
|
<a class="btn btn-primary" href="/admin/machine_families/create">@L["Create new"]</a>
|
||||||
@L["Create new"]
|
|
||||||
</span>
|
|
||||||
</p>
|
</p>
|
||||||
<table class="table table-striped">
|
<table class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
|
|||||||
@@ -39,6 +39,19 @@ namespace Marechai.Services
|
|||||||
await _context.SaveChangesAsync();
|
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)
|
public async Task DeleteAsync(int id)
|
||||||
{
|
{
|
||||||
MachineFamily item = await _context.MachineFamilies.FindAsync(id);
|
MachineFamily item = await _context.MachineFamilies.FindAsync(id);
|
||||||
|
|||||||
Reference in New Issue
Block a user