Use Entity Framework in computer and console controllers.

This commit is contained in:
2018-08-06 22:27:55 +01:00
parent 07d423f830
commit de37bc02ff
7 changed files with 63 additions and 47 deletions

View File

@@ -28,32 +28,37 @@
// Copyright © 2003-2018 Natalia Portillo // Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/ *******************************************************************************/
using System.Collections.Generic;
using System.Linq; using System.Linq;
using cicm_web.Models; using Cicm.Database;
using Cicm.Database.Models;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Machine = Cicm.Database.Schemas.Machine; using Microsoft.EntityFrameworkCore;
namespace cicm_web.Controllers namespace cicm_web.Controllers
{ {
public class ComputerController : Controller public class ComputerController : Controller
{ {
readonly cicmContext _context;
readonly IHostingEnvironment hostingEnvironment; readonly IHostingEnvironment hostingEnvironment;
public ComputerController(IHostingEnvironment env) public ComputerController(IHostingEnvironment env, cicmContext context)
{ {
hostingEnvironment = env; hostingEnvironment = env;
_context = context;
} }
public IActionResult Index() public IActionResult Index()
{ {
Program.Database.Operations.GetComputers(out List<Machine> computers); ViewBag.ItemCount = _context.Machines.Count(m => m.Type == MachineType.Computer);
ViewBag.MinYear = _context
ViewBag.ItemCount = computers.Count; .Machines.Where(t => t.Type == MachineType.Computer &&
t.Introduced.HasValue &&
ViewBag.MinYear = computers.Where(t => t.Introduced.Year > 1000).Min(t => t.Introduced.Year); t.Introduced.Value.Year > 1000).Min(t => t.Introduced.Value.Year);
ViewBag.MaxYear = computers.Where(t => t.Introduced.Year > 1000).Max(t => t.Introduced.Year); ViewBag.MaxYear = _context
.Machines.Where(t => t.Type == MachineType.Computer &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Max(t => t.Introduced.Value.Year);
return View(); return View();
} }
@@ -67,17 +72,20 @@ namespace cicm_web.Controllers
ViewBag.Letter = id; ViewBag.Letter = id;
MachineMini[] computers = return View(id == '\0'
id == '\0' ? ComputerMini.GetAllItems() : ComputerMini.GetItemsStartingWithLetter(id); ? _context.Machines.Include(c => c.Company).Where(m => m.Type == MachineType.Computer)
.ToArray()
return View(computers); : _context.Machines.Include(c => c.Company)
.Where(m => m.Type == MachineType.Computer && m.Name.StartsWith(id)).ToArray());
} }
public IActionResult ByYear(int id) public IActionResult ByYear(int id)
{ {
ViewBag.Year = id; ViewBag.Year = id;
return View(ComputerMini.GetItemsFromYear(id)); return View(_context.Machines.Include(c => c.Company)
.Where(t => t.Type == MachineType.Computer && t.Introduced.HasValue &&
t.Introduced.Value.Year == id).ToArray());
} }
} }
} }

View File

@@ -28,32 +28,37 @@
// Copyright © 2003-2018 Natalia Portillo // Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/ *******************************************************************************/
using System.Collections.Generic;
using System.Linq; using System.Linq;
using cicm_web.Models; using Cicm.Database;
using Cicm.Database.Models;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Machine = Cicm.Database.Schemas.Machine; using Microsoft.EntityFrameworkCore;
namespace cicm_web.Controllers namespace cicm_web.Controllers
{ {
public class ConsoleController : Controller public class ConsoleController : Controller
{ {
readonly cicmContext _context;
readonly IHostingEnvironment hostingEnvironment; readonly IHostingEnvironment hostingEnvironment;
public ConsoleController(IHostingEnvironment env) public ConsoleController(IHostingEnvironment env, cicmContext context)
{ {
hostingEnvironment = env; hostingEnvironment = env;
_context = context;
} }
public IActionResult Index() public IActionResult Index()
{ {
Program.Database.Operations.GetConsoles(out List<Machine> consoles); ViewBag.ItemCount = _context.Machines.Count(m => m.Type == MachineType.Console);
ViewBag.MinYear = _context
ViewBag.ItemCount = consoles.Count; .Machines.Where(t => t.Type == MachineType.Console &&
t.Introduced.HasValue &&
ViewBag.MinYear = consoles.Where(t => t.Introduced.Year > 1000).Min(t => t.Introduced.Year); t.Introduced.Value.Year > 1000).Min(t => t.Introduced.Value.Year);
ViewBag.MaxYear = consoles.Where(t => t.Introduced.Year > 1000).Max(t => t.Introduced.Year); ViewBag.MaxYear = _context
.Machines.Where(t => t.Type == MachineType.Console &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Max(t => t.Introduced.Value.Year);
return View(); return View();
} }
@@ -67,17 +72,20 @@ namespace cicm_web.Controllers
ViewBag.Letter = id; ViewBag.Letter = id;
MachineMini[] consoles = return View(id == '\0'
id == '\0' ? ConsoleMini.GetAllItems() : ConsoleMini.GetItemsStartingWithLetter(id); ? _context.Machines.Include(c => c.Company).Where(m => m.Type == MachineType.Console)
.ToArray()
return View(consoles); : _context.Machines.Include(c => c.Company)
.Where(m => m.Type == MachineType.Console && m.Name.StartsWith(id)).ToArray());
} }
public IActionResult ByYear(int id) public IActionResult ByYear(int id)
{ {
ViewBag.Year = id; ViewBag.Year = id;
return View(ConsoleMini.GetItemsFromYear(id)); return View(_context.Machines.Include(c => c.Company)
.Where(t => t.Type == MachineType.Console && t.Introduced.HasValue &&
t.Introduced.Value.Year == id).ToArray());
} }
} }
} }

