diff --git a/Marechai/Controllers/ComputerController.cs b/Marechai/Controllers/ComputerController.cs deleted file mode 100644 index 29a23638..00000000 --- a/Marechai/Controllers/ComputerController.cs +++ /dev/null @@ -1,85 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : ComputerController.cs -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// 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()); - } - } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 72912381..9ad3b898 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.964 + 3.0.99.965 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Pages/Computers/Index.razor b/Marechai/Pages/Computers/Index.razor index ef1e7927..faf82056 100644 --- a/Marechai/Pages/Computers/Index.razor +++ b/Marechai/Pages/Computers/Index.razor @@ -220,7 +220,7 @@ M
- + @L["All computers"]

diff --git a/Marechai/Pages/Computers/Search.razor b/Marechai/Pages/Computers/Search.razor new file mode 100644 index 00000000..aea4f955 --- /dev/null +++ b/Marechai/Pages/Computers/Search.razor @@ -0,0 +1,124 @@ +@{ + /****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Filename : Search.razor +// Author(s) : Natalia Portillo +// +// --[ 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 . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2020 Natalia Portillo +*******************************************************************************/ +} + +@page "/computers/all" +@page "/computers/year/{Year:int}" +@page "/computers/{StartingCharacter}" +@inherits OwningComponentBase +@inject IStringLocalizer L + +@if (_computers is null) +{ + @L["Loading..."] +} + +

@L["Search results:"]

+

+ @if (_character.HasValue) + { + @string.Format(L["Computers starting with {0}"], _character) +
+ } + else if (Year.HasValue) + { + @string.Format(L["Computers introduced in {0}"], Year) +
+ } + + @if (_computers?.Count > 0) + { +

+ @string.Format(L["{0} computers found in the database."], _computers.Count) +
+ @foreach (var computer in _computers) + { + + @computer.CompanyName @computer.Name +
+ } +

+ } + else + { + @if (_character.HasValue) + { +

@L["There are no computers found in the database that start with this letter."]

+ } + else if (Year.HasValue) + { +

@L["There are no computers found introduced this year."]

+ } + } +

+ +@code +{ + [Parameter] + public int? Year { get; set; } + + [Parameter] + public string StartingCharacter { get; set; } + + List _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(); + } +} \ No newline at end of file diff --git a/Marechai/Resources/Services/ComputersService.es.resx b/Marechai/Resources/Services/ComputersService.es.resx index 78eb6dd2..a6f17039 100644 --- a/Marechai/Resources/Services/ComputersService.es.resx +++ b/Marechai/Resources/Services/ComputersService.es.resx @@ -154,4 +154,28 @@ Búsqueda por año Search by year + + Resultados: + Search results + + + Ordenadores cuyo nombre comienza por {0} + Computers starting with {0}, a letter + + + Ordenadores introducidos en {0} + Computers introduced in, {0} is a year + + + {0} ordenadores en la base de datos + Computers found by search, {0} is number + + + No se encontraron ordenadores en la base de datos cuyo nombre comience por esa letra. + No computers in DB with that letter + + + No se encontraron ordenadores en la base de datos introducidos en ese año. + No computers in DB from that year + \ No newline at end of file diff --git a/Marechai/Services/ComputersService.cs b/Marechai/Services/ComputersService.cs index 4cb2b8e4..0bb02005 100644 --- a/Marechai/Services/ComputersService.cs +++ b/Marechai/Services/ComputersService.cs @@ -28,10 +28,12 @@ // Copyright © 2003-2020 Natalia Portillo *******************************************************************************/ +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Marechai.Database; using Marechai.Database.Models; +using Marechai.ViewModels; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Localization; @@ -62,5 +64,29 @@ namespace Marechai.Services t.Introduced.HasValue && t.Introduced.Value.Year > 1000). MaxAsync(t => t.Introduced.Value.Year); + + public async Task> 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> 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> 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(); } } \ No newline at end of file diff --git a/Marechai/ViewModels/MachineViewModel.cs b/Marechai/ViewModels/MachineViewModel.cs new file mode 100644 index 00000000..5d50e7c7 --- /dev/null +++ b/Marechai/ViewModels/MachineViewModel.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Marechai/Views/Computer/ByLetter.cshtml b/Marechai/Views/Computer/ByLetter.cshtml deleted file mode 100644 index 805db1c9..00000000 --- a/Marechai/Views/Computer/ByLetter.cshtml +++ /dev/null @@ -1,60 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : ByLetter.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ - - ViewData["Title"] = "Computers"; -} -@model Marechai.Database.Models.Machine[] -

Search results:

-

- @if (ViewBag.Letter != '\0') - { - @ViewBag.Letter -
- } - - @if (Model.Any()) - { -

- @Model.Count() computers found in the database. -
- @foreach (var computer in Model) - { - - @computer.Company.Name @computer.Name -
- } -

- } - else - { -

There are no computers found in the database that start with this letter.

- } -

\ No newline at end of file diff --git a/Marechai/Views/Computer/ByYear.cshtml b/Marechai/Views/Computer/ByYear.cshtml deleted file mode 100644 index 32981c2d..00000000 --- a/Marechai/Views/Computer/ByYear.cshtml +++ /dev/null @@ -1,56 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : ByYear.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ - - ViewData["Title"] = "Computers"; -} -@model Marechai.Database.Models.Machine[] -

Search results:

-

- @ViewBag.Year -
- @if (Model.Any()) - { -

- @Model.Count() computers found in the database. -
- @foreach (var computer in Model) - { - - @computer.Company.Name @computer.Name -
- } -

- } - else - { -

There are no computers found in the database released this year.

- } -

\ No newline at end of file