chore: separate Index and Routes pages
This commit is contained in:
parent
1146093913
commit
d32e5492bc
@ -1,137 +1,14 @@
|
|||||||
@page
|
@page
|
||||||
@using System.Globalization
|
|
||||||
@using Newtonsoft.Json
|
|
||||||
@model TicketOffice.Pages.IndexModel
|
@model TicketOffice.Pages.IndexModel
|
||||||
@{
|
@{
|
||||||
ViewData["Title"] = "Home page";
|
ViewData["Title"] = "Home page";
|
||||||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("uk-UA");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
<link rel="stylesheet" href="~/css/Routes.css" asp-append-version="true"/>
|
<div class="topnav">
|
||||||
|
<a class="active" href="/">Головна</a>
|
||||||
<form class="search-block">
|
<a href="/routes">Пошук маршрутів</a>
|
||||||
<div class="opt">
|
<div class="topnav-right">
|
||||||
<div class="station">
|
<a href="/auth/login">Авторизація</a>
|
||||||
<div class="title">
|
|
||||||
Звідки
|
|
||||||
</div>
|
|
||||||
<input type="text" autocomplete="off" asp-for="from">
|
|
||||||
</div>
|
|
||||||
<a class="change-dir"></a>
|
|
||||||
<div class="station">
|
|
||||||
<div class="title">
|
|
||||||
Куди
|
|
||||||
</div>
|
|
||||||
<input type="text" autocomplete="off" asp-for="to">
|
|
||||||
</div>
|
|
||||||
<div class="date">
|
|
||||||
<div class="title">
|
|
||||||
Дата відправлення
|
|
||||||
</div>
|
|
||||||
<input type="date" value="@Model.date.ToString("yyyy-MM-dd")" asp-for="date">
|
|
||||||
</div>
|
|
||||||
<div class="search-btn">
|
|
||||||
<input type="submit" class="search-btn" value="Пошук"/>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</div>
|
||||||
|
|
||||||
<!--
|
|
||||||
<a asp-page-handler="SortByDuration" asp-route-from=@Model.from a in a asp-route-to=@Model.to asp-route-date=@Model.date.ToString("yyyy-MM-dd") asp-route-isDescending="false">За зростанням</a>
|
|
||||||
<a asp-page-handler="SortByDuration" asp-route-from=@Model.from a in a asp-route-to=@Model.to asp-route-date=@Model.date.ToString("yyyy-MM-dd") asp-route-isDescending="true">За спаданням</a>
|
|
||||||
-->
|
|
||||||
|
|
||||||
@if (Model.Route.Count > 0)
|
|
||||||
{
|
|
||||||
<div class="route-list">
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>
|
|
||||||
№ автобуса
|
|
||||||
<a class="link-btn-sort" asp-page-handler="SortByNumber" asp-route-from=@Model.from a in a asp-route-to=@Model.to asp-route-date=@Model.date.ToString("yyyy-MM-dd")>🠕</a>
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Звідки / Куди
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Дата
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
<div class="departure">
|
|
||||||
Відправлення
|
|
||||||
<a class="link-btn-sort" asp-page-handler="SortByDeparture" asp-route-from=@Model.from a in a asp-route-to=@Model.to asp-route-date=@Model.date.ToString("yyyy-MM-dd")>🠕</a>
|
|
||||||
</div>
|
|
||||||
<div class="arrival">
|
|
||||||
Прибуття
|
|
||||||
<a class="link-btn-sort" asp-page-handler="SortByArrival" asp-route-from=@Model.from a in a asp-route-to=@Model.to asp-route-date=@Model.date.ToString("yyyy-MM-dd")>🠕</a>
|
|
||||||
</div>
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Тривалість
|
|
||||||
<a class="link-btn-sort" asp-page-handler="SortByDuration" asp-route-from=@Model.from a in a asp-route-to=@Model.to asp-route-date=@Model.date.ToString("yyyy-MM-dd")>🠕</a>
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Вільніих місць
|
|
||||||
</th>
|
|
||||||
<th>
|
|
||||||
Дії
|
|
||||||
</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
@foreach (var route in Model.Route)
|
|
||||||
{
|
|
||||||
<tr class="table-row">
|
|
||||||
<td class="num">
|
|
||||||
<div>@route.Number</div>
|
|
||||||
<div class="city-list-btn"><a class="city-list-btn">Маршрут</a></div>
|
|
||||||
</td>
|
|
||||||
<td class="city">
|
|
||||||
<div>@route.Cities.First().Name</div>
|
|
||||||
<div>@route.Cities.Last().Name</div>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<div class="route-date">
|
|
||||||
<span>
|
|
||||||
Відправлення
|
|
||||||
</span>
|
|
||||||
<span>
|
|
||||||
@route.Cities.First().ArrivalTime?.ToString("f").Split(",")[0].ToLower(),
|
|
||||||
@route.Cities.First().DepartureTime?.ToString("dd.MM.yyyy")
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<div class="route-date">
|
|
||||||
<span>Прибуття</span>
|
|
||||||
<span>
|
|
||||||
@route.Cities.Last().ArrivalTime?.ToString("f").Split(",")[0].ToLower(),
|
|
||||||
@route.Cities.Last().ArrivalTime?.ToString("dd.MM.yyyy")
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
<td class="time">
|
|
||||||
<div>@route.Cities.First().DepartureTime?.ToString("HH:mm")</div>
|
|
||||||
<div>@route.Cities.Last().ArrivalTime?.ToString("HH:mm")</div>
|
|
||||||
</td>
|
|
||||||
<td class="duration">
|
|
||||||
@{ TimeSpan? duration = route.Cities.Last().ArrivalTime - route.Cities.First().DepartureTime; }
|
|
||||||
@($"{duration?.TotalHours.ToString().Split(",")[0]}:{duration?.Minutes}")
|
|
||||||
</td>
|
|
||||||
<td class="capacity">
|
|
||||||
@(route.Capacity - route.Tickets.Count)
|
|
||||||
</td>
|
|
||||||
<td class="action">
|
|
||||||
<a class="link-btn-choose">Вибрати</a>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
<div class="search-error">
|
|
||||||
<p>По заданому Вами напрямку місць немає</p>
|
|
||||||
</div>
|
|
||||||
}
|
|
@ -1,118 +1,14 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
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;
|
|
||||||
|
|
||||||
namespace TicketOffice.Pages;
|
namespace TicketOffice.Pages;
|
||||||
|
|
||||||
public class IndexModel : PageModel
|
public class IndexModel : PageModel
|
||||||
{
|
{
|
||||||
private readonly ILogger<IndexModel> _logger;
|
public IndexModel()
|
||||||
private readonly TicketOfficeContext _context;
|
|
||||||
|
|
||||||
public IndexModel(ILogger<IndexModel> logger, TicketOfficeContext context)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
|
||||||
_context = context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Route> 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()
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
190
TicketOffice/Pages/Routes/Index.cshtml
Normal file
190
TicketOffice/Pages/Routes/Index.cshtml
Normal file
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="~/css/Routes.css" asp-append-version="true"/>
|
||||||
|
|
||||||
|
<div class="topnav">
|
||||||
|
<a href="/">Головна</a>
|
||||||
|
<a class="active" href="/routes">Пошук маршрутів</a>
|
||||||
|
<div class="topnav-right">
|
||||||
|
<a href="/auth/login">Авторизація</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wrapper">
|
||||||
|
|
||||||
|
<form class="search-block">
|
||||||
|
<div class="opt">
|
||||||
|
<div class="station">
|
||||||
|
<div class="title">
|
||||||
|
Звідки
|
||||||
|
</div>
|
||||||
|
<input type="text" autocomplete="off" asp-for="From">
|
||||||
|
</div>
|
||||||
|
<a class="change-dir"></a>
|
||||||
|
<div class="station">
|
||||||
|
<div class="title">
|
||||||
|
Куди
|
||||||
|
</div>
|
||||||
|
<input type="text" autocomplete="off" asp-for="To">
|
||||||
|
</div>
|
||||||
|
<div class="date">
|
||||||
|
<div class="title">
|
||||||
|
Дата відправлення
|
||||||
|
</div>
|
||||||
|
<input type="date" value="@Model.Date.ToString("yyyy-MM-dd")" asp-for="Date">
|
||||||
|
</div>
|
||||||
|
<div class="search-btn">
|
||||||
|
<input type="submit" class="search-btn" value="Пошук"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
@if (Model.Routes != null)
|
||||||
|
{
|
||||||
|
<div class="route-list">
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>
|
||||||
|
№ автобуса
|
||||||
|
@if (Model.SortString == "increasingNumber")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByNumber" asp-route-SortString="descendingNumber" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠕</a>
|
||||||
|
}
|
||||||
|
else if (Model.SortString == "descendingNumber")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByNumber" asp-route-SortString="increasingNumber" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠗</a>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByNumber" asp-route-SortString="increasingNumber" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>⮁</a>
|
||||||
|
}
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
Звідки / Куди
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
Дата
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
<div class="departure">
|
||||||
|
Відправлення
|
||||||
|
@if (Model.SortString == "increasingDeparture")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByDeparture" asp-route-SortString="descendingDeparture" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠕</a>
|
||||||
|
}
|
||||||
|
else if (Model.SortString == "descendingDeparture")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByDeparture" asp-route-SortString="increasingDeparture" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠗</a>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByDeparture" asp-route-SortString="increasingDeparture" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>⮁</a>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<div class="arrival">
|
||||||
|
Прибуття
|
||||||
|
@if (Model.SortString == "increasingArrival")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByArrival" asp-route-SortString="descendingArrival" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠕</a>
|
||||||
|
}
|
||||||
|
else if (Model.SortString == "descendingArrival")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByArrival" asp-route-SortString="increasingArrival" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠗</a>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByArrival" asp-route-SortString="increasingArrival" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>⮁</a>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
Тривалість
|
||||||
|
@if (Model.SortString == "increasingDuration")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByDuration" asp-route-SortString="descendingDuration" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠕</a>
|
||||||
|
}
|
||||||
|
else if (Model.SortString == "descendingDuration")
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByDuration" asp-route-SortString="increasingDuration" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>🠗</a>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<a class="link-btn-sort" asp-page-handler="SortByDuration" asp-route-SortString="increasingDuration" asp-route-From=@Model.From a in a asp-route-To=@Model.To asp-route-Date=@Model.Date.ToString("yyyy-MM-dd")>⮁</a>
|
||||||
|
}
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
Вільніих місць
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
Дії
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach (var route in Model.Routes)
|
||||||
|
{
|
||||||
|
<tr class="table-row">
|
||||||
|
<td class="num">
|
||||||
|
<div>@route.Number</div>
|
||||||
|
<div class="city-list-btn"><a class="city-list-btn">Маршрут</a></div>
|
||||||
|
</td>
|
||||||
|
<td class="city">
|
||||||
|
<div>@route.Cities.First().Name</div>
|
||||||
|
<div>@route.Cities.Last().Name</div>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div class="route-date">
|
||||||
|
<span>
|
||||||
|
Відправлення
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
@route.Cities.First().ArrivalTime?.ToString("f").Split(",")[0].ToLower(),
|
||||||
|
@route.Cities.First().DepartureTime?.ToString("dd.MM.yyyy")
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="route-date">
|
||||||
|
<span>Прибуття</span>
|
||||||
|
<span>
|
||||||
|
@route.Cities.Last().ArrivalTime?.ToString("f").Split(",")[0].ToLower(),
|
||||||
|
@route.Cities.Last().ArrivalTime?.ToString("dd.MM.yyyy")
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="time">
|
||||||
|
<div>@route.Cities.First().DepartureTime?.ToString("HH:mm")</div>
|
||||||
|
<div>@route.Cities.Last().ArrivalTime?.ToString("HH:mm")</div>
|
||||||
|
</td>
|
||||||
|
<td class="duration">
|
||||||
|
@{ TimeSpan? duration = route.Cities.Last().ArrivalTime - route.Cities.First().DepartureTime; }
|
||||||
|
@($"{duration?.TotalHours.ToString().Split(",")[0]}:{duration?.Minutes}")
|
||||||
|
</td>
|
||||||
|
<td class="capacity">
|
||||||
|
@(route.Capacity - route.Tickets.Count)
|
||||||
|
</td>
|
||||||
|
<td class="action">
|
||||||
|
<a class="link-btn-choose">Вибрати</a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div class="search-error">
|
||||||
|
<p>По заданому Вами напрямку місць немає</p>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
</div>
|
154
TicketOffice/Pages/Routes/Index.cshtml.cs
Normal file
154
TicketOffice/Pages/Routes/Index.cshtml.cs
Normal file
@ -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<Route> 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user