ui updates, use services
This commit is contained in:
parent
d1da303805
commit
43aa0aaa6e
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
@ -6,6 +6,16 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="users.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="users.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BinaryDad.Extensions" Version="21.4.20.3" />
|
<PackageReference Include="BinaryDad.Extensions" Version="21.4.20.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
|
||||||
|
@ -4,11 +4,11 @@ using System.Text.RegularExpressions;
|
|||||||
|
|
||||||
namespace BinaryDad.AacpsBusAlert.Services
|
namespace BinaryDad.AacpsBusAlert.Services
|
||||||
{
|
{
|
||||||
public class BusRouteService
|
public class HtmlScrapeBusRouteService : IBusRouteService
|
||||||
{
|
{
|
||||||
private readonly HttpClient httpClient;
|
private readonly HttpClient httpClient;
|
||||||
|
|
||||||
public BusRouteService(IHttpClientFactory httpClientFactory)
|
public HtmlScrapeBusRouteService(IHttpClientFactory httpClientFactory)
|
||||||
{
|
{
|
||||||
httpClient = httpClientFactory.CreateClient();
|
httpClient = httpClientFactory.CreateClient();
|
||||||
}
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace BinaryDad.AacpsBusAlert.Services
|
||||||
|
{
|
||||||
|
public interface IBusRouteService
|
||||||
|
{
|
||||||
|
Task<ICollection<BusRoute>> GetBusRoutesAsync();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
namespace BinaryDad.AacpsBusAlert.Services
|
||||||
|
{
|
||||||
|
public interface IUserService
|
||||||
|
{
|
||||||
|
ICollection<User> GetUsers();
|
||||||
|
User GetUser(Guid id);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace BinaryDad.AacpsBusAlert.Services
|
||||||
|
{
|
||||||
|
public class JsonUserService : IUserService
|
||||||
|
{
|
||||||
|
public ICollection<User> GetUsers()
|
||||||
|
{
|
||||||
|
var path = Path.Combine(AppContext.BaseDirectory, "users.json");
|
||||||
|
|
||||||
|
var usersJson = File.ReadAllText(path);
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<ICollection<User>>(usersJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
public User GetUser(Guid id)
|
||||||
|
{
|
||||||
|
return GetUsers().First(u => u.Id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
BinaryDad.AacpsBusAlert.Services/users.json
Normal file
18
BinaryDad.AacpsBusAlert.Services/users.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"Id": "b43f4f45-0f7b-4976-849d-8ad4b271199c",
|
||||||
|
"Email": "ryan@binarydad.com",
|
||||||
|
"Routes": [
|
||||||
|
{
|
||||||
|
"Id": "bfed1017-be53-42f9-bb46-ddbf27d4270f",
|
||||||
|
"BusNumber": 203,
|
||||||
|
"Label": "Con's bus'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Id": "bfed1017-be53-42f9-bb46-ddbf27d4270f",
|
||||||
|
"BusNumber": 365,
|
||||||
|
"Label": "Cam's bus'"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
@ -6,9 +6,9 @@ namespace BinaryDad.AacpsBusAlert.Web.Controllers
|
|||||||
[Route("api")]
|
[Route("api")]
|
||||||
public class ApiController : Controller
|
public class ApiController : Controller
|
||||||
{
|
{
|
||||||
private readonly BusRouteService busRouteService;
|
private readonly IBusRouteService busRouteService;
|
||||||
|
|
||||||
public ApiController(BusRouteService busRouteService)
|
public ApiController(IBusRouteService busRouteService)
|
||||||
{
|
{
|
||||||
this.busRouteService = busRouteService;
|
this.busRouteService = busRouteService;
|
||||||
}
|
}
|
||||||
|
@ -9,24 +9,33 @@ namespace BinaryDad.AacpsBusAlert.Web.Controllers
|
|||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
private readonly ILogger<HomeController> logger;
|
private readonly ILogger<HomeController> logger;
|
||||||
private readonly BusRouteService busRouteService;
|
private readonly IBusRouteService busRouteService;
|
||||||
|
private readonly IUserService userService;
|
||||||
|
|
||||||
public HomeController(ILogger<HomeController> logger, BusRouteService busRouteService)
|
public HomeController(ILogger<HomeController> logger, IBusRouteService busRouteService, IUserService userService)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.busRouteService = busRouteService;
|
this.busRouteService = busRouteService;
|
||||||
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Index()
|
public async Task<IActionResult> Index()
|
||||||
{
|
{
|
||||||
var userRoutes = new[] { 203, 365, 277, 269, 274 };
|
var user = userService.GetUser(Guid.Parse("b43f4f45-0f7b-4976-849d-8ad4b271199c"));
|
||||||
|
|
||||||
//var busRoutes = await CacheHelper.GetAsync("BusRoutes", () => busRouteService.GetBusRoutesAsync());
|
//var busRoutes = await CacheHelper.GetAsync("BusRoutes", () => busRouteService.GetBusRoutesAsync());
|
||||||
var busRoutes = await busRouteService.GetBusRoutesAsync();
|
var busRoutes = await busRouteService.GetBusRoutesAsync();
|
||||||
|
var userRouteIds = user.Routes.Select(r => r.BusNumber).ToList();
|
||||||
|
|
||||||
var matchedBusRoutes = busRoutes.Join(userRoutes, r => r.BusNumber, u => u, (r, u) => r).ToList();
|
var matchedBusRoutes = busRoutes.Join(userRouteIds, r => r.BusNumber, u => u, (r, u) => r).ToList();
|
||||||
|
|
||||||
return View(matchedBusRoutes);
|
var summary = new SummaryViewModel
|
||||||
|
{
|
||||||
|
User = user,
|
||||||
|
Routes = matchedBusRoutes
|
||||||
|
};
|
||||||
|
|
||||||
|
return View(summary);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IActionResult Privacy()
|
public IActionResult Privacy()
|
||||||
|
8
BinaryDad.AacpsBusAlert.Web/Models/SummaryViewModel.cs
Normal file
8
BinaryDad.AacpsBusAlert.Web/Models/SummaryViewModel.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace BinaryDad.AacpsBusAlert.Web.Models
|
||||||
|
{
|
||||||
|
public class SummaryViewModel
|
||||||
|
{
|
||||||
|
public User User { get; set; }
|
||||||
|
public ICollection<BusRoute> Routes { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
|
|
||||||
using BinaryDad.AacpsBusAlert.Services;
|
using BinaryDad.AacpsBusAlert.Services;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews();
|
||||||
builder.Services.AddTransient<BusRouteService>();
|
builder.Services.AddSingleton<IBusRouteService, HtmlScrapeBusRouteService>();
|
||||||
|
builder.Services.AddSingleton<IUserService, JsonUserService>();
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
@ -1,16 +1,38 @@
|
|||||||
@model ICollection<BusRoute>
|
@model SummaryViewModel
|
||||||
@{
|
@{
|
||||||
|
|
||||||
ViewData["Title"] = "Home Page";
|
ViewData["Title"] = "Home Page";
|
||||||
|
|
||||||
var busRoutes = Model.OrderBy(r => r.BusNumber);
|
var busRoutes = Model.Routes.OrderBy(r => r.BusNumber).ToList();
|
||||||
|
var hasDelays = busRoutes.Any();
|
||||||
|
|
||||||
|
var userBusRouteList = string.Join(", ", Model.User.Routes
|
||||||
|
.Select(r => r.BusNumber)
|
||||||
|
.OrderBy(r => r));
|
||||||
}
|
}
|
||||||
|
|
||||||
@foreach (var busRoute in busRoutes)
|
@if (hasDelays)
|
||||||
{
|
{
|
||||||
<div>
|
<div class="alert alert-danger">
|
||||||
<h3>@busRoute.BusNumber - @busRoute.Schools</h3>
|
<h3 class="alert-heading">Uh oh</h3>
|
||||||
<p>@busRoute.Impact</p>
|
We have @busRoutes.Count delays
|
||||||
<p>@busRoute.Schedules</p>
|
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div class="alert alert-success">
|
||||||
|
<h3 class="alert-heading">Good news!</h3>
|
||||||
|
No delays today!
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
<strong>My Buses: @userBusRouteList</strong>
|
||||||
|
|
||||||
|
<ul id="route-list">
|
||||||
|
@foreach (var busRoute in busRoutes)
|
||||||
|
{
|
||||||
|
<li>
|
||||||
|
<strong>@busRoute.BusNumber - @busRoute.Schools</strong>
|
||||||
|
<p>@busRoute.Impact @busRoute.Schedules</p>
|
||||||
|
</li>
|
||||||
|
}
|
||||||
|
</ul>
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
@{
|
|
||||||
ViewData["Title"] = "Privacy Policy";
|
|
||||||
}
|
|
||||||
<h1>@ViewData["Title"]</h1>
|
|
||||||
|
|
||||||
<p>Use this page to detail your site's privacy policy.</p>
|
|
@ -16,3 +16,9 @@ html {
|
|||||||
body {
|
body {
|
||||||
margin-bottom: 60px;
|
margin-bottom: 60px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#route-list {
|
||||||
|
margin: 20px 0 0;
|
||||||
|
padding: 0;
|
||||||
|
list-style-type: none;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user