diff --git a/cicm_web/Controllers/CompanyController.cs b/cicm_web/Controllers/CompanyController.cs index b8ee140b..872c3ba2 100644 --- a/cicm_web/Controllers/CompanyController.cs +++ b/cicm_web/Controllers/CompanyController.cs @@ -30,8 +30,10 @@ using System.Linq; using Cicm.Database.Models; +using cicm_web.Models; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace cicm_web.Controllers { @@ -56,9 +58,16 @@ namespace cicm_web.Controllers ViewBag.Letter = id; ViewBag.WebRootPath = hostingEnvironment.WebRootPath; - return View(id == '\0' - ? _context.Companies.ToArray() - : _context.Companies.Where(c => c.Name.StartsWith(id)).ToArray()); + + if(id == '\0') return RedirectToAction(nameof(Index)); + + return View(_context.Companies.Include(c => c.Logos).Where(c => c.Name.StartsWith(id)).OrderBy(c => c.Name) + .Select(c => new CompanyViewModel + { + Id = c.Id, + LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid, + Name = c.Name + }).ToList()); } public IActionResult View(int id) @@ -77,16 +86,33 @@ namespace cicm_web.Controllers { ViewBag.Iso3166 = _context.Iso31661Numeric.FirstOrDefault(i => i.Id == id); + if(ViewBag.Iso3166 is null) RedirectToAction(nameof(Index)); + ViewBag.WebRootPath = hostingEnvironment.WebRootPath; - return View(ViewBag.Iso3166 == null - ? _context.Companies.ToArray() - : _context.Companies.Where(c => c.CountryId == id).ToArray()); + return View(_context.Companies.Include(c => c.Logos).Where(c => c.CountryId == id).OrderBy(c => c.Name) + .Select(c => new CompanyViewModel + { + Id = c.Id, + LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid, + Name = c.Name + }).ToList()); } public IActionResult Index() { ViewBag.WebRootPath = hostingEnvironment.WebRootPath; - return View(_context.Companies.ToArray()); + + return View(_context.Companies.Include(c => c.Logos).OrderBy(c => c.Name).Select(c => new CompanyViewModel + { + Id = c.Id, + LastLogo = c + .Logos + .OrderByDescending(l => + l.Year) + .FirstOrDefault() + .Guid, + Name = c.Name + }).ToList()); } } } \ No newline at end of file diff --git a/cicm_web/Models/CompanyViewModel.cs b/cicm_web/Models/CompanyViewModel.cs new file mode 100644 index 00000000..92bbd674 --- /dev/null +++ b/cicm_web/Models/CompanyViewModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace cicm_web.Models +{ + public class CompanyViewModel + { + public int Id; + public Guid? LastLogo; + public string Name; + } +} \ No newline at end of file diff --git a/cicm_web/Views/Company/ByCountry.cshtml b/cicm_web/Views/Company/ByCountry.cshtml index 1ed37e58..89a1e3e4 100644 --- a/cicm_web/Views/Company/ByCountry.cshtml +++ b/cicm_web/Views/Company/ByCountry.cshtml @@ -32,8 +32,7 @@ ViewData["Title"] = "Companies"; } @using System.IO -@using Cicm.Database.Models -@model Cicm.Database.Models.Company[] +@model IEnumerable

@if(ViewBag.Iso3166 != null) @@ -63,24 +62,24 @@ {

@Model.Count() companies found in the database.
- @foreach(Company company in Model) + @foreach(CompanyViewModel company in Model) { - @if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo.Guid + ".svg"))) + @if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo + ".svg"))) { + srcset="/assets/logos/@(company.LastLogo).svg"> - +

Search results:

@@ -47,24 +46,24 @@ {

@Model.Count() companies found in the database.
- @foreach(Company company in Model) + @foreach(CompanyViewModel company in Model) {
- @if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo.Guid + ".svg"))) + @if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo + ".svg"))) { + srcset="/assets/logos/@(company.LastLogo).svg"> - +

@if(Model.Any()) {

@Model.Count() companies found in the database.
- @foreach(Company company in Model) + @foreach(CompanyViewModel company in Model) {
- @if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo.Guid + ".svg"))) + @if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo + ".svg"))) { + srcset="/assets/logos/@(company.LastLogo).svg"> - + netcoreapp2.2 - 3.0.99.550 + 3.0.99.557 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website