Move computers search to Blazor.

This commit is contained in:
2020-05-22 02:44:24 +01:00
parent 2e9fe91777
commit ef99e0c8e9
9 changed files with 185 additions and 203 deletions

View File

@@ -1,85 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : ComputerController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Computer 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 Marechai.Database;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
namespace Marechai.Controllers
{
public class ComputerController : Controller
{
readonly MarechaiContext _context;
readonly IWebHostEnvironment hostingEnvironment;
public ComputerController(IWebHostEnvironment env, MarechaiContext context)
{
hostingEnvironment = env;
_context = context;
}
public IActionResult ByLetter(char id)
{
// ToUpper()
if(id >= 'a' &&
id <= 'z')
id -= (char)32;
// Check if not letter
if(id < 'A' ||
id > 'Z')
id = '\0';
ViewBag.Letter = id;
return View(id == '\0' ? _context.Machines.Where(m => m.Type == MachineType.Computer).ToArray() : _context.
Machines.
Where(m =>
m.
Type ==
MachineType.
Computer &&
m.
Name.
StartsWith(id)).
ToArray());
}
public IActionResult ByYear(int id)
{
ViewBag.Year = id;
return View(_context.Machines.Where(t => t.Type == MachineType.Computer && t.Introduced.HasValue &&
t.Introduced.Value.Year == id).ToArray());
}
}
}

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.964</Version> <Version>3.0.99.965</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

@@ -220,7 +220,7 @@
M M
</a> </a>
<br> <br>
<a href="/computers"> <a href="/computers/all">
@L["All computers"] @L["All computers"]
</a> </a>
</p> </p>

View File

@@ -0,0 +1,124 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Search.razor
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Computers search
//
// --[ 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
*******************************************************************************/
}
@page "/computers/all"
@page "/computers/year/{Year:int}"
@page "/computers/{StartingCharacter}"
@inherits OwningComponentBase<ComputersService>
@inject IStringLocalizer<ComputersService> L
@if (_computers is null)
{
@L["Loading..."]
}
<p>@L["Search results:"]</p>
<p align="center">
@if (_character.HasValue)
{
<b>@string.Format(L["Computers starting with {0}"], _character)</b>
<br />
}
else if (Year.HasValue)
{
<b>@string.Format(L["Computers introduced in {0}"], Year)</b>
<br />
}
@if (_computers?.Count > 0)
{
<p>
@string.Format(L["{0} computers found in the database."], _computers.Count)
<br />
@foreach (var computer in _computers)
{
<a href="/machine/@computer.Id">
@computer.CompanyName @computer.Name</a>
<br />
}
</p>
}
else
{
@if (_character.HasValue)
{
<p>@L["There are no computers found in the database that start with this letter."]</p>
}
else if (Year.HasValue)
{
<p>@L["There are no computers found introduced this year."]</p>
}
}
</p>
@code
{
[Parameter]
public int? Year { get; set; }
[Parameter]
public string StartingCharacter { get; set; }
List<MachineViewModel> _computers;
char? _character;
protected override async Task OnInitializedAsync()
{
_character = null;
if (!string.IsNullOrWhiteSpace(StartingCharacter) &&
StartingCharacter.Length == 1)
{
_character = StartingCharacter[0];
// ToUpper()
if (_character >= 'a' &&
_character <= 'z')
_character -= (char)32;
// Check if not letter or number
if (_character < '0' ||
(_character > '9' && _character < 'A') ||
_character > 'Z')
_character = null;
}
if (_character.HasValue)
_computers = await Service.GetComputersByLetterAsync(_character.Value);
if (Year.HasValue &&
_computers is null)
_computers = await Service.GetComputersByYearAsync(Year.Value);
_computers ??= await Service.GetComputersAsync();
}
}

View File

