Move companies by country to Blazor.

This commit is contained in:
2020-05-21 23:32:21 +01:00
parent 8887ddd185
commit 337dd0b131
8 changed files with 73 additions and 109 deletions

View File

@@ -75,23 +75,6 @@ namespace Marechai.Controllers
}).ToList()); }).ToList());
} }
public IActionResult ByCountry(short id)
{
ViewBag.Iso3166 = _context.Iso31661Numeric.FirstOrDefault(i => i.Id == id);
if(ViewBag.Iso3166 is null)
RedirectToAction(nameof(Index));
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
return View(_context.Companies.Include(c => c.Logos).Where(c => c.CountryId == id).OrderBy(c => c.Name).
Select(c => new CompanyViewModel
{
Id = c.Id, LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid,
Name = c.Name
}).ToList());
}
public IActionResult Index() public IActionResult Index()
{ {
ViewBag.WebRootPath = hostingEnvironment.WebRootPath; ViewBag.WebRootPath = hostingEnvironment.WebRootPath;

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.951</Version> <Version>3.0.99.954</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>

View File

@@ -8,7 +8,7 @@
// //
// --[ Description ] ---------------------------------------------------------- // --[ Description ] ----------------------------------------------------------
// //
// Lists all companies // Lists all companies or companies by country
// //
// --[ License ] -------------------------------------------------------------- // --[ License ] --------------------------------------------------------------
// //
@@ -31,6 +31,7 @@
} }
@page "/companies" @page "/companies"
@page "/companies/country/{CountryId:int}"
@inherits OwningComponentBase<CompaniesService> @inherits OwningComponentBase<CompaniesService>
@inject IStringLocalizer<CompaniesService> L @inject IStringLocalizer<CompaniesService> L
@inject IWebHostEnvironment Host @inject IWebHostEnvironment Host
@@ -43,6 +44,24 @@
} }
<p align="center"> <p align="center">
@if (CountryId.HasValue)
{
<b>@string.Format(L["Companies founded in {0}."], L[_countryName])</b>
if (File.Exists(System.IO.Path.Combine(Host.WebRootPath, "assets/flags/countries", CountryId + ".svg")))
{
<picture>
<source type="image/svg+xml" srcset="/assets/flags/countries/@(CountryId).svg">
<source type="image/webp" srcset="/assets/flags/countries/webp/1x/@(CountryId).webp,
/assets/flags/countries/webp/1x/@(CountryId).webp 2x,
/assets/flags/countries/webp/1x/@(CountryId).webp 3x">
<img srcset="/assets/flags/countries/png/1x/@(CountryId).png,
/assets/flags/countries/png/1x/@(CountryId).png 2x,
/assets/flags/countries/png/1x/@(CountryId).webp 3x" src="/assets/flags/countries/png/1x@(CountryId).png" alt="" height="32" />
</picture>
}
<br />
}
@if (_companies.Any()) @if (_companies.Any())
{ {
<p> <p>
@@ -72,15 +91,32 @@
} }
else else
{ {
<p>There are no companies in the database.</p> <p>@L["There are no companies in the database."]</p>
} }
</p> </p>
@code @code
{ {
[Parameter]
public int? CountryId{get;set;}
List<CompanyViewModel> _companies; List<CompanyViewModel> _companies;
string _countryName;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
_companies = await Service.GetCompaniesAsync(); if (CountryId.HasValue)
{
_countryName = await Service.GetCountryNameAsync(CountryId.Value);
if (_countryName != null)
{
_companies = await Service.GetCompaniesByCountryAsync(CountryId.Value);
}
else
CountryId = null;
}
_companies ??= await Service.GetCompaniesAsync();
} }
} }

View File

@@ -242,4 +242,12 @@
<value>&lt;red&gt;Ninguna&lt;/red&gt; videoconsola conocida.</value> <value>&lt;red&gt;Ninguna&lt;/red&gt; videoconsola conocida.</value>
<comment>There are no known computers. Do not translate &lt;red&gt; neither &lt;/red&gt; and keep negation in between.</comment> <comment>There are no known computers. Do not translate &lt;red&gt; neither &lt;/red&gt; and keep negation in between.</comment>
</data> </data>
<data name="There are no companies in the database." xml:space="preserve">
<value>No hay compañías en la base de datos.</value>
<comment>No companies in the database</comment>
</data>
<data name="Companies founded in {0}." xml:space="preserve">
<value>Compañías fundadas en {0}.</value>
<comment>Companies founded in, {0} country name</comment>
</data>
</root> </root>

View File

@@ -80,5 +80,28 @@ namespace Marechai.Services
{ {
Id = c.Id, Name = c.Name Id = c.Id, Name = c.Name
}).FirstOrDefaultAsync(c => c.Id == id); }).FirstOrDefaultAsync(c => c.Id == id);
public async Task<string> GetCountryNameAsync(int id) =>
(await _context.Iso31661Numeric.FirstOrDefaultAsync(c => c.Id == id))?.Name;
public Task<List<CompanyViewModel>> GetCompaniesByCountryAsync(int countryId) => _context.
Companies.
Include(c => c.Logos).
Where(c => c.CountryId ==
countryId).
OrderBy(c => c.Name).
Select(c =>
new CompanyViewModel
{
Id = c.Id,
LastLogo = c.
Logos.
OrderByDescending(l =>
l.
Year).
FirstOrDefault().
Guid,
Name = c.Name
}).ToListAsync();
} }
} }

