From 4be86d5d12263f82987879621b26c5be89c6fac1 Mon Sep 17 00:00:00 2001 From: Krzysztof Wieclaw Date: Thu, 15 Nov 2018 17:15:21 +0100 Subject: [PATCH] Added paging --- GearRacingCloud/Controllers/HomeController.cs | 7 +++- GearRacingCloud/Controllers/TestController.cs | 10 ++--- .../HelperMethods/StringDisplayExtensions.cs | 30 +++++++++++++ GearRacingCloud/Startup.cs | 2 +- GearRacingCloud/Views/Home/Index.cshtml | 33 ++++++++++----- .../Views/Shared/_LayoutRefreshing.cshtml | 49 ++++++++++++++++++++++ GearRacingCloud/Views/Test/FullCar.cshtml | 24 +++++++---- GearRacingCloud/Views/Test/FullLap.cshtml | 30 ++++++++----- GearRacingCloud/wwwroot/css/site.css | 45 ++++++++++---------- 9 files changed, 174 insertions(+), 56 deletions(-) create mode 100644 GearRacingCloud/HelperMethods/StringDisplayExtensions.cs create mode 100644 GearRacingCloud/Views/Shared/_LayoutRefreshing.cshtml diff --git a/GearRacingCloud/Controllers/HomeController.cs b/GearRacingCloud/Controllers/HomeController.cs index 345c111..2aa73f4 100644 --- a/GearRacingCloud/Controllers/HomeController.cs +++ b/GearRacingCloud/Controllers/HomeController.cs @@ -1,4 +1,5 @@ using System; +using System.Timers; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -10,9 +11,11 @@ namespace GearRacingCloud.Controllers { public class HomeController : Controller { - public IActionResult Index() + const int perPage = 5; + public IActionResult Index([FromQuery] int id) { - return View(Laps.GetInstance().GetLaps()); + var laps = Laps.GetInstance().GetLaps().OrderBy(l => l.laptime).Skip(id * perPage).Take(perPage); + return View((laps,id, perPage)); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] diff --git a/GearRacingCloud/Controllers/TestController.cs b/GearRacingCloud/Controllers/TestController.cs index bdcc386..8b3c6dc 100644 --- a/GearRacingCloud/Controllers/TestController.cs +++ b/GearRacingCloud/Controllers/TestController.cs @@ -14,7 +14,7 @@ namespace GearRacingCloud.Controllers { public class TestController : Controller { - const int perSize = 5; + const int perPage = 5; private HttpContent _CreateJsonContent(string json) { var buffer = System.Text.Encoding.UTF8.GetBytes(json); @@ -55,15 +55,15 @@ namespace GearRacingCloud.Controllers [Route("test/full-lap.html")] public IActionResult FullLap([FromQuery] int id) { - IEnumerable laps = Laps.GetInstance().GetLaps().Skip(id * perSize).Take(perSize); - return View(laps); + IEnumerable laps = Laps.GetInstance().GetLaps().Skip(id * perPage).Take(perPage); + return View((laps,id, perPage)); } [Route("test/full-car.html")] public IActionResult FullCar([FromQuery] int id) { - IEnumerable cars = Cars.GetInstance().GetCars().Skip(id * perSize).Take(perSize); - return View(cars); + IEnumerable cars = Cars.GetInstance().GetCars().Skip(id * perPage).Take(perPage); + return View((cars,id, perPage)); } [HttpPost] diff --git a/GearRacingCloud/HelperMethods/StringDisplayExtensions.cs b/GearRacingCloud/HelperMethods/StringDisplayExtensions.cs new file mode 100644 index 0000000..75a0d73 --- /dev/null +++ b/GearRacingCloud/HelperMethods/StringDisplayExtensions.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace GearRacingCloud.HelperMethods +{ + static public class StringDisplayExtensions + { + static public string ShortenTo(this string s, int maxChars, char omitChar = '.') + { + if (maxChars >= s.Length) + return s; + + if (maxChars < 2) + return s.Substring(0, maxChars); + + if (maxChars == 2) + return s.Substring(0, 1) + "."; + + int omitCharCount = Math.Min(maxChars - 2, 3); + int startCharsCount = (int)Math.Ceiling((maxChars - omitCharCount) / 2.0f); + int endCharsCount = (int)Math.Floor((maxChars - omitCharCount) / 2.0f); + string separator = new string(omitChar, omitCharCount); + string startChars = s.Substring(0, startCharsCount); + string endChars = s.Substring(s.Length - endCharsCount); + return startChars + separator + endChars; + } + } +} diff --git a/GearRacingCloud/Startup.cs b/GearRacingCloud/Startup.cs index 23db974..3b1b2b3 100644 --- a/GearRacingCloud/Startup.cs +++ b/GearRacingCloud/Startup.cs @@ -57,7 +57,7 @@ namespace GearRacingCloud { routes.MapRoute( name: "default", - template: "{controller=Home}/{action=Index}/{id?}"); + template: "{controller=Home}/{action=Index}"); }); } } diff --git a/GearRacingCloud/Views/Home/Index.cshtml b/GearRacingCloud/Views/Home/Index.cshtml index 2429daa..18362ba 100644 --- a/GearRacingCloud/Views/Home/Index.cshtml +++ b/GearRacingCloud/Views/Home/Index.cshtml @@ -1,43 +1,56 @@ -@model IEnumerable +@model (IEnumerable Laps, int Id, int PerSize) @{ - ViewData["Title"] = "Home Page"; + Layout = "~/Views/Shared/_LayoutRefreshing.cshtml"; + ViewData["Title"] = "Racing Results"; }

Lap Counter

+ - + + - @if (Model.Count() == 0) + @if (Model.Laps.Count() == 0) { - + } - @foreach (Lap lap in Model.OrderBy(l => l.laptime)) + @foreach (var lapTuple in Model.Laps.Select((lap, i) => (lap, i))) { - - - + + + + }
Pos.
Driver
Laptime
Car Id

No registered lap!

No registered lap!

@lap.driver

@TimeSpan.FromMilliseconds(lap.laptime).ToString(@"hh\:mm\:ss\:fff")

@lap.carId

@(lapTuple.Item2+Model.Id*Model.PerSize + 1).

@lapTuple.Item1.driver

@TimeSpan.FromMilliseconds(lapTuple.Item1.laptime).ToString(@"hh\:mm\:ss\:fff")

@lapTuple.Item1.carId

- +

+ @if (Model.Id > 0) + { + @Html.ActionLink("<<", "Index", "Home", new { id = Model.Id - 1 }); + } + @if (Model.Laps.Count() == Model.PerSize) + { + @Html.ActionLink(">>", "Index", "Home", new { id = Model.Id + 1 }); + } +

Gear Racing Cloud - 2018 ©

diff --git a/GearRacingCloud/Views/Shared/_LayoutRefreshing.cshtml b/GearRacingCloud/Views/Shared/_LayoutRefreshing.cshtml new file mode 100644 index 0000000..795a266 --- /dev/null +++ b/GearRacingCloud/Views/Shared/_LayoutRefreshing.cshtml @@ -0,0 +1,49 @@ + + + + + + + @ViewData["Title"] - Gear Racing Cloud + + + + + + + + + + + + +
+ @RenderBody(); +
+ + + + + + + + + + + + + @RenderSection("Scripts", required: false) + + diff --git a/GearRacingCloud/Views/Test/FullCar.cshtml b/GearRacingCloud/Views/Test/FullCar.cshtml index 12264bb..97b967d 100644 --- a/GearRacingCloud/Views/Test/FullCar.cshtml +++ b/GearRacingCloud/Views/Test/FullCar.cshtml @@ -1,4 +1,5 @@ -@model IEnumerable +@using GearRacingCloud.HelperMethods +@model (IEnumerable Cars, int Id, int PerSize) @{ ViewData["Title"] = "FullCarView"; @@ -24,26 +25,35 @@ - @if (Model.Count() == 0) + @if (Model.Cars.Count() == 0) {

No registered car!

} - @foreach (Car car in Model.OrderBy(c => c.carName)) + @foreach (Car car in Model.Cars.OrderBy(c => c.carName)) { -

@car.carId

+

@car.carId.ShortenTo(20)

@car.carIp

-

@car.carName

+

@car.carName.ShortenTo(20)

@car.apMac

-

@car.apSsid

+

@car.apSsid.ShortenTo(20)

} - +

+ @if (Model.Id > 0) + { + @Html.ActionLink("<<", "FullCar", "Test", new { id = Model.Id - 1 }); + } + @if (Model.Cars.Count() == Model.PerSize) + { + @Html.ActionLink(">>", "FullCar", "Test", new { id = Model.Id + 1 }); + } +

Gear Racing Cloud - 2018 ©

diff --git a/GearRacingCloud/Views/Test/FullLap.cshtml b/GearRacingCloud/Views/Test/FullLap.cshtml index 75c91a3..88e0cbf 100644 --- a/GearRacingCloud/Views/Test/FullLap.cshtml +++ b/GearRacingCloud/Views/Test/FullLap.cshtml @@ -1,4 +1,5 @@ -@model IEnumerable +@using GearRacingCloud.HelperMethods +@model (IEnumerable Laps, int Id, int PerSize) @{ ViewData["Title"] = "FullLapView"; @@ -8,10 +9,10 @@

Full Lap View

- + + + - - @@ -24,26 +25,35 @@ - @if (Model.Count() == 0) + @if (Model.Laps.Count() == 0) { } - @foreach (Lap lap in Model.OrderByDescending(l => l.timeStamp)) + @foreach (Lap lap in Model.Laps.OrderByDescending(l => l.timeStamp)) { - + - - + + }

No registered lap!

@lap.id@lap.id.ShortenTo(30) @DateTime.FromBinary(lap.timeStamp).ToString() @TimeSpan.FromMilliseconds(lap.laptime).ToString(@"hh\:mm\:ss\:fff")@lap.driver@lap.carId@lap.driver.ShortenTo(15)@lap.carId.ShortenTo(20)
- +

+ @if (Model.Id > 0) + { + @Html.ActionLink("<<", "FullLap", "Test", new { id = Model.Id - 1 }); + } + @if (Model.Laps.Count() == Model.PerSize) + { + @Html.ActionLink(">>", "FullLap", "Test", new { id = Model.Id + 1 }); + } +

Gear Racing Cloud - 2018 ©

diff --git a/GearRacingCloud/wwwroot/css/site.css b/GearRacingCloud/wwwroot/css/site.css index 769c7a4..180e776 100644 --- a/GearRacingCloud/wwwroot/css/site.css +++ b/GearRacingCloud/wwwroot/css/site.css @@ -9,10 +9,7 @@ for details on configuring this project to bundle and minify static web assets. } body { - padding-top: 5%; - padding-bottom: 20px; - padding-left: 5%; - padding-right: 5%; + padding: 5%; background-image: url("/images/table/BG.png"); background-repeat: no-repeat, repeat, repeat; background-size: cover; @@ -103,32 +100,36 @@ html { } .main-header { - height: 54px; + height: 10%; color: #FFFFFF; font-family: SamsungOne800; - font-size: 70px; + font-size: 440%; line-height: 50px; text-align: left; } -table.racing-table { - border-collapse: separate; - border-spacing: 0px 19px; +.racing-line { + height: 2px; + opacity: 0.28; + background-color: #8D8283; + margin-right: 50px; } -table.racing-table tr { +table.racing-table { + border-collapse: separate; + border-spacing: 0px 2vh; } table.racing-table th { - height: 24px; + height: 10vh; color: #B62626; font-family: SamsungOne800; - font-size: 32px; + font-size: 200%; line-height: 50px; text-align: left; } -table.racing-table td:first-child, th:first-child { +table.racing-table td:first-child { padding-left: 10px; } @@ -145,17 +146,19 @@ table.racing-table > tbody > tr:nth-of-type(2n) { } table.racing-table td { - height: 73px; + height: 6vh; font-family: SamsungOne450; text-align: left; color: white; - font-size: 24px; - padding-top: 11px; + font-size: x-large; + align-self: center; + vertical-align: central; } -.racing-line { - height: 2px; - opacity: 0.28; - background-color: #8D8283; - margin-right: 50px; +table.racing-table > thead > tr > th:last-child > .racing-line { + margin-right: 0px; +} + +table.racing-table > tbody > tr > td:last-child > .racing-line { + margin-right: 0px; } \ No newline at end of file -- 2.7.4