View File

@@ -31,11 +31,11 @@
ViewData["Title"] = "Computers"; ViewData["Title"] = "Computers";
} }
@using Machine = Cicm.Database.Models.Machine
@model IEnumerable<MachineMini> @model Cicm.Database.Models.Machine[]
<p>Search results:</p> <p>Search results:</p>
<p align=center> <p align="center">
@if(ViewBag.Letter != '\0') @if(ViewBag.Letter != '\0')
{ {
<b>@ViewBag.Letter</b> <b>@ViewBag.Letter</b>
@@ -46,7 +46,7 @@
{ {
<p> <p>
@Model.Count() computers found in the database.<br /> @Model.Count() computers found in the database.<br />
@foreach(MachineMini computer in Model) @foreach(Machine computer in Model)
{ {
<a asp-controller="Machine" <a asp-controller="Machine"
asp-action="View" asp-action="View"

View File

@@ -31,18 +31,18 @@
ViewData["Title"] = "Computers"; ViewData["Title"] = "Computers";
} }
@using Machine = Cicm.Database.Models.Machine
@model IEnumerable<MachineMini> @model Cicm.Database.Models.Machine[]
<p>Search results:</p> <p>Search results:</p>
<p align=center> <p align="center">
<b>@ViewBag.Year</b><br /> <b>@ViewBag.Year</b><br />
@if(Model.Any()) @if(Model.Any())
{ {
<p> <p>
@Model.Count() computers found in the database.<br /> @Model.Count() computers found in the database.<br />
@foreach(MachineMini computer in Model) @foreach(Machine computer in Model)
{ {
<a asp-controller="Machine" <a asp-controller="Machine"
asp-action="View" asp-action="View"

View File

@@ -31,11 +31,11 @@
ViewData["Title"] = "Consoles"; ViewData["Title"] = "Consoles";
} }
@using Machine = Cicm.Database.Models.Machine
@model IEnumerable<MachineMini> @model Cicm.Database.Models.Machine[]
<p>Search results:</p> <p>Search results:</p>
<p align=center> <p align="center">
@if(ViewBag.Letter != '\0') @if(ViewBag.Letter != '\0')
{ {
<b>@ViewBag.Letter</b> <b>@ViewBag.Letter</b>
@@ -46,7 +46,7 @@
{ {
<p> <p>
@Model.Count() computers found in the database.<br /> @Model.Count() computers found in the database.<br />
@foreach(MachineMini console in Model) @foreach(Machine console in Model)
{ {
<a asp-controller="Machine" <a asp-controller="Machine"
asp-action="View" asp-action="View"

View File

@@ -31,18 +31,18 @@
ViewData["Title"] = "Computers"; ViewData["Title"] = "Computers";
} }
@using Machine = Cicm.Database.Models.Machine
@model IEnumerable<MachineMini> @model Cicm.Database.Models.Machine[]
<p>Search results:</p> <p>Search results:</p>
<p align=center> <p align="center">
<b>@ViewBag.Year</b><br /> <b>@ViewBag.Year</b><br />
@if(Model.Any()) @if(Model.Any())
{ {
<p> <p>
@Model.Count() videoconsoles found in the database.<br /> @Model.Count() videoconsoles found in the database.<br />
@foreach(MachineMini console in Model) @foreach(Machine console in Model)
{ {
<a asp-controller="Machine" <a asp-controller="Machine"
asp-action="View" asp-action="View"

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.289</Version> <Version>3.0.99.292</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>