View File

@@ -1,86 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : ByCountry.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Lists companies by country (or all)
//
// --[ 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
*******************************************************************************/
ViewData["Title"] = "Companies";
}
@using System.IO
@model IEnumerable<Marechai.ViewModels.CompanyViewModel>
<p align="center">
@if (ViewBag.Iso3166 != null)
{
<b>Companies founded in @ViewBag.Iso3166.Name</b>
if (File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/flags/countries", ViewBag.Iso3166.Id + ".svg")))
{
<picture>
<source type="image/svg+xml" srcset="/assets/flags/countries/@(ViewBag.Iso3166.Id).svg">
<source type="image/webp" srcset="/assets/flags/countries/webp/1x/@(ViewBag.Iso3166.Id).webp,
/assets/flags/countries/webp/1x/@(ViewBag.Iso3166.Id).webp 2x,
/assets/flags/countries/webp/1x/@(ViewBag.Iso3166.Id).webp 3x">
<img srcset="/assets/flags/countries/png/1x/@(ViewBag.Iso3166.Id).png,
/assets/flags/countries/png/1x/@(ViewBag.Iso3166.Id).png 2x,
/assets/flags/countries/png/1x/@(ViewBag.Iso3166.Id).webp 3x" src="/assets/flags/countries/png/1x@(ViewBag.Iso3166.Id).png" ) alt="" height="32" />
</picture>
}
<br />
}
@if (Model.Any())
{
<p>
@Model.Count() companies found in the database.
<br />
@foreach (var company in Model)
{
<a asp-controller="Company" asp-action="View" asp-route-id="@company.Id">
@if (company.LastLogo != null &&
File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo + ".svg")))
{
<picture>
<source type="image/svg+xml" srcset="/assets/logos/@(company.LastLogo).svg">
<source type="image/webp" srcset="/assets/logos/thumbs/webp/1x/@(company.LastLogo).webp,
/assets/logos/thumbs/webp/2x/@(company.LastLogo).webp 2x,
/assets/logos/thumbs/webp/3x/@(company.LastLogo).webp 3x">
<img srcset="/assets/logos/thumbs/png/1x/@(company.LastLogo).png,
/assets/logos/thumbs/png/2x/@(company.LastLogo).png 2x,
/assets/logos/thumbs/png/3x/@(company.LastLogo).png 3x" src="/assets/logos/thumbs/png/1x@(company.LastLogo).png" ) alt="" height="auto" width="auto" style="max-height: 32px; max-width: 128px" />
</picture>
}
@company.Name
</a>
<br />
}
</p>
}
else
{
<p>There are no companies found in the database founded in the requested country.</p>
}
</p>

View File

@@ -59,7 +59,7 @@
/assets/logos/thumbs/webp/3x/@(company.LastLogo).webp 3x"> /assets/logos/thumbs/webp/3x/@(company.LastLogo).webp 3x">
<img srcset="/assets/logos/thumbs/png/1x/@(company.LastLogo).png, <img srcset="/assets/logos/thumbs/png/1x/@(company.LastLogo).png,
/assets/logos/thumbs/png/2x/@(company.LastLogo).png 2x, /assets/logos/thumbs/png/2x/@(company.LastLogo).png 2x,
/assets/logos/thumbs/png/3x/@(company.LastLogo).png 3x" src="/assets/logos/thumbs/png/1x@(company.LastLogo).png" ) alt="" height="auto" width="auto" style="max-height: 32px; max-width: 128px" /> /assets/logos/thumbs/png/3x/@(company.LastLogo).png 3x" src="/assets/logos/thumbs/png/1x@(company.LastLogo).png" alt="" height="auto" width="auto" style="max-height: 32px; max-width: 128px" />
</picture> </picture>
} }
@company.Name @company.Name

View File

@@ -45,7 +45,7 @@
/assets/logos/webp/3x/@(Model.Company.LastLogo.Guid).webp 3x"> /assets/logos/webp/3x/@(Model.Company.LastLogo.Guid).webp 3x">
<img srcset="/assets/logos/png/1x/@(Model.Company.LastLogo.Guid).png, <img srcset="/assets/logos/png/1x/@(Model.Company.LastLogo.Guid).png,
/assets/logos/png/2x/@(Model.Company.LastLogo.Guid).png 2x, /assets/logos/png/2x/@(Model.Company.LastLogo.Guid).png 2x,
/assets/logos/png/3x/@(Model.Company.LastLogo.Guid).png 3x" src="/assets/logos/png/1x/@(Model.Company.LastLogo.Guid).png" ) alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" /> /assets/logos/png/3x/@(Model.Company.LastLogo.Guid).png 3x" src="/assets/logos/png/1x/@(Model.Company.LastLogo.Guid).png" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
</picture> </picture>
} }
</p> </p>