Use EF in company controller.

This commit is contained in:
2018-08-06 21:07:23 +01:00
parent e6378588f3
commit 728eb83d6b
14 changed files with 96 additions and 80 deletions

View File

@@ -28,21 +28,23 @@
// Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/
using cicm_web.Models;
using Cicm.Database.Schemas;
using System.Linq;
using Cicm.Database.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Company = cicm_web.Models.Company;
using Microsoft.EntityFrameworkCore;
namespace cicm_web.Controllers
{
public class CompanyController : Controller
{
readonly cicmContext _context;
readonly IHostingEnvironment hostingEnvironment;
public CompanyController(IHostingEnvironment env)
public CompanyController(IHostingEnvironment env, cicmContext context)
{
hostingEnvironment = env;
_context = context;
}
public IActionResult ByLetter(char id)
@@ -54,36 +56,40 @@ namespace cicm_web.Controllers
ViewBag.Letter = id;
Company[] companies = id == '\0' ? Company.GetAllItems() : Company.GetItemsStartingWithLetter(id);
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
return View(companies);
return View(id == '\0'
? _context.Companies.ToArray()
: _context.Companies.Where(c => c.Name.StartsWith(id)).ToArray());
}
public IActionResult View(int id)
{
CompanyWithItems company = CompanyWithItems.GetItem(id);
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
Companies company = _context.Companies.Where(c => c.Id == id).Include(c => c.Description)
.Include(c => c.Machines).Include(c => c.Country).FirstOrDefault();
if(company == null) return Index();
ViewBag.LastLogo = company.CompanyLogos.OrderByDescending(l => l.Year).FirstOrDefault();
ViewBag.CompanyLogos = company.CompanyLogos.OrderByDescending(l => l.Year).ToList();
return View(company);
}
public IActionResult ByCountry(short id)
{
Iso3166 iso3166 = Program.Database.Operations.GetIso3166(id);
ViewBag.Iso3166 = iso3166;
Company[] companies = iso3166 == null ? Company.GetAllItems() : Company.GetItemsByCountry(id);
ViewBag.Iso3166 = _context.Iso31661Numeric.FirstOrDefault(i => i.Id == id);
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
return View(companies);
return View(ViewBag.Iso3166 == null
? _context.Companies.ToArray()
: _context.Companies.Where(c => c.CountryId == id).ToArray());
}
public IActionResult Index()
{
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
return View(Company.GetAllItems());
return View(_context.Companies.ToArray());
}
}
}

View File

@@ -31,6 +31,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Cicm.Database;
using Cicm.Database.Schemas;
using Markdig;

View File

