Add company creation in admin view.

This commit is contained in:
2020-05-27 22:30:06 +01:00
parent f5d8f2bcca
commit 94e841c2cf
7 changed files with 40 additions and 371 deletions

View File

@@ -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);
}
}

View File

@@ -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"); }
}

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1236</Version> <Version>3.0.99.1241</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <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\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Index.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Index.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

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

View File

@@ -32,6 +32,7 @@
@page "/admin/companies/details/{Id:int}" @page "/admin/companies/details/{Id:int}"
@page "/admin/companies/edit/{Id:int}" @page "/admin/companies/edit/{Id:int}"
@page "/admin/companies/create"
@using Marechai.Database @using Marechai.Database
@inherits OwningComponentBase<CompaniesService> @inherits OwningComponentBase<CompaniesService>
@inject IStringLocalizer<CompaniesService> L @inject IStringLocalizer<CompaniesService> L
@@ -133,7 +134,7 @@
@if (_editing || _model.Facebook != null) @if (_editing || _model.Facebook != null)
{ {
<Field> <Field>
<TextEdit ReadOnly="!_editing" @bind-Text="@_model.Facebook" /> <FieldLabel>@L["Facebook"]</FieldLabel>
@if (_editing) @if (_editing)
{ {
<Check TValue="bool" @bind-Checked="@_unknownFacebook">@L["Unknown (facebook)"]</Check> <Check TValue="bool" @bind-Checked="@_unknownFacebook">@L["Unknown (facebook)"]</Check>
@@ -172,7 +173,7 @@
} }
</Field> </Field>
} }
@if (_editing || _model.Address != null) @if (_editing || _model.City != null)
{ {
<Field> <Field>
<FieldLabel>@L["City"]</FieldLabel> <FieldLabel>@L["City"]</FieldLabel>
@@ -214,7 +215,7 @@
} }
</Field> </Field>
} }
@if (_editing || _model.Address != null) @if (_editing || _model.PostalCode != null)
{ {
<Field> <Field>
<FieldLabel>@L["Postal code"]</FieldLabel> <FieldLabel>@L["Postal code"]</FieldLabel>
@@ -292,7 +293,7 @@
<Select Disabled="!_editing" TValue="int?" @bind-SelectedValue="@_model.SoldToId"> <Select Disabled="!_editing" TValue="int?" @bind-SelectedValue="@_model.SoldToId">
@foreach (var company in _companies) @foreach (var company in _companies)
{ {
<SelectItem TValue="short?" Value="@company.Id">@company.Name</SelectItem> <SelectItem TValue="int?" Value="@company.Id">@company.Name</SelectItem>
} }
</Select> </Select>
} }

View File

@@ -14,6 +14,7 @@ namespace Marechai.Pages.Admin.Details
{ {
List<CompanyViewModel> _companies; List<CompanyViewModel> _companies;
List<Iso31661Numeric> _countries; List<Iso31661Numeric> _countries;
bool _creating;
bool _editing; bool _editing;
bool _loaded; bool _loaded;
CompanyViewModel _model; CompanyViewModel _model;
@@ -44,15 +45,20 @@ namespace Marechai.Pages.Admin.Details
_loaded = true; _loaded = true;
if(Id <= 0) _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/companies/create", StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
return; return;
_countries = await CountriesService.GetAsync(); _countries = await CountriesService.GetAsync();
_companies = await Service.GetAsync(); _companies = await Service.GetAsync();
_model = await Service.GetAsync(Id); _model = _creating ? new CompanyViewModel() : await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/companies/edit/", StringComparison.InvariantCulture); StartsWith("admin/companies/edit/",
StringComparison.InvariantCulture);
if(_editing) if(_editing)
SetCheckboxes(); SetCheckboxes();
@@ -85,6 +91,14 @@ namespace Marechai.Pages.Admin.Details
async void OnCancelClicked() async void OnCancelClicked()
{ {
_editing = false; _editing = false;
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/companies");
return;
}
_model = await Service.GetAsync(Id); _model = await Service.GetAsync(Id);
SetCheckboxes(); SetCheckboxes();
StateHasChanged(); StateHasChanged();
@@ -155,8 +169,13 @@ namespace Marechai.Pages.Admin.Details
else if(_model.Sold?.Date >= DateTime.UtcNow.Date) else if(_model.Sold?.Date >= DateTime.UtcNow.Date)
return; return;
_editing = false; if(_creating)
Id = await Service.CreateAsync(_model);
else
await Service.UpdateAsync(_model); await Service.UpdateAsync(_model);
_editing = false;
_creating = false;
_model = await Service.GetAsync(Id); _model = await Service.GetAsync(Id);
SetCheckboxes(); SetCheckboxes();
StateHasChanged(); StateHasChanged();

View File

@@ -227,7 +227,7 @@
@if (_editing || _model.Facebook != null) @if (_editing || _model.Facebook != null)
{ {
<Field> <Field>
<TextEdit ReadOnly="!_editing" @bind-Text="@_model.Facebook" /> <FieldLabel>@L["Facebook"]</FieldLabel>
@if (_editing) @if (_editing)
{ {
<Check TValue="bool" @bind-Checked="@_unknownFacebook">@L["Unknown (facebook)"]</Check> <Check TValue="bool" @bind-Checked="@_unknownFacebook">@L["Unknown (facebook)"]</Check>