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
*******************************************************************************/
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using cicm_web.Models;
using Cicm.Database;
using Cicm.Database.Models;
using cicm_web.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
@@ -39,34 +41,84 @@ namespace cicm_web.Controllers
{
public class HomeController : Controller
{
readonly cicmContext _context;
readonly IHostingEnvironment hostingEnvironment;
readonly cicmContext _context;
public HomeController(IHostingEnvironment env, cicmContext context)
{
hostingEnvironment = env;
_context = context;
_context = context;
}
public IActionResult Index()
{
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()
{
return View();
}
public IActionResult About() => View();
public IActionResult Contact()
{
return View();
}
public IActionResult Contact() => View();
public IActionResult Error()
{
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
}
public IActionResult Error() =>
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 News = Cicm.Database.Models.News
@model List<Cicm.Database.Models.News>
@model List<NewsModel>
<h2>News</h2>
@foreach(News news in Model)
@foreach(NewsModel news in Model)
{
<table border="0"
width="100%">
<tr>
<td colspan="2">
<b>@news.Date</b>
<b>@news.Timestamp</b>
</td>
</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>
<td colspan="2">
<a asp-action="View"
asp-controller="@news.TargetView"
asp-route-id="@news.AffectedId">
@news.SubText
</a>
@news.Text
</td>
*@
</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>
<br />
}

View File

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