diff --git a/.gitignore b/.gitignore index ef318f03..a2d98456 100644 --- a/.gitignore +++ b/.gitignore @@ -440,3 +440,6 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk + +# Assets +cicm_web/wwwroot/assets/ diff --git a/cicm_web/Controllers/HomeController.cs b/cicm_web/Controllers/HomeController.cs index 4544e973..2f904a7d 100644 --- a/cicm_web/Controllers/HomeController.cs +++ b/cicm_web/Controllers/HomeController.cs @@ -30,15 +30,24 @@ using System.Diagnostics; using cicm_web.Models; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; namespace cicm_web.Controllers { public class HomeController : Controller { + readonly IHostingEnvironment hostingEnvironment; + + public HomeController(IHostingEnvironment env) + { + hostingEnvironment = env; + } + public IActionResult Index() { - return View(); + ViewBag.WebRootPath = hostingEnvironment.WebRootPath; + return View(News.GetLastItems()); } public IActionResult About() diff --git a/cicm_web/Models/News.cs b/cicm_web/Models/News.cs new file mode 100644 index 00000000..4d965119 --- /dev/null +++ b/cicm_web/Models/News.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using Cicm.Database.Schemas; + +namespace cicm_web.Models +{ + public class News + { + /// Affected ID + public int AffectedId; + /// Date + public DateTime Date; + /// URL of image + public string Image; + /// URL of target view, if applicable + public string TargetView; + /// Text + public string Text; + + public static News[] GetAllItems() + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetNews(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + return dbItems.OrderByDescending(i => i.Id).Select(TransformItem) as News[]; + } + + public static News[] GetLastItems(int count = 10) + { + List dbItems = null; + bool? result = Program.Database?.Operations.GetNews(out dbItems); + if(result == null || result.Value == false || dbItems == null) return null; + + return dbItems.OrderByDescending(i => i.Id).Take(count).Select(TransformItem).ToArray(); + } + + static News TransformItem(Cicm.Database.Schemas.News dbItem) + { + string imageUrl; + string text; + string targetView; + + switch(dbItem.Type) + { + case NewsType.NewComputerInDb: + text = "New computer added to the database."; + imageUrl = "assets/photos/computers/"; + targetView = "computer"; + break; + case NewsType.NewConsoleInDb: + text = "New videoconsole added to the database."; + imageUrl = "assets/photos/consoles/"; + targetView = "console"; + break; + case NewsType.NewComputerInCollection: + text = "New computer added to the museum's collection."; + imageUrl = "assets/photos/computers/"; + targetView = "collection_computer"; + break; + case NewsType.NewConsoleInCollection: + text = "New videoconsole added to the museum's collection."; + imageUrl = "assets/photos/consoles/"; + targetView = "collection_console"; + break; + case NewsType.UpdatedComputerInDb: + text = "Updated computer from the database."; + imageUrl = "assets/photos/computers/"; + targetView = "computer"; + break; + case NewsType.UpdatedConsoleInDb: + text = "Updated videoconsole from the database."; + imageUrl = "assets/photos/consoles/"; + targetView = "console"; + break; + case NewsType.UpdatedComputerInCollection: + text = "Updated computer from museum's collection."; + imageUrl = "assets/photos/computers/"; + targetView = "collection_computer"; + break; + case NewsType.UpdatedConsoleInCollection: + text = "Updated videoconsole from museum's collection."; + imageUrl = "assets/photos/consoles/"; + targetView = "collection_console"; + break; + case NewsType.NewMoneyDonation: + text = "New money donation."; + imageUrl = null; + targetView = null; + break; + default: throw new ArgumentOutOfRangeException(); + } + + return new News + { + AffectedId = dbItem.AffectedId, + Date = DateTime.ParseExact(dbItem.Date, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture), + Image = imageUrl == null ? null : imageUrl + $"{dbItem.AffectedId}", + Text = text, + TargetView = targetView + }; + } + } +} \ No newline at end of file diff --git a/cicm_web/Views/Home/Index.cshtml b/cicm_web/Views/Home/Index.cshtml index b25c5f2c..bd8f840e 100644 --- a/cicm_web/Views/Home/Index.cshtml +++ b/cicm_web/Views/Home/Index.cshtml @@ -31,11 +31,54 @@ ViewData["Title"] = "Home Page"; } +@using System.IO +@model IEnumerable News - - News should be fetch from the database - But this is currently not implemented! - TODO: TODO - \ No newline at end of file +@foreach(News news in Model) +{ + + + + @news.Date + + + + @if(news.Image != null && File.Exists(System.IO.Path.Combine(ViewBag.WebRootPath, news.Image + ".jpg"))) + { + + + + + + + @news.Text + + } + else + { + + @news.Text + + } + + + + + + Company - Computer + + + + + +} \ No newline at end of file diff --git a/cicm_web/cicm_web.csproj b/cicm_web/cicm_web.csproj index 426a8c76..219b039a 100644 --- a/cicm_web/cicm_web.csproj +++ b/cicm_web/cicm_web.csproj @@ -2,7 +2,7 @@ netcoreapp2.0 - 3.0.99.23 + 3.0.99.36 Canary Islands Computer Museum Copyright © 2003-2018 Natalia Portillo Canary Islands Computer Museum Website @@ -24,7 +24,6 @@ - diff --git a/cicm_web/wwwroot/assets/photos/.donotremove b/cicm_web/wwwroot/assets/photos/computers/.donotremove similarity index 100% rename from cicm_web/wwwroot/assets/photos/.donotremove rename to cicm_web/wwwroot/assets/photos/computers/.donotremove diff --git a/cicm_web/wwwroot/assets/photos/consoles/.donotremove b/cicm_web/wwwroot/assets/photos/consoles/.donotremove new file mode 100644 index 00000000..e69de29b