Implement news.

This commit is contained in:
2019-05-19 16:53:18 +01:00
parent 3ca5d5029e
commit 51a3ca3f31
4 changed files with 107 additions and 52 deletions

View File

@@ -28,10 +28,12 @@
// Copyright © 2003-2018 Natalia Portillo // Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/ *******************************************************************************/
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using cicm_web.Models; using Cicm.Database;
using Cicm.Database.Models; using Cicm.Database.Models;
using cicm_web.Models;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -39,34 +41,84 @@ namespace cicm_web.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
readonly cicmContext _context;
readonly IHostingEnvironment hostingEnvironment; readonly IHostingEnvironment hostingEnvironment;
readonly cicmContext _context;
public HomeController(IHostingEnvironment env, cicmContext context) public HomeController(IHostingEnvironment env, cicmContext context)
{ {
hostingEnvironment = env; hostingEnvironment = env;
_context = context; _context = context;
} }
public IActionResult Index() public IActionResult Index()
{ {
ViewBag.WebRootPath = hostingEnvironment.WebRootPath; ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
return View(_context.News.OrderByDescending(t => t.Date).Take(10).ToList());
List<NewsModel> news = new List<NewsModel>();
foreach(News @new in _context.News.OrderByDescending(t => t.Date).Take(10))
{
Machine machine = _context.Machines.Find(@new.AddedId);
if(machine is null) continue;
switch(@new.Type)
{
case NewsType.NewComputerInDb:
news.Add(new NewsModel(@new.AddedId, "New computer in database", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewConsoleInDb:
news.Add(new NewsModel(@new.AddedId, "New console in database", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewComputerInCollection:
news.Add(new NewsModel(@new.AddedId, "New computer in collection", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewConsoleInCollection:
news.Add(new NewsModel(@new.AddedId, "New console in collection", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedComputerInDb:
news.Add(new NewsModel(@new.AddedId, "Updated computer in database", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedConsoleInDb:
news.Add(new NewsModel(@new.AddedId, "Updated console in database", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedComputerInCollection:
news.Add(new NewsModel(@new.AddedId, "Updated computer in collection", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedConsoleInCollection:
news.Add(new NewsModel(@new.AddedId, "Updated console in collection", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewMoneyDonation:
// TODO
break;
default: continue;
}
}
return View(news);
} }
public IActionResult About() public IActionResult About() => View();
{
return View();
}
public IActionResult Contact() public IActionResult Contact() => View();
{
return View();
}
public IActionResult Error() public IActionResult Error() =>
{ View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
}
} }
} }

View File

@@ -0,0 +1,24 @@
using System;
namespace cicm_web.Models
{
public class NewsModel
{
public readonly int AffectedId;
public readonly string Text;
public readonly DateTime Timestamp;
public readonly string Controller;
public readonly string Action;
public readonly string ItemName;
public NewsModel(int affectedId, string text, DateTime timestamp, string controller, string action, string itemName)
{
AffectedId = affectedId;
Text = text;
Timestamp = timestamp;
Controller = controller;
Action = action;
ItemName = itemName;
}
}
}

View File

@@ -33,55 +33,34 @@
} }
@using System.IO @using System.IO
@using News = Cicm.Database.Models.News @using News = Cicm.Database.Models.News
@model List<Cicm.Database.Models.News> @model List<NewsModel>
<h2>News</h2> <h2>News</h2>
@foreach(News news in Model) @foreach(NewsModel news in Model)
{ {
<table border="0" <table border="0"
width="100%"> width="100%">
<tr> <tr>
<td colspan="2"> <td colspan="2">
<b>@news.Date</b> <b>@news.Timestamp</b>
</td> </td>
</tr> </tr>
<tr>
News items not yet implemented!
@* // TODO
@if(news.Image != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, news.Image + ".jpg")))
{
<td height="128"
width="128">
<a asp-action="View"
asp-controller="@news.TargetView"
asp-route-id="@news.AffectedId">
<img height="128"
width="128"
src="@(news.Image + ".jpg")">
</a>
</td>
<td>
@news.Text
</td>
}
else
{
<td colspan="2">
@news.Text
</td>
}
</tr>
<tr> <tr>
<td colspan="2"> <td colspan="2">
<a asp-action="View" @news.Text
asp-controller="@news.TargetView"
asp-route-id="@news.AffectedId">
@news.SubText
</a>
</td> </td>
*@
</tr> </tr>
<tr>
<td>@* TODO: Image *@</td>
<td>
<a asp-action="@news.Action"
asp-route-id="@news.AffectedId"
asp-controller="@news.Controller">
@news.ItemName
</a>
</td>
</tr>
</table> </table>
<br /> <br />
} }

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
<Version>3.0.99.484</Version> <Version>3.0.99.486</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>