Add search computer by letter.

This commit is contained in:
2018-04-14 05:40:28 +01:00
parent d043ef3333
commit 113cebe789
4 changed files with 118 additions and 4 deletions

View File

@@ -1,7 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Cicm.Database.Schemas; using cicm_web.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Computer = Cicm.Database.Schemas.Computer;
namespace cicm_web.Controllers namespace cicm_web.Controllers
{ {
@@ -18,5 +19,20 @@ namespace cicm_web.Controllers
return View(); return View();
} }
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;
ComputerMini[] computers =
id == '\0' ? ComputerMini.GetAllItems() : ComputerMini.GetItemsStartingWithLetter(id);
return View(computers);
}
} }
} }

View File

@@ -28,6 +28,7 @@
// Copyright © 2003-2018 Natalia Portillo // Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/ *******************************************************************************/
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -69,7 +70,7 @@ namespace cicm_web.Models
bool? result = Program.Database?.Operations.GetComputers(out dbItems); bool? result = Program.Database?.Operations.GetComputers(out dbItems);
if(result == null || result.Value == false || dbItems == null) return null; if(result == null || result.Value == false || dbItems == null) return null;
return dbItems.OrderByDescending(i => i.Id).Select(TransformItem) as Computer[]; return dbItems.Select(TransformItem) as Computer[];
} }
public static Computer[] GetItemsFromCompany(int id) public static Computer[] GetItemsFromCompany(int id)
@@ -78,7 +79,7 @@ namespace cicm_web.Models
bool? result = Program.Database?.Operations.GetComputers(out dbItems, id); bool? result = Program.Database?.Operations.GetComputers(out dbItems, id);
if(result == null || result.Value == false || dbItems == null) return null; if(result == null || result.Value == false || dbItems == null) return null;
return dbItems.OrderByDescending(i => i.Id).Select(TransformItem) as Computer[]; return dbItems.Select(TransformItem) as Computer[];
} }
public static Computer GetItem(int id) public static Computer GetItem(int id)
@@ -147,4 +148,44 @@ namespace cicm_web.Models
return item; return item;
} }
} }
public class ComputerMini
{
public Company Company;
public int Id;
public string Model;
public static ComputerMini[] GetAllItems()
{
List<Cicm.Database.Schemas.Computer> dbItems = null;
bool? result = Program.Database?.Operations.GetComputers(out dbItems);
if(result == null || result.Value == false || dbItems == null) return null;
List<ComputerMini> items = new List<ComputerMini>();
foreach(Cicm.Database.Schemas.Computer dbItem in dbItems) items.Add(TransformItem(dbItem));
return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray();
}
public static ComputerMini[] GetItemsStartingWithLetter(char letter)
{
List<Cicm.Database.Schemas.Computer> dbItems = null;
bool? result = Program.Database?.Operations.GetComputers(out dbItems);
if(result == null || result.Value == false || dbItems == null) return null;
List<ComputerMini> items = new List<ComputerMini>();
foreach(Cicm.Database.Schemas.Computer dbItem in dbItems)
if(dbItem.Model.StartsWith(new string(letter, 1), StringComparison.InvariantCultureIgnoreCase))
items.Add(TransformItem(dbItem));
return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray();
}
static ComputerMini TransformItem(Cicm.Database.Schemas.Computer dbItem)
{
System.Console.WriteLine("Transforming item");
return new ComputerMini {Company = Company.GetItem(dbItem.Company), Id = dbItem.Id, Model = dbItem.Model};
}
}
} }

View File

@@ -0,0 +1,57 @@
@{
/******************************************************************************
// Canary Islands Computer Museum Website
// ----------------------------------------------------------------------------
//
// 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-2018 Natalia Portillo
*******************************************************************************/
ViewData["Title"] = "Computers";
}
@model IEnumerable<ComputerMini>
<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(ComputerMini computer in @Model)
{
<a asp-controller="Computer" asp-action="View" asp-route-id="@computer.Id">@computer.Company.Name @computer.Model</a><br/>
}
</p>
}
else
{
<p>There are no computers found in the database that start with this letter.</p>
}
</p>

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<Version>3.0.99.54</Version> <Version>3.0.99.67</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>