@@ -31,7 +31,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Cicm.Database.Schemas;
using Cicm.Database;
namespace cicm_web.Models
{

View File

@@ -29,16 +29,16 @@
*******************************************************************************/
using System.Collections.Generic;
using Cicm.Database.Schemas;
using Cicm.Database;
namespace cicm_web.Models
{
public class MemoryByMachine
{
public MemoryType Type;
public long Size;
public double Speed;
public MemoryType Type;
public MemoryUsage Usage;
public long Size;
public double Speed;
public static MemoryByMachine[] GetAllItems(int machineId)
{
@@ -52,10 +52,10 @@ namespace cicm_web.Models
foreach(Cicm.Database.Schemas.MemoryByMachine dbItem in dbItems)
items.Add(new MemoryByMachine
{
Type =dbItem.Type,
Usage =dbItem.Usage,
Size =dbItem.Size,
Speed =dbItem.Speed
Type = dbItem.Type,
Usage = dbItem.Usage,
Size = dbItem.Size,
Speed = dbItem.Speed
});
return items.ToArray();

View File

@@ -32,7 +32,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Cicm.Database.Schemas;
using Cicm.Database;
namespace cicm_web.Models
{

View File

@@ -32,7 +32,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Cicm.Database.Schemas;
using Cicm.Database;
namespace cicm_web.Models
{

View File

@@ -32,7 +32,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Cicm.Database.Schemas;
using Cicm.Database;
namespace cicm_web.Models
{

View File

@@ -29,7 +29,7 @@
*******************************************************************************/
using System.Collections.Generic;
using Cicm.Database.Schemas;
using Cicm.Database;
namespace cicm_web.Models
{

View File

@@ -32,9 +32,10 @@
ViewData["Title"] = "Companies";
}
@using System.IO
@model IEnumerable<Company>
@using Cicm.Database.Models
@model Cicm.Database.Models.Companies[]
<p align=center>
<p align="center">
@if(ViewBag.Iso3166 != null)
{
<b>Companies founded in @ViewBag.Iso3166.Name</b>
@@ -62,11 +63,12 @@
{
<p>
@Model.Count() companies found in the database.<br />
@foreach(Company company in Model)
@foreach(Companies company in Model)
{
<a asp-controller="Company"
asp-action="View"
asp-route-id="@company.Id">
@* TODO
@if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo.Guid + ".svg")))
{
<picture>
@@ -86,6 +88,7 @@
style="max-height: 32px; max-width: 128px" />
</picture>
}
*@
@company.Name
</a>
<br />

View File

@@ -31,11 +31,11 @@
ViewData["Title"] = "Companies";
}
@using System.IO
@model IEnumerable<Company>
@using Cicm.Database.Models
@model Cicm.Database.Models.Companies[]
<p>Search results:</p>
<p align=center>
<p align="center">
@if(ViewBag.Letter != '\0')
{
<b>@ViewBag.Letter</b>
@@ -46,11 +46,12 @@
{
<p>
@Model.Count() companies found in the database.<br />
@foreach(Company company in Model)
@foreach(Companies company in Model)
{
<a asp-controller="Company"
asp-action="View"
asp-route-id="@company.Id">
@*
@if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo.Guid + ".svg")))
{
<picture>
@@ -70,6 +71,7 @@
style="max-height: 32px; max-width: 128px" />
</picture>
}
*@
@company.Name
</a>
<br />

View File

@@ -31,19 +31,20 @@
ViewData["Title"] = "Companies";
}
@using System.IO
@model IEnumerable<Company>
@using Cicm.Database.Models
@model Cicm.Database.Models.Companies[]
<p align=center>
<p align="center">
@if(Model.Any())
{
<p>
@Model.Count() companies found in the database.<br />
@foreach(Company company in Model)
@foreach(Companies company in Model)
{
<a asp-controller="Company"
asp-action="View"
asp-route-id="@company.Id">
@* TODO
@if(company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", company.LastLogo.Guid + ".svg")))
{
<picture>
@@ -58,10 +59,13 @@
/assets/logos/thumbs/png/1x/@(company.LastLogo.Guid).webp 3x"
src="/assets/logos/thumbs/png/1x@(company.LastLogo.Guid).png")
alt=""
height="auto" width="auto" style="max-height: 32px; max-width: 128px "/>
height="auto"
width="auto"
style="max-height: 32px; max-width: 128px" />
</picture>
}
*@
@company.Name
</a>
<br />

View File

@@ -32,26 +32,27 @@
ViewData["Title"] = "Companies";
}
@using System.IO
@using Cicm.Database.Schemas
@model CompanyWithItems
@using Cicm.Database
@using Cicm.Database.Models
@model Cicm.Database.Models.Companies
@if(Model != null)
{
<div class="container-fluid">
<p align=center>
@if(Model.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", Model.LastLogo.Guid + ".svg")))
@if(ViewBag.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", ViewBag.LastLogo.Guid + ".svg")))
{
<picture>
<source type="image/svg+xml"
srcset="/assets/logos/@(Model.LastLogo.Guid).svg">
srcset="/assets/logos/@(ViewBag.LastLogo.Guid).svg">
<source type="image/webp"
srcset="/assets/logos/webp/1x/@(Model.LastLogo.Guid).webp,
/assets/logos/webp/1x/@(Model.LastLogo.Guid).webp 2x,
/assets/logos/webp/1x/@(Model.LastLogo.Guid).webp 3x">
<img srcset="/assets/logos/png/1x/@(Model.LastLogo.Guid).png,
/assets/logos/png/1x/@(Model.LastLogo.Guid).png 2x,
/assets/logos/png/1x/@(Model.LastLogo.Guid).webp 3x"
src="/assets/logos/png/1x@(Model.LastLogo.Guid).png")
srcset="/assets/logos/webp/1x/@(ViewBag.LastLogo.Guid).webp,
/assets/logos/webp/1x/@(ViewBag.LastLogo.Guid).webp 2x,
/assets/logos/webp/1x/@(ViewBag.LastLogo.Guid).webp 3x">
<img srcset="/assets/logos/png/1x/@(ViewBag.LastLogo.Guid).png,
/assets/logos/png/1x/@(ViewBag.LastLogo.Guid).png 2x,
/assets/logos/png/1x/@(ViewBag.LastLogo.Guid).webp 3x"
src="/assets/logos/png/1x@(ViewBag.LastLogo.Guid).png")
alt=""
height="auto"
width="auto"
@@ -63,7 +64,7 @@
@{
string carrouselActive = "active";
}
@if(Model.Logos != null && Model.Logos.Length > 1)
@if(ViewBag.Logos != null && ViewBag.Logos.Length > 1)
{
<div class="col-3">
<div class="carousel slide"
@@ -72,7 +73,7 @@
<div class="carousel-inner">
@foreach(CompanyLogo logo in Model.Logos)
@foreach(CompanyLogos logo in ViewBag.Logos)
{
if(File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", logo.Guid + ".svg")))
{
@@ -129,11 +130,11 @@
<b>@Model.Name</b>
</th>
</tr>
@if(Model.Founded > DateTime.MinValue)
@if(Model.Founded.HasValue)
{
<tr>
<th>Founded</th>
<td>@Model.Founded.ToLongDateString().</td>
<td>@Model.Founded.Value.ToLongDateString().</td>
</tr>
}
<tr>
@@ -183,12 +184,12 @@
<a asp-controller="Company"
asp-action="View"
asp-route-id="@Model.SoldTo.Id">
@Model.SoldTo.Name</a> on @Model.Sold.ToLongDateString().
@Model.SoldTo.Name</a> on @Model.Sold.Value.ToLongDateString().
</td>
}
else
{
<td>Company was sold on @Model.Sold.ToLongDateString() to an unknown company.</td>
<td>Company was sold on @Model.Sold.Value.ToLongDateString() to an unknown company.</td>
}
}
else
@@ -215,7 +216,7 @@
if(Model.SoldTo != null)
{
<td>
Company was merged on @Model.Sold.ToLongDateString() to form
Company was merged on @Model.Sold.Value.ToLongDateString() to form
<a asp-controller="Company"
asp-action="View"
asp-route-id="@Model.SoldTo.Id">
@@ -224,7 +225,7 @@
}
else
{
<td>Company was merge on @Model.Sold.ToLongDateString() to form an unknown company.</td>
<td>Company was merge on @Model.Sold.Value.ToLongDateString() to form an unknown company.</td>
}
}
else
@@ -248,7 +249,7 @@
case CompanyStatus.Bankrupt:
if(Model.Sold != DateTime.MinValue)
{
<td>Company declared bankruptcy on @Model.Sold.ToLongDateString().</td>
<td>Company declared bankruptcy on @Model.Sold.Value.ToLongDateString().</td>
}
else
{
@@ -258,7 +259,7 @@
case CompanyStatus.Defunct:
if(Model.Sold != DateTime.MinValue)
{
<td>Company ceased operations on @Model.Sold.ToLongDateString().</td>
<td>Company ceased operations on @Model.Sold.Value.ToLongDateString().</td>
}
else
{
@@ -275,12 +276,12 @@
<a asp-controller="Company"
asp-action="View"
asp-route-id="@Model.SoldTo.Id">
@Model.SoldTo.Name</a> on @Model.Sold.ToLongDateString().
@Model.SoldTo.Name</a> on @Model.Sold.Value.ToLongDateString().
</td>
}
else
{
<td>Company was renamed on @Model.Sold.ToLongDateString() to an unknown name.</td>
<td>Company was renamed on @Model.Sold.Value.ToLongDateString() to an unknown name.</td>
}
}
else
@@ -345,7 +346,7 @@
<div class="row"
id="itemsAccordion">
<div class="card">
@if(Model.Computers.Any())
@if(Model.Machines.Any(t => t.Type == MachineType.Computer))
{
<div class="card-header"
id="headingComputers">
@@ -355,7 +356,7 @@
class="btn btn-info"
data-target="#collapseComputers"
data-toggle="collapse">
<span class="badge badge-success">@Model.Computers.Count()</span> computers known.
<span class="badge badge-success">@Model.Machines.Count(t => t.Type == MachineType.Computer)</span> computers known.
</button>
</h5>
@@ -365,7 +366,7 @@
data-parent="#itemsAccordion"
id="collapseComputers">
<div class="card-body">
@foreach(MachineMini computer in Model.Computers)
@foreach(Machines computer in Model.Machines.Where(t => t.Type == MachineType.Computer))
{
<a asp-controller="Machine"
asp-action="View"
@@ -387,7 +388,7 @@
</h5>
</div>
}
@if(Model.Consoles.Any())
@if(Model.Machines.Any(t => t.Type == MachineType.Console))
{
<div class="card-header"
id="headingConsoles">
@@ -397,7 +398,7 @@
class="btn btn-info"
data-target="#collapseConsoles"
data-toggle="collapse">
<span class="badge badge-success">@Model.Consoles.Count()</span> videogame consoles known.
<span class="badge badge-success">@Model.Machines.Count(t => t.Type == MachineType.Console)</span> videogame consoles known.
</button>
</h5>
@@ -407,7 +408,7 @@
data-parent="#itemsAccordion"
id="collapseConsoles">
<div class="card-body">
@foreach(MachineMini console in Model.Consoles)
@foreach(Machines console in Model.Machines.Where(t => t.Type == MachineType.Console))
{
<a asp-controller="Machine"
asp-action="View"
@@ -432,6 +433,7 @@
</div>
</div>
@* TODO: This is not working with EF, check why *@
@if(Model.Description != null)
{
<div class="container-fluid row">

View File

@@ -32,12 +32,10 @@
ViewData["Title"] = "Computer";
}
@using System.IO
@using Cicm.Database.Schemas
@using MemoryByMachine = cicm_web.Models.MemoryByMachine
@using StorageByMachine = cicm_web.Models.StorageByMachine
@model cicm_web.Models.Machine
@using Cicm.Database
@model Machine
<p align=center>
<p align="center">
@if(Model.Company.LastLogo != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, "assets/logos", Model.Company.LastLogo.Guid + ".svg")))
{
<picture>
@@ -67,11 +65,11 @@
}
<b>
<a asp-controller=Company
asp-action=View
asp-route-id=@Model.Company.Id>
<a asp-controller="Company"
asp-action="View"
asp-route-id="@Model.Company.Id">
@Model.Company.Name</a> @Model.Name</b>
<table width=100%>
<table width="100%">
@if(Model.Introduced.Year != 1000)
{

View File

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