diff --git a/cicm_web/Controllers/CompanyController.cs b/cicm_web/Controllers/CompanyController.cs new file mode 100644 index 00000000..a188d908 --- /dev/null +++ b/cicm_web/Controllers/CompanyController.cs @@ -0,0 +1,72 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : CompanyController.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Company 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-2018 Natalia Portillo +*******************************************************************************/ + +using cicm_web.Models; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; + +namespace cicm_web.Controllers +{ + public class CompanyController : Controller + { + readonly IHostingEnvironment hostingEnvironment; + + public CompanyController(IHostingEnvironment env) + { + hostingEnvironment = env; + } + + 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; + + Company[] companies = + id == '\0' ? Company.GetAllItems() : Company.GetItemsStartingWithLetter(id); + + ViewBag.WebRootPath = hostingEnvironment.WebRootPath; + return View(companies); + } + + public IActionResult View(int id) + { + Company company = Company.GetItem(id); + ViewBag.Company = company; + Computer[] computers = Computer.GetItemsFromCompany(id); + + ViewBag.WebRootPath = hostingEnvironment.WebRootPath; + return View(computers); + } + } +} \ No newline at end of file diff --git a/cicm_web/Controllers/ComputerController.cs b/cicm_web/Controllers/ComputerController.cs index d4bf5e05..4657d80d 100644 --- a/cicm_web/Controllers/ComputerController.cs +++ b/cicm_web/Controllers/ComputerController.cs @@ -1,4 +1,34 @@ -using System.Collections.Generic; +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// 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-2018 Natalia Portillo +*******************************************************************************/ + +using System.Collections.Generic; using System.Linq; using cicm_web.Models; using Microsoft.AspNetCore.Hosting; diff --git a/cicm_web/Models/Company.cs b/cicm_web/Models/Company.cs index 3840f551..ce69e7a3 100644 --- a/cicm_web/Models/Company.cs +++ b/cicm_web/Models/Company.cs @@ -28,7 +28,9 @@ // Copyright © 2003-2018 Natalia Portillo *******************************************************************************/ +using System; using System.Collections.Generic; +using System.Linq; namespace cicm_web.Models { @@ -49,12 +51,18 @@ namespace cicm_web.Models bool? result = Program.Database?.Operations.GetCompanies(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - List items = new List(); + return dbItems.Select(t => new Company {Id = t.Id, Name = t.Name}).OrderBy(t => t.Name).ToArray(); + } - foreach(Cicm.Database.Schemas.Company dbItem in dbItems) - items.Add(new Company {Id = dbItem.Id, Name = dbItem.Name}); + public static Company[] GetItemsStartingWithLetter(char letter) + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetCompanies(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; - return items.ToArray(); + return dbItems + .Where(t => t.Name.StartsWith(new string(letter, 1), StringComparison.InvariantCultureIgnoreCase)) + .Select(t => new Company {Id = t.Id, Name = t.Name}).OrderBy(t => t.Name).ToArray(); } } } \ No newline at end of file diff --git a/cicm_web/Models/Computer.cs b/cicm_web/Models/Computer.cs index 8cc8e569..75f11c89 100644 --- a/cicm_web/Models/Computer.cs +++ b/cicm_web/Models/Computer.cs @@ -70,16 +70,19 @@ namespace cicm_web.Models bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - return dbItems.Select(TransformItem) as Computer[]; + List items = new List(); + + return dbItems.Select(TransformItem).OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray(); } public static Computer[] GetItemsFromCompany(int id) { List dbItems = null; - bool? result = Program.Database?.Operations.GetComputers(out dbItems, id); + bool? result = Program.Database?.Operations.GetComputers(out dbItems); if(result == null || result.Value == false || dbItems == null) return null; - return dbItems.Select(TransformItem) as Computer[]; + // TODO: Company chosen by DB + return dbItems.Where(t => t.Company == id).Select(TransformItem).OrderBy(t => t.Model).ToArray(); } public static Computer GetItem(int id) diff --git a/cicm_web/Views/Company/ByLetter.cshtml b/cicm_web/Views/Company/ByLetter.cshtml new file mode 100644 index 00000000..06995ef4 --- /dev/null +++ b/cicm_web/Views/Company/ByLetter.cshtml @@ -0,0 +1,81 @@ +@{ + /****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : ByLetter.cshtml +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Lists companies 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"] = "Companies"; +} +@using System.IO +@model IEnumerable + +

Search results:

+

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

+ @Model.Count() companies found in the database.
+ @foreach(Company company in Model) + { + if(File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos/computers", company.Id + ".gif"))) + { + + } + + if(File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos/computers", company.Id + ".jpg"))) + { + + } + + if(File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos/computers", company.Id + ".png"))) + { + + } + + + @company.Name +
+ } +

+ } + else + { +

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

+ } +

\ No newline at end of file diff --git a/cicm_web/Views/Company/View.cshtml b/cicm_web/Views/Company/View.cshtml new file mode 100644 index 00000000..9510336f --- /dev/null +++ b/cicm_web/Views/Company/View.cshtml @@ -0,0 +1,80 @@ +@{ + /****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : ByLetter.cshtml +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Lists computers by company +// +// --[ 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"] = "Companies"; +} +@using System.IO +@model IEnumerable + +

Search results:

+

+ @if(ViewBag.Company != null) + { + if(File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos/computers", ViewBag.Company.Id + ".gif"))) + { + + } + + if(File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos/computers", ViewBag.Company.Id + ".jpg"))) + { + + } + + if(File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos/computers", ViewBag.Company.Id + ".png"))) + { + + } + @ViewBag.Company.Name +
+ } + + @if(Model.Any()) + { +

+ @Model.Count() computers found in the database.
+ @foreach(Computer computer in Model) + { + + @computer.Model +
+ } +

+ } + else + { +

There are no computers found in the database that belong to that company.

+ } +

\ No newline at end of file diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj index 026bbcf7..564cd6d3 100644 --- a/cicm_web/cicm_web.csproj +++ b/cicm_web/cicm_web.csproj @@ -2,7 +2,7 @@ netcoreapp2.0 - 3.0.99.69 + 3.0.99.84 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website