mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add company creation in admin view.
This commit is contained in:
@@ -1,199 +0,0 @@
|
||||
/******************************************************************************
|
||||
// MARECHAI: Master repository of computing history artifacts information
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : CompaniesController.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Companies 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 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 CompaniesController : Controller
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
|
||||
public CompaniesController(MarechaiContext context) => _context = context;
|
||||
|
||||
// GET: Admin/Companies
|
||||
public IActionResult Index()
|
||||
{
|
||||
IIncludableQueryable<Company, Company> marechaiContext =
|
||||
_context.Companies.Include(c => c.Country).Include(c => c.SoldTo);
|
||||
|
||||
return View(marechaiContext.OrderBy(c => c.Name).Select(c => new CompanyViewModel
|
||||
{
|
||||
Id = c.Id, Name = c.Name, Founded = c.Founded, Status = c.Status,
|
||||
Country = c.Country.Name, Sold = c.Sold, SoldTo = c.SoldTo.Name
|
||||
}));
|
||||
}
|
||||
|
||||
// GET: Admin/Companies/Details/5
|
||||
public async Task<IActionResult> Details(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Company company = await _context.Companies.Include(c => c.Country).Include(c => c.SoldTo).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(company == null)
|
||||
return NotFound();
|
||||
|
||||
return View(company);
|
||||
}
|
||||
|
||||
// GET: Admin/Companies/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
ViewData["CountryId"] = new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name");
|
||||
ViewData["SoldToId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name");
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Admin/Companies/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,Name,Founded,Website,Twitter,Facebook,Sold,SoldToId,Address,City,Province,PostalCode,CountryId,Status")]
|
||||
Company company)
|
||||
{
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
_context.Add(company);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["CountryId"] =
|
||||
new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId);
|
||||
|
||||
ViewData["SoldToId"] =
|
||||
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId);
|
||||
|
||||
return View(company);
|
||||
}
|
||||
|
||||
// GET: Admin/Companies/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Company company = await _context.Companies.FindAsync(id);
|
||||
|
||||
if(company == null)
|
||||
return NotFound();
|
||||
|
||||
ViewData["CountryId"] =
|
||||
new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId);
|
||||
|
||||
ViewData["SoldToId"] =
|
||||
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId);
|
||||
|
||||
return View(company);
|
||||
}
|
||||
|
||||
// POST: Admin/Companies/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,Name,Founded,Website,Twitter,Facebook,Sold,SoldToId,Address,City,Province,PostalCode,CountryId,Status")]
|
||||
Company company)
|
||||
{
|
||||
if(id != company.Id)
|
||||
return NotFound();
|
||||
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Update(company);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch(DbUpdateConcurrencyException)
|
||||
{
|
||||
if(!CompanyExists(company.Id))
|
||||
return NotFound();
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["CountryId"] =
|
||||
new SelectList(_context.Iso31661Numeric.OrderBy(c => c.Name), "Id", "Name", company.CountryId);
|
||||
|
||||
ViewData["SoldToId"] =
|
||||
new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name", company.SoldToId);
|
||||
|
||||
return View(company);
|
||||
}
|
||||
|
||||
// GET: Admin/Companies/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Company company = await _context.Companies.Include(c => c.Country).Include(c => c.SoldTo).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(company == null)
|
||||
return NotFound();
|
||||
|
||||
return View(company);
|
||||
}
|
||||
|
||||
// POST: Admin/Companies/Delete/5
|
||||
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
Company company = await _context.Companies.FindAsync(id);
|
||||
_context.Companies.Remove(company);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
bool CompanyExists(int id) => _context.Companies.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -1,151 +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.Company
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Create";
|
||||
}
|
||||
<h2>Create</h2>
|
||||
<h4>Company</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="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="Founded" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Founded" class="form-control" />
|
||||
<span asp-validation-for="Founded" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Website" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Website" class="form-control" />
|
||||
<span asp-validation-for="Website" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Twitter" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Twitter" class="form-control" />
|
||||
<span asp-validation-for="Twitter" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Facebook" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Facebook" class="form-control" />
|
||||
<span asp-validation-for="Facebook" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Sold" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Sold" class="form-control" />
|
||||
<span asp-validation-for="Sold" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="SoldTo" class="control-label">
|
||||
</label>
|
||||
<select asp-for="SoldToId" class="form-control" asp-items="ViewBag.SoldToId">
|
||||
<option selected value="">
|
||||
None or unknown
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Address" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Address" class="form-control" />
|
||||
<span asp-validation-for="Address" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="City" class="control-label">
|
||||
</label>
|
||||
<input asp-for="City" class="form-control" />
|
||||
<span asp-validation-for="City" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Province" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Province" class="form-control" />
|
||||
<span asp-validation-for="Province" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="PostalCode" class="control-label">
|
||||
</label>
|
||||
<input asp-for="PostalCode" class="form-control" />
|
||||
<span asp-validation-for="PostalCode" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Country" class="control-label">
|
||||
</label>
|
||||
<select asp-for="CountryId" class="form-control" asp-items="ViewBag.CountryId">
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Status" class="control-label">
|
||||
</label>
|
||||
<select asp-for="Status" class="form-control" asp-items="Html.GetEnumSelectList<CompanyStatus>().OrderBy(s => s.Text)">
|
||||
</select>
|
||||
<span asp-validation-for="Status" 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"); }
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<Version>3.0.99.1236</Version>
|
||||
<Version>3.0.99.1241</Version>
|
||||
<Company>Canary Islands Computer Museum</Company>
|
||||
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
|
||||
<Product>Canary Islands Computer Museum Website</Product>
|
||||
@@ -128,5 +128,6 @@
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Edit.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Index.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -43,9 +43,7 @@
|
||||
return;
|
||||
}
|
||||
<p>
|
||||
<span class="btn btn-primary">
|
||||
@L["Create new"]
|
||||
</span>
|
||||
<a class="btn btn-primary" href="/admin/companies/create">@L["Create new"]</a>
|
||||
</p>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
@page "/admin/companies/details/{Id:int}"
|
||||
@page "/admin/companies/edit/{Id:int}"
|
||||
@page "/admin/companies/create"
|
||||
@using Marechai.Database
|
||||
@inherits OwningComponentBase<CompaniesService>
|
||||
@inject IStringLocalizer<CompaniesService> L
|
||||
@@ -133,7 +134,7 @@
|
||||
@if (_editing || _model.Facebook != null)
|
||||
{
|
||||
<Field>
|
||||
<TextEdit ReadOnly="!_editing" @bind-Text="@_model.Facebook" />
|
||||
<FieldLabel>@L["Facebook"]</FieldLabel>
|
||||
@if (_editing)
|
||||
{
|
||||
<Check TValue="bool" @bind-Checked="@_unknownFacebook">@L["Unknown (facebook)"]</Check>
|
||||
@@ -172,7 +173,7 @@
|
||||
}
|
||||
</Field>
|
||||
}
|
||||
@if (_editing || _model.Address != null)
|
||||
@if (_editing || _model.City != null)
|
||||
{
|
||||
<Field>
|
||||
<FieldLabel>@L["City"]</FieldLabel>
|
||||
@@ -214,7 +215,7 @@
|
||||
}
|
||||
</Field>
|
||||
}
|
||||
@if (_editing || _model.Address != null)
|
||||
@if (_editing || _model.PostalCode != null)
|
||||
{
|
||||
<Field>
|
||||
<FieldLabel>@L["Postal code"]</FieldLabel>
|
||||
@@ -263,13 +264,13 @@
|
||||
<FieldLabel>@L["Sold"]</FieldLabel>
|
||||
@if (_editing)
|
||||
{
|
||||
<Check TValue="bool" @ bind-Checked="@_unknownSold">@L["Unknown (sold/merged/bankrupt date)"]</Check>
|
||||
<Check TValue="bool" @bind-Checked="@_unknownSold">@L["Unknown (sold/merged/bankrupt date)"]</Check>
|
||||
}
|
||||
@if (!_editing ||
|
||||
!_unknownSold)
|
||||
{
|
||||
<Validation Validator="@ValidateSold">
|
||||
<DateEdit TValue="DateTime?" ReadOnly="!_editing" @ bind-Text="@_model.Sold">
|
||||
<DateEdit TValue="DateTime?" ReadOnly="!_editing" @bind-Text="@_model.Sold">
|
||||
<Feedback>
|
||||
<ValidationError>@L["Please enter a valid sold/merge/bankruptcy date."]</ValidationError>
|
||||
</Feedback>
|
||||
@@ -284,15 +285,15 @@
|
||||
<FieldLabel>@L["Sold to"]</FieldLabel>
|
||||
@if (_editing)
|
||||
{
|
||||
<Check TValue="bool" @ bind-Checked="@_unknownSoldTo">@L["Unknown (sold to)"]</Check>
|
||||
<Check TValue="bool" @bind-Checked="@_unknownSoldTo">@L["Unknown (sold to)"]</Check>
|
||||
}
|
||||
@if (!_editing ||
|
||||
!_unknownSoldTo)
|
||||
{
|
||||
<Select Disabled="!_editing" TValue="int?" @ bind-SelectedValue="@_model.SoldToId">
|
||||
<Select Disabled="!_editing" TValue="int?" @bind-SelectedValue="@_model.SoldToId">
|
||||
@foreach (var company in _companies)
|
||||
{
|
||||
<SelectItem TValue="short?" Value="@company.Id">@company.Name</SelectItem>
|
||||
<SelectItem TValue="int?" Value="@company.Id">@company.Name</SelectItem>
|
||||
}
|
||||
</Select>
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Marechai.Pages.Admin.Details
|
||||
{
|
||||
List<CompanyViewModel> _companies;
|
||||
List<Iso31661Numeric> _countries;
|
||||
bool _creating;
|
||||
bool _editing;
|
||||
bool _loaded;
|
||||
CompanyViewModel _model;
|
||||
@@ -44,15 +45,20 @@ namespace Marechai.Pages.Admin.Details
|
||||
|
||||
_loaded = true;
|
||||
|
||||
if(Id <= 0)
|
||||
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/companies/create", StringComparison.InvariantCulture);
|
||||
|
||||
if(Id <= 0 &&
|
||||
!_creating)
|
||||
return;
|
||||
|
||||
_countries = await CountriesService.GetAsync();
|
||||
_companies = await Service.GetAsync();
|
||||
_model = await Service.GetAsync(Id);
|
||||
_model = _creating ? new CompanyViewModel() : await Service.GetAsync(Id);
|
||||
|
||||
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/companies/edit/", StringComparison.InvariantCulture);
|
||||
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/companies/edit/",
|
||||
StringComparison.InvariantCulture);
|
||||
|
||||
if(_editing)
|
||||
SetCheckboxes();
|
||||
@@ -85,7 +91,15 @@ namespace Marechai.Pages.Admin.Details
|
||||
async void OnCancelClicked()
|
||||
{
|
||||
_editing = false;
|
||||
_model = await Service.GetAsync(Id);
|
||||
|
||||
if(_creating)
|
||||
{
|
||||
NavigationManager.ToBaseRelativePath("admin/companies");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_model = await Service.GetAsync(Id);
|
||||
SetCheckboxes();
|
||||
StateHasChanged();
|
||||
}
|
||||
@@ -155,9 +169,14 @@ namespace Marechai.Pages.Admin.Details
|
||||
else if(_model.Sold?.Date >= DateTime.UtcNow.Date)
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@
|
||||
@if (_editing || _model.Facebook != null)
|
||||
{
|
||||
<Field>
|
||||
<TextEdit ReadOnly="!_editing" @bind-Text="@_model.Facebook" />
|
||||
<FieldLabel>@L["Facebook"]</FieldLabel>
|
||||
@if (_editing)
|
||||
{
|
||||
<Check TValue="bool" @bind-Checked="@_unknownFacebook">@L["Unknown (facebook)"]</Check>
|
||||
|
||||
Reference in New Issue
Block a user