This commit is contained in:
Jim Paine 2020-05-05 16:32:34 +01:00
Родитель 9e6178a3ca
Коммит b18335069b
4 изменённых файлов: 83 добавлений и 48 удалений

Просмотреть файл

@ -49,35 +49,54 @@ namespace Project.Zap.Controllers
[HttpGet] [HttpGet]
public async Task<IActionResult> Index(SearchShiftViewModel search = null) public async Task<IActionResult> Index(SearchShiftViewModel search = null)
{ {
if(search == null) IEnumerable<Location> locations = await this.locationService.Get();
if (locations == null || !locations.Any())
{ {
IEnumerable<Location> locations = await this.locationService.Get(); this.logger.LogInformation("No locations, so redirecting to location view");
if (locations == null || !locations.Any()) return Redirect("/Locations");
{
this.logger.LogInformation("No locations, so redirecting to location view");
return Redirect("/Locations");
}
SearchShiftViewModel viewModel = await GetShifts(locations);
ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"];
return View("Index", viewModel);
} }
return await this.Search(search); return await this.Search(search);
} }
private async Task<SearchShiftViewModel> GetShifts(IEnumerable<Location> locations) private async Task<SearchShiftViewModel> GetShifts(IEnumerable<Location> locations, string sql, IDictionary<string, object> parameters, bool available = true)
{ {
IEnumerable<Shift> shifts = await this.shiftRepository.Get("SELECT * FROM c WHERE c.StartDateTime > @start", new Dictionary<string, object> { { "@start", DateTime.Now } }); IEnumerable<Shift> shifts = await this.shiftRepository.Get(sql, parameters);
IEnumerable<ShiftViewModel> shiftViewModels = shifts.Map(locations).Where(x => available ? x.Available > 0 : true);
SearchShiftViewModel viewModel = new SearchShiftViewModel SearchShiftViewModel viewModel = new SearchShiftViewModel
{ {
LocationNames = this.GetLocationNames(locations), LocationNames = this.GetLocationNames(locations),
Result = shifts.Map(locations) Result = shiftViewModels,
MapPoints = this.GetMapPoints(shiftViewModels, locations)
}; };
return viewModel; return viewModel;
} }
private IEnumerable<MapPointViewModel> GetMapPoints(IEnumerable<ShiftViewModel> shifts, IEnumerable<Location> locations)
{
List<MapPointViewModel> mapPoints = new List<MapPointViewModel>();
foreach(var location in shifts.GroupBy(x => x.LocationName))
{
Address address = locations.Where(x => x.Name == location.Key).Select(x => x.Address).FirstOrDefault();
MapPointViewModel mapPoint = new MapPointViewModel
{
Location = location.Key,
Address = address?.Text,
ZipOrPostcode = address?.ZipOrPostcode,
Quantity = location.Sum(x => x.Quantity),
Available = location.Sum(x => x.Available),
Point = location.Select(x => x.Point).FirstOrDefault(),
Start = location.Select(x => x.Start).FirstOrDefault()
};
mapPoints.Add(mapPoint);
}
return mapPoints;
}
private SelectList GetLocationNames(IEnumerable<Location> locations) private SelectList GetLocationNames(IEnumerable<Location> locations)
{ {
return new SelectList(locations.Select(x => x.Name).Distinct().Select(x => new { Value = x, Text = x }), "Value", "Text"); return new SelectList(locations.Select(x => x.Name).Distinct().Select(x => new { Value = x, Text = x }), "Value", "Text");
@ -118,10 +137,11 @@ namespace Project.Zap.Controllers
sql = sql + " AND ARRAY_CONTAINS(@locationIds, c.LocationId)"; sql = sql + " AND ARRAY_CONTAINS(@locationIds, c.LocationId)";
parameters.Add("@locationIds", locationIds); parameters.Add("@locationIds", locationIds);
} }
IEnumerable<Shift> shifts = await this.shiftRepository.Get(sql, parameters);
SearchShiftViewModel results = await this.GetShifts(locations, sql, parameters, search.Available);
search.LocationNames = this.GetLocationNames(locations); search.Result = results.Result;
search.Result = shifts.Map(locations).Where(x => search.Available ? x.Available > 0 : true); search.MapPoints = results.MapPoints;
search.LocationNames = results.LocationNames;
ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"]; ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"];
@ -263,7 +283,7 @@ namespace Project.Zap.Controllers
} }
ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"]; ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"];
return View("Index", await this.GetShifts(locations)); return View("Index", await this.GetShifts(locations, "SELECT * FROM c WHERE c.StartDateTime > @start", new Dictionary<string, object> { { "@start", DateTime.Now } }));
} }
Location location = await this.GetLocation(viewModel.LocationName); Location location = await this.GetLocation(viewModel.LocationName);
@ -291,7 +311,7 @@ namespace Project.Zap.Controllers
} }
ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"]; ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"];
return View("Index", await this.GetShifts(locations)); return View("Index", await this.GetShifts(locations, "SELECT * FROM c WHERE c.StartDateTime > @start", new Dictionary<string, object> { { "@start", DateTime.Now } }));
} }
shift.EmployeeId = id.Value; shift.EmployeeId = id.Value;

Просмотреть файл

