diff --git a/Marechai/Areas/Admin/Controllers/CompaniesController.cs b/Marechai/Areas/Admin/Controllers/CompaniesController.cs deleted file mode 100644 index e7b9ed38..00000000 --- a/Marechai/Areas/Admin/Controllers/CompaniesController.cs +++ /dev/null @@ -1,199 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : CompaniesController.cs -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// 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 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 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 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 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 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 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 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); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/Companies/Create.cshtml b/Marechai/Areas/Admin/Views/Companies/Create.cshtml deleted file mode 100644 index 80b1ea3e..00000000 --- a/Marechai/Areas/Admin/Views/Companies/Create.cshtml +++ /dev/null @@ -1,151 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Create.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@using Marechai.Database -@model Marechai.Database.Models.Company - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

Company

-
-
-
-
-
-
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - - - -
-
- - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 79370df0..a216ed51 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1236 + 3.0.99.1241 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website @@ -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" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Companies.razor b/Marechai/Pages/Admin/Companies.razor index 3a3e87d1..befa60d4 100644 --- a/Marechai/Pages/Admin/Companies.razor +++ b/Marechai/Pages/Admin/Companies.razor @@ -43,9 +43,7 @@ return; }

- - @L["Create new"] - + @L["Create new"]

diff --git a/Marechai/Pages/Admin/Details/Company.razor b/Marechai/Pages/Admin/Details/Company.razor index dc9da566..62127756 100644 --- a/Marechai/Pages/Admin/Details/Company.razor +++ b/Marechai/Pages/Admin/Details/Company.razor @@ -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 @inject IStringLocalizer L @@ -133,7 +134,7 @@ @if (_editing || _model.Facebook != null) { - + @L["Facebook"] @if (_editing) { @L["Unknown (facebook)"] @@ -172,7 +173,7 @@ } } - @if (_editing || _model.Address != null) + @if (_editing || _model.City != null) { @L["City"] @@ -214,7 +215,7 @@ } } - @if (_editing || _model.Address != null) + @if (_editing || _model.PostalCode != null) { @L["Postal code"] @@ -263,13 +264,13 @@ @L["Sold"] @if (_editing) { - @L["Unknown (sold/merged/bankrupt date)"] + @L["Unknown (sold/merged/bankrupt date)"] } @if (!_editing || !_unknownSold) { - + @L["Please enter a valid sold/merge/bankruptcy date."] @@ -284,15 +285,15 @@ @L["Sold to"] @if (_editing) { - @L["Unknown (sold to)"] + @L["Unknown (sold to)"] } @if (!_editing || !_unknownSoldTo) { - @foreach (var company in _companies) { - @company.Name + @company.Name } } diff --git a/Marechai/Pages/Admin/Details/Company.razor.cs b/Marechai/Pages/Admin/Details/Company.razor.cs index e4493c98..95965089 100644 --- a/Marechai/Pages/Admin/Details/Company.razor.cs +++ b/Marechai/Pages/Admin/Details/Company.razor.cs @@ -14,6 +14,7 @@ namespace Marechai.Pages.Admin.Details { List _companies; List _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(); } diff --git a/Marechai/Pages/Admin/Details/Person.razor b/Marechai/Pages/Admin/Details/Person.razor index 193d5325..09249640 100644 --- a/Marechai/Pages/Admin/Details/Person.razor +++ b/Marechai/Pages/Admin/Details/Person.razor @@ -227,7 +227,7 @@ @if (_editing || _model.Facebook != null) { - + @L["Facebook"] @if (_editing) { @L["Unknown (facebook)"]