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]
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();
if (locations == null || !locations.Any())
{
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);
this.logger.LogInformation("No locations, so redirecting to location view");
return Redirect("/Locations");
}
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
{
LocationNames = this.GetLocationNames(locations),
Result = shifts.Map(locations)
Result = shiftViewModels,
MapPoints = this.GetMapPoints(shiftViewModels, locations)
};
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)
{
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)";
parameters.Add("@locationIds", locationIds);
}
IEnumerable<Shift> shifts = await this.shiftRepository.Get(sql, parameters);
search.LocationNames = this.GetLocationNames(locations);
search.Result = shifts.Map(locations).Where(x => search.Available ? x.Available > 0 : true);
SearchShiftViewModel results = await this.GetShifts(locations, sql, parameters, search.Available);
search.Result = results.Result;
search.MapPoints = results.MapPoints;
search.LocationNames = results.LocationNames;
ViewData["AzureMapsKey"] = this.configuration["AzureMapsSubscriptionKey"];
@ -263,7 +283,7 @@ namespace Project.Zap.Controllers
}
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);
@ -291,7 +311,7 @@ namespace Project.Zap.Controllers
}
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;

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

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

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

@ -99,14 +99,14 @@
var markers = [];
function onLocationFound(e) {
var radius = e.accuracy / 2;
var marker = L.marker(e.latlng)
.addTo(map)
.bindPopup("You are within " + radius + " meters from this point")
.openPopup();
L.circle(e.latlng, radius).addTo(map);
markers.push(marker);
var group = new L.featureGroup(markers);
map.fitBounds(group.getBounds());
@ -129,43 +129,38 @@
@if(Model.UseMyLocation)
{
<text>
<text>
map.locate({ setView: false, enableHighAccuracy: true });
map.on('locationfound', onLocationFound);
map.on('locationfound', onLocationFound);
</text>
}
}
var baseMaps = {
"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>
var popUpContent = L.Util.template(`
<dl>
<dt>@Localizer["TableLocation"]</dt>
<dd>@shift.LocationName</dd>
<dd>@mapPoint.Location</dd>
<dt>@Localizer["TableStart"]</dt>
<dd>@shift.Start</dd>
<dt>@Localizer["TableEnd"]</dt>
<dd>@shift.End</dd>
<dt>@Localizer["TableWorkType"]</dt>
<dd>@shift.WorkType</dd>
</dl>
@if ((await AuthorizationService.AuthorizeAsync(User, "OrgAManager")).Succeeded)
{
<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>
}`);
<dd>@mapPoint.Start</dd>
<dt>@Localizer["AddressText"]</dt>
<dd>@mapPoint.Address</dd>
<dt>@Localizer["ZipOrPostcode"]</dt>
<dd>@mapPoint.ZipOrPostcode</dd>
<dt>@Localizer["TableAvailable"]</dt>
<dd>@mapPoint.Available @mapPoint.Quantity</dd>
</dl>`);
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)
.bindPopup(popUpContent)
.openPopup());