Commit c72a3b94 authored by Andres Käver's avatar Andres Käver
Browse files

session fix in admin

parent 4379e338
Pipeline #834 passed with stages
in 1 minute and 36 seconds
......@@ -26,5 +26,64 @@ namespace BLL.App.Services
{
return (await Repository.GetAllForViewAsync(minLocationsCount, minDuration, minDistance, fromDateTime, toDateTime)).Select(e => Mapper.MapGpsSessionView(e));
}
public async Task UpdateStatisticsAsync(Guid id)
{
// take the session, with all the locations. recalculate stats.
var session = await UOW.GpsSessions.GetFirstWithAllLocationsAsync(id);
if (session == null) return;
// reset stats
session.Distance = 0;
session.Duration = 0;
session.Descent = 0;
session.Climb = 0;
session.Speed = 0;
// calculate duration - in seconds
if (session.GpsLocations!.Count > 1)
{
session.Duration = (session.GpsLocations.Last().RecordedAt - session.GpsLocations.First().RecordedAt).TotalSeconds;
}
DAL.App.DTO.GpsLocation? prevLocation = null;
foreach (var gpsLocation in session.GpsLocations!)
{
if (prevLocation != null)
{
var heightDif = gpsLocation.Altitude - prevLocation.Altitude;
if (heightDif > 0)
{
session.Climb += heightDif;
}
else
{
session.Descent += Math.Abs(heightDif);
}
session.Distance += GetDistance(gpsLocation, prevLocation);
}
prevLocation = gpsLocation;
}
// remove list
session.GpsLocations = null;
await UOW.GpsSessions.UpdateAsync(session);
}
private static double GetDistance(DAL.App.DTO.GpsLocation gpsLocation, DAL.App.DTO.GpsLocation lastLocation)
{
var d1 = gpsLocation.Latitude * (Math.PI / 180.0);
var num1 = gpsLocation.Longitude * (Math.PI / 180.0);
var d2 = lastLocation.Latitude * (Math.PI / 180.0);
var num2 = lastLocation.Longitude * (Math.PI / 180.0) - num1;
var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
return Math.Abs(6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3))));
}
}
}
\ No newline at end of file
......@@ -14,5 +14,7 @@ namespace Contracts.BLL.App
ILangStrTranslationService LangStrTranslation { get; }
ITrackPointService TrackPoints { get; }
ITrackService Tracks { get; }
}
}
\ No newline at end of file
using System;
using System.Threading.Tasks;
using BLL.App.DTO;
using Contracts.BLL.Base.Services;
using Contracts.DAL.App.Repositories;
......@@ -6,5 +8,6 @@ namespace Contracts.BLL.App.Services
{
public interface IGpsSessionService : IBaseEntityService<GpsSession>, IGpsSessionRepositoryCustom<GpsSessionView>
{
Task UpdateStatisticsAsync(Guid id);
}
}
\ No newline at end of file
using System;
using System.Threading.Tasks;
using Contracts.DAL.Base.Repositories;
using DAL.App.DTO;
......@@ -5,5 +7,6 @@ namespace Contracts.DAL.App.Repositories
{
public interface IGpsSessionRepository : IBaseRepository<GpsSession>, IGpsSessionRepositoryCustom
{
Task<GpsSession> GetFirstWithAllLocationsAsync(Guid id);
}
}
\ No newline at end of file
......@@ -78,5 +78,18 @@ namespace DAL.App.EF.Repositories
return result;
}
public async Task<GpsSession> GetFirstWithAllLocationsAsync(Guid id)
{
var domainGpsSession = await RepoDbSet.AsNoTracking()
.FirstOrDefaultAsync(a => a.Id == id);
domainGpsSession.GpsLocations = await RepoDbContext.GpsLocations.AsNoTracking()
.Where(l => l.GpsSessionId == id)
.OrderByDescending(l => l.RecordedAt)
.ToListAsync();
return Mapper.Map(domainGpsSession);
}
}
}
\ No newline at end of file
......@@ -32,6 +32,18 @@ namespace WebApp.Controllers
return View(result);
}
public async Task<IActionResult> UpdateStatistics(Guid? id)
{
if (id != null)
{
await _bll.GpsSessions.UpdateStatisticsAsync(id.Value);
}
return RedirectToAction(nameof(Index));
}
[AllowAnonymous]
// GET: GpsSessions/Details/5
public async Task<IActionResult> Details(Guid? id)
......@@ -68,7 +80,10 @@ namespace WebApp.Controllers
[Authorize(Roles = "admin")]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Name,Description,RecordedAt,Duration,Speed,Distance,Climb,Descent,PaceMin,PaceMax,GpsSessionTypeId,AppUserId,CreatedBy,CreatedAt,ChangedBy,ChangedAt,Id")] GpsSession gpsSession)
public async Task<IActionResult> Create(
[Bind(
"Name,Description,RecordedAt,Duration,Speed,Distance,Climb,Descent,PaceMin,PaceMax,GpsSessionTypeId,AppUserId,CreatedBy,CreatedAt,ChangedBy,ChangedAt,Id")]
GpsSession gpsSession)
{
if (ModelState.IsValid)
{
......@@ -77,8 +92,10 @@ namespace WebApp.Controllers
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["AppUserId"] = new SelectList(_context.Users, "Id", "FirstName", gpsSession.AppUserId);
ViewData["GpsSessionTypeId"] = new SelectList(_context.GpsSessionTypes, "Id", "Id", gpsSession.GpsSessionTypeId);
ViewData["GpsSessionTypeId"] =
new SelectList(_context.GpsSessionTypes, "Id", "Id", gpsSession.GpsSessionTypeId);
return View(gpsSession);
}
......@@ -95,8 +112,10 @@ namespace WebApp.Controllers
{
return NotFound();
}
ViewData["AppUserId"] = new SelectList(_context.Users, "Id", "FirstName", gpsSession.AppUserId);
ViewData["GpsSessionTypeId"] = new SelectList(_context.GpsSessionTypes, "Id", "Id", gpsSession.GpsSessionTypeId);
ViewData["GpsSessionTypeId"] =
new SelectList(_context.GpsSessionTypes, "Id", "Id", gpsSession.GpsSessionTypeId);
return View(gpsSession);
}
......@@ -105,7 +124,10 @@ namespace WebApp.Controllers
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Name,Description,RecordedAt,Duration,Speed,Distance,Climb,Descent,PaceMin,PaceMax,GpsSessionTypeId,AppUserId,CreatedBy,CreatedAt,ChangedBy,ChangedAt,Id")] GpsSession gpsSession)
public async Task<IActionResult> Edit(Guid id,
[Bind(
"Name,Description,RecordedAt,Duration,Speed,Distance,Climb,Descent,PaceMin,PaceMax,GpsSessionTypeId,AppUserId,CreatedBy,CreatedAt,ChangedBy,ChangedAt,Id")]
GpsSession gpsSession)
{
if (id != gpsSession.Id)
{
......@@ -130,10 +152,13 @@ namespace WebApp.Controllers
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["AppUserId"] = new SelectList(_context.Users, "Id", "FirstName", gpsSession.AppUserId);
ViewData["GpsSessionTypeId"] = new SelectList(_context.GpsSessionTypes, "Id", "Id", gpsSession.GpsSessionTypeId);
ViewData["GpsSessionTypeId"] =
new SelectList(_context.GpsSessionTypes, "Id", "Id", gpsSession.GpsSessionTypeId);
return View(gpsSession);
}
......@@ -173,4 +198,4 @@ namespace WebApp.Controllers
return _context.GpsSessions.Any(e => e.Id == id);
}
}
}
}
\ No newline at end of file
......@@ -36,6 +36,7 @@
{
<a asp-action="Edit" class="btn btn-primary" asp-route-id="@item.Id">Edit</a>
<a asp-action="Delete" class="btn btn-primary" asp-route-id="@item.Id">Delete</a>
<a asp-action="UpdateStatistics" class="btn btn-primary" asp-route-id="@item.Id">Update</a>
}
</div>
</div>
......@@ -43,91 +44,3 @@
</div>
}
</div>
@*
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.RecordedAt)
</th>
<th>
@Html.DisplayNameFor(model => model.Duration)
</th>
<th>
@Html.DisplayNameFor(model => model.Speed)
</th>
<th>
@Html.DisplayNameFor(model => model.Distance)
</th>
<th>
@Html.DisplayNameFor(model => model.Climb)
</th>
<th>
@Html.DisplayNameFor(model => model.Descent)
</th>
<th>
@Html.DisplayNameFor(model => model.PaceMin)
</th>
<th>
@Html.DisplayNameFor(model => model.PaceMax)
</th>
<th>
@Html.DisplayNameFor(model => model.GpsSessionType)
</th>
<th>
@Html.DisplayNameFor(model => model.AppUser)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.RecordedAt)
</td>
<td>
@Html.DisplayFor(modelItem => item.Duration)
</td>
<td>
@Html.DisplayFor(modelItem => item.Speed)
</td>
<td>
@Html.DisplayFor(modelItem => item.Distance)
</td>
<td>
@Html.DisplayFor(modelItem => item.Climb)
</td>
<td>
@Html.DisplayFor(modelItem => item.Descent)
</td>
<td>
@Html.DisplayFor(modelItem => item.PaceMin)
</td>
<td>
@Html.DisplayFor(modelItem => item.PaceMax)
</td>
<td>
@(item.GpsSessionType!.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.AppUser!.FirstLastName)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
*@
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment