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

location post bll - calculate some session metrics

parent 3b5879b1
Pipeline #821 passed with stages
in 1 minute and 28 seconds
using AutoMapper;
using BLL.Base.Mappers;
namespace BLL.App.Mappers
{
public class BLLMapper<TLeftObject, TRightObject> : BaseMapper<TLeftObject, TRightObject>
where TRightObject : class?, new()
where TLeftObject : class?, new()
{
public BLLMapper() : base()
{
// add more mappings
MapperConfigurationExpression.CreateMap<DAL.App.DTO.GpsSession, BLL.App.DTO.GpsSession>();
MapperConfigurationExpression.CreateMap<BLL.App.DTO.GpsSession, DAL.App.DTO.GpsSession>();
Mapper = new Mapper(new MapperConfiguration(MapperConfigurationExpression));
}
}
}
\ No newline at end of file
...@@ -4,7 +4,7 @@ using BLLAppDTO=BLL.App.DTO; ...@@ -4,7 +4,7 @@ using BLLAppDTO=BLL.App.DTO;
using DALAppDTO=DAL.App.DTO; using DALAppDTO=DAL.App.DTO;
namespace BLL.App.Mappers namespace BLL.App.Mappers
{ {
public class GpsLocationServiceMapper : BaseMapper<DALAppDTO.GpsLocation, BLLAppDTO.GpsLocation>, IGpsLocationServiceMapper public class GpsLocationServiceMapper : BLLMapper<DALAppDTO.GpsLocation, BLLAppDTO.GpsLocation>, IGpsLocationServiceMapper
{ {
} }
......
...@@ -9,6 +9,7 @@ using Contracts.BLL.App.Mappers; ...@@ -9,6 +9,7 @@ using Contracts.BLL.App.Mappers;
using Contracts.BLL.App.Services; using Contracts.BLL.App.Services;
using Contracts.DAL.App; using Contracts.DAL.App;
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace BLL.App.Services namespace BLL.App.Services
{ {
...@@ -25,5 +26,48 @@ namespace BLL.App.Services ...@@ -25,5 +26,48 @@ namespace BLL.App.Services
{ {
return (await Repository.GetAllAsync(gpsSessionId, userId, noTracking)).Select(e => Mapper.Map(e)); return (await Repository.GetAllAsync(gpsSessionId, userId, noTracking)).Select(e => Mapper.Map(e));
} }
public virtual async Task<GpsLocation> AddAndUpdateSessionAsync(GpsLocation gpsLocation)
{
// get the last location from uow
// calculate updated metrics for session
// update session
// add location
var gpsSession = await UOW.GpsSessions.FirstOrDefaultAsync(gpsLocation.GpsSessionId, gpsLocation.AppUserId);
var lastLocation = await UOW.GpsLocations.LastInSessionAsync(gpsSession.Id);
// calculate the metrics
var distance = getDistance(gpsLocation, lastLocation);
var vertical = getVerticalDistance(gpsLocation, lastLocation);
gpsSession.Distance += distance;
if (vertical < 0)
{
gpsSession.Descent += Math.Abs(vertical);
} else if (vertical > 0)
{
gpsSession.Descent += vertical;
}
await UOW.GpsSessions.UpdateAsync(gpsSession);
return base.Add(gpsLocation);
}
private double getDistance(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))));
}
private double getVerticalDistance(GpsLocation gpsLocation, DAL.App.DTO.GpsLocation lastLocation)
{
return gpsLocation.Altitude - lastLocation.Altitude;
}
} }
} }
\ No newline at end of file
...@@ -62,7 +62,7 @@ namespace BLL.Base.Services ...@@ -62,7 +62,7 @@ namespace BLL.Base.Services
return result; return result;
} }
public TBLLEntity Add(TBLLEntity entity) public virtual TBLLEntity Add(TBLLEntity entity)
{ {
var dalEntity = Mapper.Map(entity); var dalEntity = Mapper.Map(entity);
var trackedDALEntity = Repository.Add(dalEntity); var trackedDALEntity = Repository.Add(dalEntity);
......
using System.Threading.Tasks;
using BLL.App.DTO; using BLL.App.DTO;
using Contracts.BLL.Base.Services; using Contracts.BLL.Base.Services;
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
...@@ -6,5 +7,6 @@ namespace Contracts.BLL.App.Services ...@@ -6,5 +7,6 @@ namespace Contracts.BLL.App.Services
{ {
public interface IGpsLocationService : IBaseEntityService<GpsLocation>, IGpsLocationRepositoryCustom<GpsLocation> public interface IGpsLocationService : IBaseEntityService<GpsLocation>, IGpsLocationRepositoryCustom<GpsLocation>
{ {
Task<GpsLocation> AddAndUpdateSessionAsync(GpsLocation gpsLocation);
} }
} }
\ No newline at end of file
using System;
using System.Threading.Tasks;
using Contracts.DAL.Base.Repositories; using Contracts.DAL.Base.Repositories;
using DAL.App.DTO; using DAL.App.DTO;
...@@ -5,6 +7,6 @@ namespace Contracts.DAL.App.Repositories ...@@ -5,6 +7,6 @@ namespace Contracts.DAL.App.Repositories
{ {
public interface IGpsLocationRepository : IBaseRepository<GpsLocation>, IGpsLocationRepositoryCustom public interface IGpsLocationRepository : IBaseRepository<GpsLocation>, IGpsLocationRepositoryCustom
{ {
Task<GpsLocation> LastInSessionAsync(Guid gpsSessionId, bool noTracking = true);
} }
} }
\ No newline at end of file
...@@ -13,6 +13,9 @@ namespace DAL.App.EF.Mappers ...@@ -13,6 +13,9 @@ namespace DAL.App.EF.Mappers
MapperConfigurationExpression.CreateMap<Domain.App.Identity.AppUser, DAL.App.DTO.Identity.AppUser>(); MapperConfigurationExpression.CreateMap<Domain.App.Identity.AppUser, DAL.App.DTO.Identity.AppUser>();
MapperConfigurationExpression.CreateMap<Domain.App.GpsSessionType, DAL.App.DTO.GpsSessionType>(); MapperConfigurationExpression.CreateMap<Domain.App.GpsSessionType, DAL.App.DTO.GpsSessionType>();
MapperConfigurationExpression.CreateMap<Domain.App.GpsSession, DAL.App.DTO.GpsSession>();
MapperConfigurationExpression.CreateMap<DAL.App.DTO.GpsSession, Domain.App.GpsSession>();
Mapper = new Mapper(new MapperConfiguration(MapperConfigurationExpression)); Mapper = new Mapper(new MapperConfiguration(MapperConfigurationExpression));
} }
} }
......
...@@ -4,8 +4,8 @@ using System.Linq; ...@@ -4,8 +4,8 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using DAL.App.DTO; using DAL.App.DTO;
using DAL.App.EF.Mappers;
using DAL.Base.EF.Repositories; using DAL.Base.EF.Repositories;
using DAL.Base.Mappers;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace DAL.App.EF.Repositories namespace DAL.App.EF.Repositories
...@@ -15,7 +15,7 @@ namespace DAL.App.EF.Repositories ...@@ -15,7 +15,7 @@ namespace DAL.App.EF.Repositories
IGpsLocationRepository IGpsLocationRepository
{ {
public GpsLocationRepository(AppDbContext repoDbContext) : base(repoDbContext, public GpsLocationRepository(AppDbContext repoDbContext) : base(repoDbContext,
new BaseMapper<Domain.App.GpsLocation, DTO.GpsLocation>()) new DALMapper<Domain.App.GpsLocation, DTO.GpsLocation>())
{ {
} }
...@@ -28,5 +28,13 @@ namespace DAL.App.EF.Repositories ...@@ -28,5 +28,13 @@ namespace DAL.App.EF.Repositories
var result = domainEntities.Select(e => Mapper.Map(e)); var result = domainEntities.Select(e => Mapper.Map(e));
return result; return result;
} }
public async Task<GpsLocation> LastInSessionAsync(Guid gpsSessionId, bool noTracking = true)
{
var query = PrepareQuery(null, noTracking);
query = query.Where(a => a.GpsSessionId == gpsSessionId).OrderBy(a => a.RecordedAt);
var domainEntity = await query.FirstOrDefaultAsync();
return Mapper.Map(domainEntity);
}
} }
} }
\ No newline at end of file
...@@ -4,6 +4,7 @@ using System.Threading.Tasks; ...@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using Contracts.DAL.Base.Mappers; using Contracts.DAL.Base.Mappers;
using DAL.App.DTO; using DAL.App.DTO;
using DAL.App.EF.Mappers;
using DAL.Base.EF.Repositories; using DAL.Base.EF.Repositories;
using DAL.Base.Mappers; using DAL.Base.Mappers;
using Domain; using Domain;
...@@ -16,7 +17,7 @@ namespace DAL.App.EF.Repositories ...@@ -16,7 +17,7 @@ namespace DAL.App.EF.Repositories
IGpsLocationTypeRepository IGpsLocationTypeRepository
{ {
public GpsLocationTypeRepository(AppDbContext repoDbContext) : base(repoDbContext, public GpsLocationTypeRepository(AppDbContext repoDbContext) : base(repoDbContext,
new BaseMapper<Domain.App.GpsLocationType, DTO.GpsLocationType>()) new DALMapper<Domain.App.GpsLocationType, DTO.GpsLocationType>())
{ {
} }
......
...@@ -3,6 +3,7 @@ using System.Linq; ...@@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using Contracts.DAL.Base.Mappers; using Contracts.DAL.Base.Mappers;
using DAL.App.EF.Mappers;
using DAL.Base.EF.Repositories; using DAL.Base.EF.Repositories;
using DAL.Base.Mappers; using DAL.Base.Mappers;
using Domain.App; using Domain.App;
...@@ -15,7 +16,7 @@ namespace DAL.App.EF.Repositories ...@@ -15,7 +16,7 @@ namespace DAL.App.EF.Repositories
EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.GpsSessionType, DAL.App.DTO.GpsSessionType>, EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.GpsSessionType, DAL.App.DTO.GpsSessionType>,
IGpsSessionTypeRepository IGpsSessionTypeRepository
{ {
public GpsSessionTypeRepository(AppDbContext repoDbContext) : base(repoDbContext, new BaseMapper<Domain.App.GpsSessionType, DTO.GpsSessionType>()) public GpsSessionTypeRepository(AppDbContext repoDbContext) : base(repoDbContext, new DALMapper<Domain.App.GpsSessionType, DTO.GpsSessionType>())
{ {
} }
......
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using DAL.App.EF.Mappers;
using DAL.Base.EF.Repositories; using DAL.Base.EF.Repositories;
using DAL.Base.Mappers; using DAL.Base.Mappers;
using DomainApp=Domain.App; using DomainApp = Domain.App;
using DALAppDTO = DAL.App.DTO; using DALAppDTO = DAL.App.DTO;
namespace DAL.App.EF.Repositories namespace DAL.App.EF.Repositories
{ {
public class LangStrRepository: public class LangStrRepository :
EFBaseRepository<AppDbContext, DomainApp.Identity.AppUser, DomainApp.LangStr, DALAppDTO.LangStr>, EFBaseRepository<AppDbContext, DomainApp.Identity.AppUser, DomainApp.LangStr, DALAppDTO.LangStr>,
ILangStrRepository ILangStrRepository
{ {
public LangStrRepository(AppDbContext repoDbContext) : base(repoDbContext, new BaseMapper<DomainApp.LangStr, DALAppDTO.LangStr>()) public LangStrRepository(AppDbContext repoDbContext) : base(repoDbContext,
new DALMapper<DomainApp.LangStr, DALAppDTO.LangStr>())
{ {
} }
} }
......
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using Contracts.DAL.Base.Mappers; using Contracts.DAL.Base.Mappers;
using DAL.App.EF.Mappers;
using DAL.Base.EF.Repositories; using DAL.Base.EF.Repositories;
using DAL.Base.Mappers; using DAL.Base.Mappers;
using Domain.App; using Domain.App;
namespace DAL.App.EF.Repositories namespace DAL.App.EF.Repositories
{ {
public class LangStrTranslationRepository: public class LangStrTranslationRepository :
EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.LangStrTranslation, DAL.App.DTO.LangStrTranslation>, EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.LangStrTranslation,
DAL.App.DTO.LangStrTranslation>,
ILangStrTranslationRepository ILangStrTranslationRepository
{ {
public LangStrTranslationRepository(AppDbContext repoDbContext) : base(repoDbContext, new BaseMapper<Domain.App.LangStrTranslation, DTO.LangStrTranslation>()) public LangStrTranslationRepository(AppDbContext repoDbContext) : base(repoDbContext,
new DALMapper<Domain.App.LangStrTranslation, DTO.LangStrTranslation>())
{ {
} }
} }
......
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using Contracts.DAL.Base.Mappers; using Contracts.DAL.Base.Mappers;
using DAL.App.EF.Mappers;
using DAL.Base.EF.Repositories; using DAL.Base.EF.Repositories;
using DAL.Base.Mappers; using DAL.Base.Mappers;
using Domain.App; using Domain.App;
...@@ -10,7 +11,7 @@ namespace DAL.App.EF.Repositories ...@@ -10,7 +11,7 @@ namespace DAL.App.EF.Repositories
EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.TrackPoint, DAL.App.DTO.TrackPoint>, EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.TrackPoint, DAL.App.DTO.TrackPoint>,
ITrackPointRepository ITrackPointRepository
{ {
public TrackPointRepository(AppDbContext repoDbContext) : base(repoDbContext, new BaseMapper<Domain.App.TrackPoint, DTO.TrackPoint>()) public TrackPointRepository(AppDbContext repoDbContext) : base(repoDbContext, new DALMapper<Domain.App.TrackPoint, DTO.TrackPoint>())
{ {
} }
} }
......
using Contracts.DAL.App.Repositories; using Contracts.DAL.App.Repositories;
using Contracts.DAL.Base.Mappers; using Contracts.DAL.Base.Mappers;
using DAL.App.EF.Mappers;
using DAL.Base.EF.Repositories; using DAL.Base.EF.Repositories;
using DAL.Base.Mappers; using DAL.Base.Mappers;
using Domain.App; using Domain.App;
...@@ -10,7 +11,7 @@ namespace DAL.App.EF.Repositories ...@@ -10,7 +11,7 @@ namespace DAL.App.EF.Repositories
EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.Track, DAL.App.DTO.Track>, EFBaseRepository<AppDbContext, Domain.App.Identity.AppUser, Domain.App.Track, DAL.App.DTO.Track>,
ITrackRepository ITrackRepository
{ {
public TrackRepository(AppDbContext repoDbContext) : base(repoDbContext, new BaseMapper<Domain.App.Track, DTO.Track>()) public TrackRepository(AppDbContext repoDbContext) : base(repoDbContext, new DALMapper<Domain.App.Track, DTO.Track>())
{ {
} }
} }
......
...@@ -5,12 +5,12 @@ using Contracts.Domain; ...@@ -5,12 +5,12 @@ using Contracts.Domain;
namespace Domain.Base namespace Domain.Base
{ {
public abstract class DomainEntityIdMetadata : DomainEntityIdMetadata<Guid>, IDomainEntityId, IDomainEntityMetadata public abstract class DomainEntityIdMetadata : DomainEntityIdMetadata<Guid>, IDomainEntityId
{ {
} }
public abstract class DomainEntityIdMetadata<TKey> : DomainEntityId<TKey> public abstract class DomainEntityIdMetadata<TKey> : DomainEntityId<TKey>, IDomainEntityMetadata
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
[MaxLength(256)] [MaxLength(256)]
......
...@@ -5,15 +5,15 @@ using Microsoft.AspNetCore.Identity; ...@@ -5,15 +5,15 @@ using Microsoft.AspNetCore.Identity;
namespace Domain.Base namespace Domain.Base
{ {
public abstract class DomainEntityIdUser<TUser> : DomainEntityIdUser<Guid, TUser>,IDomainEntityId, IDomainEntityUser<TUser> public abstract class DomainEntityIdUser<TUser> : DomainEntityIdUser<Guid, TUser>, IDomainEntityId,
IDomainEntityUser<TUser>
where TUser : IdentityUser<Guid> where TUser : IdentityUser<Guid>
{ {
} }
public abstract class DomainEntityIdUser<TKey, TUser> : DomainEntityId<TKey>, public abstract class DomainEntityIdUser<TKey, TUser> : DomainEntityId<TKey>, IDomainEntityUser<TKey, TUser>
IDomainEntityUser<TKey, TUser>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
where TUser: IdentityUser<TKey> where TUser : IdentityUser<TKey>
{ {
public TKey AppUserId { get; set; } = default!; public TKey AppUserId { get; set; } = default!;
......
...@@ -56,7 +56,7 @@ namespace WebApp.ApiControllers._1._0 ...@@ -56,7 +56,7 @@ namespace WebApp.ApiControllers._1._0
if (!await _bll.GpsSessions.ExistsAsync(gpsSessionId.Value)) if (!await _bll.GpsSessions.ExistsAsync(gpsSessionId.Value))
{ {
return NotFound(new V1DTO.MessageDTO($"GpsSession with id {gpsSessionId} not found")); return NotFound(new V1DTO.MessageDTO($"GpsSession with id {gpsSessionId} was not found"));
} }
return Ok((await _bll.GpsLocations.GetAllAsync(gpsSessionId.Value)).Select(e => _mapper.Map(e))); return Ok((await _bll.GpsLocations.GetAllAsync(gpsSessionId.Value)).Select(e => _mapper.Map(e)));
...@@ -128,7 +128,7 @@ namespace WebApp.ApiControllers._1._0 ...@@ -128,7 +128,7 @@ namespace WebApp.ApiControllers._1._0
} }
var bllEntity = _mapper.Map(gpsLocation); var bllEntity = _mapper.Map(gpsLocation);
_bll.GpsLocations.Add(bllEntity); await _bll.GpsLocations.AddAndUpdateSessionAsync(bllEntity);
await _bll.SaveChangesAsync(); await _bll.SaveChangesAsync();
gpsLocation.Id = bllEntity.Id; gpsLocation.Id = bllEntity.Id;
......
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