@@ -154,4 +154,28 @@
<value>Búsqueda por año</value> <value>Búsqueda por año</value>
<comment>Search by year</comment> <comment>Search by year</comment>
</data> </data>
<data name="Search results:" xml:space="preserve">
<value>Resultados:</value>
<comment>Search results</comment>
</data>
<data name="Computers starting with {0}" xml:space="preserve">
<value>Ordenadores cuyo nombre comienza por {0}</value>
<comment>Computers starting with {0}, a letter</comment>
</data>
<data name="Computers introduced in {0}" xml:space="preserve">
<value>Ordenadores introducidos en {0}</value>
<comment>Computers introduced in, {0} is a year</comment>
</data>
<data name="{0} computers found in the database." xml:space="preserve">
<value>{0} ordenadores en la base de datos</value>
<comment>Computers found by search, {0} is number</comment>
</data>
<data name="There are no computers found in the database that start with this letter." xml:space="preserve">
<value>No se encontraron ordenadores en la base de datos cuyo nombre comience por esa letra.</value>
<comment>No computers in DB with that letter</comment>
</data>
<data name="There are no computers found introduced this year." xml:space="preserve">
<value>No se encontraron ordenadores en la base de datos introducidos en ese año.</value>
<comment>No computers in DB from that year</comment>
</data>
</root> </root>

View File

@@ -28,10 +28,12 @@
// Copyright © 2003-2020 Natalia Portillo // Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/ *******************************************************************************/
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Marechai.Database; using Marechai.Database;
using Marechai.Database.Models; using Marechai.Database.Models;
using Marechai.ViewModels;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
@@ -62,5 +64,29 @@ namespace Marechai.Services
t.Introduced.HasValue && t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000). t.Introduced.Value.Year > 1000).
MaxAsync(t => t.Introduced.Value.Year); MaxAsync(t => t.Introduced.Value.Year);
public async Task<List<MachineViewModel>> GetComputersByLetterAsync(char c) =>
await _context.Machines.Include(m => m.Company).
Where(m => m.Type == MachineType.Computer && EF.Functions.Like(m.Name, $"{c}%")).
OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new MachineViewModel
{
Id = m.Id, Name = m.Name, CompanyName = m.Company.Name
}).ToListAsync();
public async Task<List<MachineViewModel>> GetComputersByYearAsync(int year) =>
await _context.Machines.Include(m => m.Company).
Where(m => m.Type == MachineType.Computer && m.Introduced != null &&
m.Introduced.Value.Year == year).OrderBy(m => m.Company.Name).ThenBy(m => m.Name).
Select(m => new MachineViewModel
{
Id = m.Id, Name = m.Name, CompanyName = m.Company.Name
}).ToListAsync();
public async Task<List<MachineViewModel>> GetComputersAsync() =>
await _context.Machines.Include(m => m.Company).Where(m => m.Type == MachineType.Computer).
OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new MachineViewModel
{
Id = m.Id, Name = m.Name, CompanyName = m.Company.Name
}).ToListAsync();
} }
} }

View File

@@ -0,0 +1,9 @@
namespace Marechai.ViewModels
{
public class MachineViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string CompanyName { get; set; }
}
}

View File

@@ -1,60 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : ByLetter.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Lists computers by letter (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"] = "Computers";
}
@model Marechai.Database.Models.Machine[]
<p>Search results:</p>
<p align="center">
@if (ViewBag.Letter != '\0')
{
<b>@ViewBag.Letter</b>
<br />
}
@if (Model.Any())
{
<p>
@Model.Count() computers found in the database.
<br />
@foreach (var computer in Model)
{
<a asp-controller="Machine" asp-action="View" asp-route-id="@computer.Id">
@computer.Company.Name @computer.Name</a>
<br />
}
</p>
}
else
{
<p>There are no computers found in the database that start with this letter.</p>
}
</p>

View File

@@ -1,56 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : ByYear.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Lists computers by letter (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"] = "Computers";
}
@model Marechai.Database.Models.Machine[]
<p>Search results:</p>
<p align="center">
<b>@ViewBag.Year</b>
<br />
@if (Model.Any())
{
<p>
@Model.Count() computers found in the database.
<br />
@foreach (var computer in Model)
{
<a asp-controller="Machine" asp-action="View" asp-route-id="@computer.Id">
@computer.Company.Name @computer.Name</a>
<br />
}
</p>
}
else
{
<p>There are no computers found in the database released this year.</p>
}
</p>