@ -0,0 +1,20 @@
using System;
namespace Project.Zap.Models
{
public class MapPointViewModel
{
public string Location { get; set; }
public DateTime Start { get; set; }
public string Address { get; set; }
public string ZipOrPostcode { get; set; }
public int Quantity { get; set; }
public int Available { get; set; }
public PointViewModel Point { get; internal set; }
}
}

Просмотреть файл

@ -1,11 +1,8 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Project.Zap.Models namespace Project.Zap.Models
{ {
@ -36,10 +33,13 @@ namespace Project.Zap.Models
public IEnumerable<ShiftViewModel> Result { get; set; } public IEnumerable<ShiftViewModel> Result { get; set; }
public IEnumerable<MapPointViewModel> MapPoints { get; set; }
[BindProperty]
[Display(Name = "Available")] [Display(Name = "Available")]
public bool Available { get; set; } = true; public bool Available { get; set; } = true;
[BindProperty]
[Display(Name = "UseMyLocation")] [Display(Name = "UseMyLocation")]
public bool UseMyLocation { get; set; } = true; public bool UseMyLocation { get; set; } = true;

Просмотреть файл

@ -99,14 +99,14 @@
var markers = []; var markers = [];
function onLocationFound(e) { function onLocationFound(e) {
var radius = e.accuracy / 2; var radius = e.accuracy / 2;
var marker = L.marker(e.latlng) var marker = L.marker(e.latlng)
.addTo(map) .addTo(map)
.bindPopup("You are within " + radius + " meters from this point") .bindPopup("You are within " + radius + " meters from this point")
.openPopup(); .openPopup();
L.circle(e.latlng, radius).addTo(map); L.circle(e.latlng, radius).addTo(map);
markers.push(marker); markers.push(marker);
var group = new L.featureGroup(markers); var group = new L.featureGroup(markers);
map.fitBounds(group.getBounds()); map.fitBounds(group.getBounds());
@ -129,43 +129,38 @@
@if(Model.UseMyLocation) @if(Model.UseMyLocation)
{ {
<text> <text>
map.locate({ setView: false, enableHighAccuracy: true }); map.locate({ setView: false, enableHighAccuracy: true });
map.on('locationfound', onLocationFound); map.on('locationfound', onLocationFound);
</text> </text>
} }
var baseMaps = { var baseMaps = {
"Azure Roads": roads "Azure Roads": roads
}; };
L.control.layers(baseMaps, null, { collapsed: false }).addTo(map); L.control.layers(baseMaps, null, { collapsed: false }).addTo(map);
@foreach(var shift in Model.Result) @foreach(var mapPoint in Model.MapPoints)
{ {
<text> <text>
var popUpContent = L.Util.template(` var popUpContent = L.Util.template(`
<dl> <dl>
<dt>@Localizer["TableLocation"]</dt> <dt>@Localizer["TableLocation"]</dt>
<dd>@shift.LocationName</dd> <dd>@mapPoint.Location</dd>
<dt>@Localizer["TableStart"]</dt> <dt>@Localizer["TableStart"]</dt>
<dd>@shift.Start</dd> <dd>@mapPoint.Start</dd>
<dt>@Localizer["TableEnd"]</dt> <dt>@Localizer["AddressText"]</dt>
<dd>@shift.End</dd> <dd>@mapPoint.Address</dd>
<dt>@Localizer["TableWorkType"]</dt> <dt>@Localizer["ZipOrPostcode"]</dt>
<dd>@shift.WorkType</dd> <dd>@mapPoint.ZipOrPostcode</dd>
</dl> <dt>@Localizer["TableAvailable"]</dt>
@if ((await AuthorizationService.AuthorizeAsync(User, "OrgAManager")).Succeeded) <dd>@mapPoint.Available @mapPoint.Quantity</dd>
{ </dl>`);
<a class="btn btn-primary text-white" asp-controller="Shifts" asp-action="ViewShift" asp-route-LocationName="@shift.LocationName" asp-route-Start="@shift.Start.ToString("yyyy-MM-ddTHH:mm")" asp-route-End="@shift.End.ToString("yyyy-MM-ddTHH:mm")" asp-route-WorkType="@shift.WorkType" asp-route-Quantity="@shift.Quantity" asp-route-Available="@shift.Available">@Localizer["LinkView"]</a>
}
@if ((await AuthorizationService.AuthorizeAsync(User, "OrgBEmployee")).Succeeded)
{
<a class="btn btn-primary text-white" asp-controller="Shifts" asp-action="Book" asp-route-LocationName="@shift.LocationName" asp-route-Start="@shift.Start.ToString("yyyy-MM-ddTHH:mm")" asp-route-End="@shift.End.ToString("yyyy-MM-ddTHH:mm")" asp-route-WorkType="@shift.WorkType">@Localizer["LinkBook"]</a>
}`);
markers.push(L.marker([@shift.Point.Coordinates[0], @shift.Point.Coordinates[1]])
markers.push(L.marker([@mapPoint.Point.Coordinates[0], @mapPoint.Point.Coordinates[1]])
.addTo(map) .addTo(map)
.bindPopup(popUpContent) .bindPopup(popUpContent)
.openPopup()); .openPopup());