From d32e5492bc1bf990b3a11d7573f8e3d41abb56b8 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Thu, 31 Mar 2022 17:52:12 +0300 Subject: [PATCH] chore: separate Index and Routes pages --- TicketOffice/Pages/Index.cshtml | 135 +-------------- TicketOffice/Pages/Index.cshtml.cs | 108 +----------- TicketOffice/Pages/Routes/Index.cshtml | 190 ++++++++++++++++++++++ TicketOffice/Pages/Routes/Index.cshtml.cs | 154 ++++++++++++++++++ 4 files changed, 352 insertions(+), 235 deletions(-) create mode 100644 TicketOffice/Pages/Routes/Index.cshtml create mode 100644 TicketOffice/Pages/Routes/Index.cshtml.cs diff --git a/TicketOffice/Pages/Index.cshtml b/TicketOffice/Pages/Index.cshtml index 97c458c..c6be329 100644 --- a/TicketOffice/Pages/Index.cshtml +++ b/TicketOffice/Pages/Index.cshtml @@ -1,137 +1,14 @@ @page -@using System.Globalization -@using Newtonsoft.Json @model TicketOffice.Pages.IndexModel @{ ViewData["Title"] = "Home page"; - CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("uk-UA"); } - - -
-
-
-
- Звідки -
- -
- -
-
- Куди -
- -
-
-
- Дата відправлення -
- -
-
- -
+ - - -@if (Model.Route.Count > 0) -{ -
- - - - - - - - - - - - - - @foreach (var route in Model.Route) - { - - - - - - - - - - } - -
- № автобуса - 🠕 - - Звідки / Куди - - Дата - -
- Відправлення - 🠕 -
-
- Прибуття - 🠕 -
-
- Тривалість - 🠕 - - Вільніих місць - - Дії -
-
@route.Number
- -
-
@route.Cities.First().Name
-
@route.Cities.Last().Name
-
-
- - Відправлення - - - @route.Cities.First().ArrivalTime?.ToString("f").Split(",")[0].ToLower(), - @route.Cities.First().DepartureTime?.ToString("dd.MM.yyyy") - -
-
- Прибуття - - @route.Cities.Last().ArrivalTime?.ToString("f").Split(",")[0].ToLower(), - @route.Cities.Last().ArrivalTime?.ToString("dd.MM.yyyy") - -
-
-
@route.Cities.First().DepartureTime?.ToString("HH:mm")
-
@route.Cities.Last().ArrivalTime?.ToString("HH:mm")
-
- @{ TimeSpan? duration = route.Cities.Last().ArrivalTime - route.Cities.First().DepartureTime; } - @($"{duration?.TotalHours.ToString().Split(",")[0]}:{duration?.Minutes}") - - @(route.Capacity - route.Tickets.Count) - - Вибрати -
-
-} -else -{ -
-

По заданому Вами напрямку місць немає

-
-} \ No newline at end of file diff --git a/TicketOffice/Pages/Index.cshtml.cs b/TicketOffice/Pages/Index.cshtml.cs index 569f546..9f3ec35 100644 --- a/TicketOffice/Pages/Index.cshtml.cs +++ b/TicketOffice/Pages/Index.cshtml.cs @@ -1,118 +1,14 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.EntityFrameworkCore; -using System.Globalization; -using Newtonsoft.Json; -using TicketOffice.Data; -using TicketOffice.Models; -using Route = TicketOffice.Models.Route; +using Microsoft.AspNetCore.Mvc.RazorPages; namespace TicketOffice.Pages; public class IndexModel : PageModel { - private readonly ILogger _logger; - private readonly TicketOfficeContext _context; - - public IndexModel(ILogger logger, TicketOfficeContext context) + public IndexModel() { - _logger = logger; - _context = context; } - public List Route { get; set; } = null!; - [BindProperty(SupportsGet = true)] public string from { get; set; } - [BindProperty(SupportsGet = true)] public string to { get; set; } - [BindProperty(SupportsGet = true)] public DateTime date { get; set; } = new DateTime(2022, 03, 28, 0, 0, 0).Date; - [BindProperty(SupportsGet = true)] public string SortString { get; set; } - public void OnGet() { - RetrieveAllRoutes(); - - if (!string.IsNullOrWhiteSpace(from)) - { - FilterRoutesByFrom(); - } - - if (!string.IsNullOrWhiteSpace(to)) - { - FilterRoutesByTo(); - } - - FilterRoutesByDate(); - } - - public void OnGetSortByNumber() - { - OnGet(); - - Route.Sort((x, y) => Math.Clamp(x.Number - y.Number, -1, 1)); - } - - public void OnGetSortByDeparture() - { - OnGet(); - - Route.Sort((x, y) => Math.Clamp((int)(x.Cities.First().DepartureTime - y.Cities.First().DepartureTime).Value.TotalMilliseconds, -1, 1)); - } - - public void OnGetSortByArrival() - { - OnGet(); - - Route.Sort((x, y) => - { - TimeSpan? totalDuration = x.Cities.Last().ArrivalTime - y.Cities.Last().ArrivalTime; - - return Math.Clamp((int)totalDuration.Value.TotalMilliseconds, -1, 1); - }); - } - - public void OnGetSortByDuration() - { - OnGet(); - - Route.Sort((x, y) => - { - TimeSpan? xDuration = x.Cities.Last().ArrivalTime - x.Cities.First().DepartureTime; - TimeSpan? yDuration = y.Cities.Last().ArrivalTime - y.Cities.First().DepartureTime; - TimeSpan? totalDuration = xDuration - yDuration; - - return Math.Clamp((int)totalDuration.Value.TotalMilliseconds, -1, 1); - }); - } - - private void RetrieveAllRoutes() - { - Route = _context.Route - .Include(r => r.Cities) - .Include(r => r.Tickets) - .ToList(); - } - - private void FilterRoutesByFrom() - { - - Route.ForEach(r => r.Cities = r.Cities - .SkipWhile(c => c.Name != from) - .ToList()); - - Route.RemoveAll(r => r.Cities.Count < 2); - } - - private void FilterRoutesByTo() - { - - Route.ForEach(r => r.Cities = r.Cities - .Reverse().SkipWhile(c => c.Name != to) - .Reverse().ToList()); - - Route.RemoveAll(r => r.Cities.Count < 2); - } - - private void FilterRoutesByDate() - { - Route.RemoveAll(r => r.Cities.First().DepartureTime.Value.DayOfYear != date.DayOfYear); } } \ No newline at end of file diff --git a/TicketOffice/Pages/Routes/Index.cshtml b/TicketOffice/Pages/Routes/Index.cshtml new file mode 100644 index 0000000..a504ec2 --- /dev/null +++ b/TicketOffice/Pages/Routes/Index.cshtml @@ -0,0 +1,190 @@ +@page +@using System.Globalization +@model TicketOffice.Pages.Routes.IndexModel +@{ + Layout = "~/Pages/Shared/_Layout.cshtml"; + ViewData["Title"] = "Пошу маршрутів"; + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("uk-UA"); +} + + + + + + + +
+ +
+
+
+
+ Звідки +
+ +
+ +
+
+ Куди +
+ +
+
+
+ Дата відправлення +
+ +
+
+ +
+
+
+ + @if (Model.Routes != null) + { +
+ + + + + + + + + + + + + + @foreach (var route in Model.Routes) + { + + + + + + + + + + } + +
+ № автобуса + @if (Model.SortString == "increasingNumber") + { + 🠕 + } + else if (Model.SortString == "descendingNumber") + { + 🠗 + } + else + { + + } + + Звідки / Куди + + Дата + +
+ Відправлення + @if (Model.SortString == "increasingDeparture") + { + 🠕 + } + else if (Model.SortString == "descendingDeparture") + { + 🠗 + } + else + { + + } +
+
+ Прибуття + @if (Model.SortString == "increasingArrival") + { + 🠕 + } + else if (Model.SortString == "descendingArrival") + { + 🠗 + } + else + { + + } +
+
+ Тривалість + @if (Model.SortString == "increasingDuration") + { + 🠕 + } + else if (Model.SortString == "descendingDuration") + { + 🠗 + } + else + { + + } + + Вільніих місць + + Дії +
+
@route.Number
+ +
+
@route.Cities.First().Name
+
@route.Cities.Last().Name
+
+
+ + Відправлення + + + @route.Cities.First().ArrivalTime?.ToString("f").Split(",")[0].ToLower(), + @route.Cities.First().DepartureTime?.ToString("dd.MM.yyyy") + +
+
+ Прибуття + + @route.Cities.Last().ArrivalTime?.ToString("f").Split(",")[0].ToLower(), + @route.Cities.Last().ArrivalTime?.ToString("dd.MM.yyyy") + +
+
+
@route.Cities.First().DepartureTime?.ToString("HH:mm")
+
@route.Cities.Last().ArrivalTime?.ToString("HH:mm")
+
+ @{ TimeSpan? duration = route.Cities.Last().ArrivalTime - route.Cities.First().DepartureTime; } + @($"{duration?.TotalHours.ToString().Split(",")[0]}:{duration?.Minutes}") + + @(route.Capacity - route.Tickets.Count) + + Вибрати +
+
+ } + else + { +
+

По заданому Вами напрямку місць немає

+
+ } + +
\ No newline at end of file diff --git a/TicketOffice/Pages/Routes/Index.cshtml.cs b/TicketOffice/Pages/Routes/Index.cshtml.cs new file mode 100644 index 0000000..cffd3a1 --- /dev/null +++ b/TicketOffice/Pages/Routes/Index.cshtml.cs @@ -0,0 +1,154 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.EntityFrameworkCore; +using TicketOffice.Data; +using Route = TicketOffice.Models.Route; + +namespace TicketOffice.Pages.Routes; + +public class IndexModel : PageModel +{ + public List Routes { get; set; } = null!; + [BindProperty(SupportsGet = true)] public string From { get; set; } + [BindProperty(SupportsGet = true)] public string To { get; set; } + [BindProperty(SupportsGet = true)] public DateTime Date { get; set; } = new DateTime(2022, 03, 28, 0, 0, 0).Date; + [BindProperty(SupportsGet = true)] public string SortString { get; set; } + + private readonly TicketOfficeContext _context; + + public IndexModel(TicketOfficeContext context) + { + _context = context; + } + + public void OnGet() + { + RetrieveAllRoutes(); + + if (!string.IsNullOrWhiteSpace(From)) + { + FilterRoutesByFrom(); + } + + if (!string.IsNullOrWhiteSpace(To)) + { + FilterRoutesByTo(); + } + + FilterRoutesByDate(); + } + + public void OnGetSortByNumber() + { + OnGet(); + + if (SortString == "increasingNumber") + { + Routes.Sort((x, y) => Math.Clamp(x.Number - y.Number, -1, 1)); + } + else + { + Routes.Sort((x, y) => Math.Clamp(y.Number - x.Number, -1, 1)); + } + } + + public void OnGetSortByDeparture() + { + OnGet(); + + Routes.Sort((x, y) => + { + TimeSpan? totalDuration; + + if (SortString == "increasingDeparture") + { + totalDuration = x.Cities.First().DepartureTime - y.Cities.First().DepartureTime; + } + else + { + totalDuration = y.Cities.First().DepartureTime - x.Cities.First().DepartureTime; + } + + return Math.Clamp((int)totalDuration.Value.TotalMilliseconds, -1, 1); + }); + } + + public void OnGetSortByArrival() + { + OnGet(); + + + + Routes.Sort((x, y) => + { + TimeSpan? totalDuration; + + if (SortString == "increasingArrival") + { + totalDuration = x.Cities.Last().ArrivalTime - y.Cities.Last().ArrivalTime; + } + else + { + totalDuration = y.Cities.Last().ArrivalTime - x.Cities.Last().ArrivalTime; + } + + return Math.Clamp((int)totalDuration.Value.TotalMilliseconds, -1, 1); + }); + } + + public void OnGetSortByDuration() + { + OnGet(); + + Routes.Sort((x, y) => + { + TimeSpan? xDuration = x.Cities.Last().ArrivalTime - x.Cities.First().DepartureTime; + TimeSpan? yDuration = y.Cities.Last().ArrivalTime - y.Cities.First().DepartureTime; + TimeSpan? totalDuration; + + if (SortString == "increasingDuration") + { + totalDuration = xDuration - yDuration; + } + else + { + totalDuration = yDuration - xDuration; + } + + return Math.Clamp((int)totalDuration.Value.TotalMilliseconds, -1, 1); + }); + } + + private void RetrieveAllRoutes() + { + Routes = _context.Route + .Include(r => r.Cities) + .Include(r => r.Tickets) + .ToList(); + } + + private void FilterRoutesByFrom() + { + + Routes.ForEach(r => r.Cities = r.Cities + .SkipWhile(c => c.Name != From) + .ToList()); + + Routes.RemoveAll(r => r.Cities.Count < 2); + } + + private void FilterRoutesByTo() + { + + Routes.ForEach(r => r.Cities = r.Cities + .Reverse().SkipWhile(c => c.Name != To) + .Reverse().ToList()); + + Routes.RemoveAll(r => r.Cities.Count < 2); + } + + private void FilterRoutesByDate() + { + Routes.RemoveAll(r => r.Cities.First().DepartureTime.Value.DayOfYear != Date.DayOfYear); + } +} \ No newline at end of file