From 113cebe789e71986fe48d2b0546c18229b7eb83c Mon Sep 17 00:00:00 2001
From: Natalia Portillo
Date: Sat, 14 Apr 2018 05:40:28 +0100
Subject: [PATCH] Add search computer by letter.
---
cicm_web/Controllers/ComputerController.cs | 18 ++++++-
cicm_web/Models/Computer.cs | 45 ++++++++++++++++-
cicm_web/Views/Computer/ByLetter.cshtml | 57 ++++++++++++++++++++++
cicm_web/cicm_web.csproj | 2 +-
4 files changed, 118 insertions(+), 4 deletions(-)
create mode 100644 cicm_web/Views/Computer/ByLetter.cshtml
diff --git a/cicm_web/Controllers/ComputerController.cs b/cicm_web/Controllers/ComputerController.cs
index 39d8641c..5ea14579 100644
--- a/cicm_web/Controllers/ComputerController.cs
+++ b/cicm_web/Controllers/ComputerController.cs
@@ -1,7 +1,8 @@
using System.Collections.Generic;
using System.Linq;
-using Cicm.Database.Schemas;
+using cicm_web.Models;
using Microsoft.AspNetCore.Mvc;
+using Computer = Cicm.Database.Schemas.Computer;
namespace cicm_web.Controllers
{
@@ -18,5 +19,20 @@ namespace cicm_web.Controllers
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);
+ }
}
}
\ No newline at end of file
diff --git a/cicm_web/Models/Computer.cs b/cicm_web/Models/Computer.cs
index a21d41ae..64069aeb 100644
--- a/cicm_web/Models/Computer.cs
+++ b/cicm_web/Models/Computer.cs
@@ -28,6 +28,7 @@
// Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/
+using System;
using System.Collections.Generic;
using System.Linq;
@@ -69,7 +70,7 @@ namespace cicm_web.Models
bool? result = Program.Database?.Operations.GetComputers(out dbItems);
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)
@@ -78,7 +79,7 @@ namespace cicm_web.Models
bool? result = Program.Database?.Operations.GetComputers(out dbItems, id);
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)
@@ -147,4 +148,44 @@ namespace cicm_web.Models
return item;
}
}
+
+ public class ComputerMini
+ {
+ public Company Company;
+ public int Id;
+ public string Model;
+
+ public static ComputerMini[] GetAllItems()
+ {
+ List dbItems = null;
+ bool? result = Program.Database?.Operations.GetComputers(out dbItems);
+
+ if(result == null || result.Value == false || dbItems == null) return null;
+
+ List items = new List();
+ 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 dbItems = null;
+ bool? result = Program.Database?.Operations.GetComputers(out dbItems);
+ if(result == null || result.Value == false || dbItems == null) return null;
+
+ List items = new List();
+ 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};
+ }
+ }
}
\ No newline at end of file
diff --git a/cicm_web/Views/Computer/ByLetter.cshtml b/cicm_web/Views/Computer/ByLetter.cshtml
new file mode 100644
index 00000000..c6c8d10b
--- /dev/null
+++ b/cicm_web/Views/Computer/ByLetter.cshtml
@@ -0,0 +1,57 @@
+@{
+/******************************************************************************
+// Canary Islands Computer Museum Website
+// ----------------------------------------------------------------------------
+//
+// 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-2018 Natalia Portillo
+*******************************************************************************/
+
+ ViewData["Title"] = "Computers";
+}
+
+@model IEnumerable
+
+Search results:
+
+ @if(ViewBag.Letter != '\0')
+ {
+ @ViewBag.Letter
+ }
+
+ @if(Model.Any())
+ {
+
@Model.Count() computers found in the database.
+ @foreach(ComputerMini computer in @Model)
+ {
+ @computer.Company.Name @computer.Model
+ }
+
+ }
+ else
+ {
+ There are no computers found in the database that start with this letter.
+ }
+
diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj
index 6a00088b..e035936c 100644
--- a/cicm_web/cicm_web.csproj
+++ b/cicm_web/cicm_web.csproj
@@ -2,7 +2,7 @@
netcoreapp2.0
- 3.0.99.54
+ 3.0.99.67
Canary Islands Computer Museum
Copyright © 2003-2018 Natalia Portillo
Canary Islands Computer Museum Website