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
*******************************************************************************/
using System.Collections.Generic;
using System.Linq;
using cicm_web.Models;
using Cicm.Database;
using Cicm.Database.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Machine = Cicm.Database.Schemas.Machine;
using Microsoft.EntityFrameworkCore;
namespace cicm_web.Controllers
{
public class ComputerController : Controller
{
readonly cicmContext _context;
readonly IHostingEnvironment hostingEnvironment;
public ComputerController(IHostingEnvironment env)
public ComputerController(IHostingEnvironment env, cicmContext context)
{
hostingEnvironment = env;
_context = context;
}
public IActionResult Index()
{
Program.Database.Operations.GetComputers(out List<Machine> computers);
ViewBag.ItemCount = computers.Count;
ViewBag.MinYear = computers.Where(t => t.Introduced.Year > 1000).Min(t => t.Introduced.Year);
ViewBag.MaxYear = computers.Where(t => t.Introduced.Year > 1000).Max(t => t.Introduced.Year);
ViewBag.ItemCount = _context.Machines.Count(m => m.Type == MachineType.Computer);
ViewBag.MinYear = _context
.Machines.Where(t => t.Type == MachineType.Computer &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Min(t => t.Introduced.Value.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();
}
@@ -67,17 +72,20 @@ namespace cicm_web.Controllers
ViewBag.Letter = id;
MachineMini[] computers =
id == '\0' ? ComputerMini.GetAllItems() : ComputerMini.GetItemsStartingWithLetter(id);
return View(computers);
return View(id == '\0'
? _context.Machines.Include(c => c.Company).Where(m => m.Type == MachineType.Computer)
.ToArray()
: _context.Machines.Include(c => c.Company)
.Where(m => m.Type == MachineType.Computer && m.Name.StartsWith(id)).ToArray());
}
public IActionResult ByYear(int 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
*******************************************************************************/
using System.Collections.Generic;
using System.Linq;
using cicm_web.Models;
using Cicm.Database;
using Cicm.Database.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Machine = Cicm.Database.Schemas.Machine;
using Microsoft.EntityFrameworkCore;
namespace cicm_web.Controllers
{
public class ConsoleController : Controller
{
readonly cicmContext _context;
readonly IHostingEnvironment hostingEnvironment;
public ConsoleController(IHostingEnvironment env)
public ConsoleController(IHostingEnvironment env, cicmContext context)
{
hostingEnvironment = env;
_context = context;
}
public IActionResult Index()
{
Program.Database.Operations.GetConsoles(out List<Machine> consoles);
ViewBag.ItemCount = consoles.Count;
ViewBag.MinYear = consoles.Where(t => t.Introduced.Year > 1000).Min(t => t.Introduced.Year);
ViewBag.MaxYear = consoles.Where(t => t.Introduced.Year > 1000).Max(t => t.Introduced.Year);
ViewBag.ItemCount = _context.Machines.Count(m => m.Type == MachineType.Console);
ViewBag.MinYear = _context
.Machines.Where(t => t.Type == MachineType.Console &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Min(t => t.Introduced.Value.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();
}
@@ -67,17 +72,20 @@ namespace cicm_web.Controllers
ViewBag.Letter = id;
MachineMini[] consoles =
id == '\0' ? ConsoleMini.GetAllItems() : ConsoleMini.GetItemsStartingWithLetter(id);
return View(consoles);
return View(id == '\0'
? _context.Machines.Include(c => c.Company).Where(m => m.Type == MachineType.Console)
.ToArray()
: _context.Machines.Include(c => c.Company)
.Where(m => m.Type == MachineType.Console && m.Name.StartsWith(id)).ToArray());
}
public IActionResult ByYear(int 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";
}
@model IEnumerable<MachineMini>
@using Machine = Cicm.Database.Models.Machine
@model Cicm.Database.Models.Machine[]
<p>Search results:</p>
<p align=center>
<p align="center">
@if(ViewBag.Letter != '\0')
{
<b>@ViewBag.Letter</b>
@@ -46,7 +46,7 @@
{
<p>
@Model.Count() computers found in the database.<br />
@foreach(MachineMini computer in Model)
@foreach(Machine computer in Model)
{
<a asp-controller="Machine"
asp-action="View"

View File

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

View File

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

View File

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

View File

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