diff --git a/cicm_web/Controllers/HomeController.cs b/cicm_web/Controllers/HomeController.cs index bf788752..b5d517fe 100644 --- a/cicm_web/Controllers/HomeController.cs +++ b/cicm_web/Controllers/HomeController.cs @@ -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 news = new List(); + + 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}); } } \ No newline at end of file diff --git a/cicm_web/Models/NewsModel.cs b/cicm_web/Models/NewsModel.cs new file mode 100644 index 00000000..373e0662 --- /dev/null +++ b/cicm_web/Models/NewsModel.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/cicm_web/Views/Home/Index.cshtml b/cicm_web/Views/Home/Index.cshtml index 20e7a001..510482de 100644 --- a/cicm_web/Views/Home/Index.cshtml +++ b/cicm_web/Views/Home/Index.cshtml @@ -33,55 +33,34 @@ } @using System.IO @using News = Cicm.Database.Models.News -@model List +@model List

News

-@foreach(News news in Model) +@foreach(NewsModel news in Model) { - - News items not yet implemented! - @* // TODO - @if(news.Image != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, news.Image + ".jpg"))) - { - - - } - else - { - - } - - *@ + + + +
- @news.Date + @news.Timestamp
- - - - - @news.Text - - @news.Text -
- - @news.SubText - + @news.Text
@* TODO: Image *@ + + @news.ItemName + +

} \ No newline at end of file diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj index 941a5cae..b2f24d0a 100644 --- a/cicm_web/cicm_web.csproj +++ b/cicm_web/cicm_web.csproj @@ -2,7 +2,7 @@ netcoreapp2.2 - 3.0.99.484 + 3.0.99.486 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website