Files
SkydiveLogs/Back/skydiveLogs-api.DomainBusiness/StatsForLastMonthByDzService.cs

396 lines
16 KiB
C#

using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories;
using System.Collections.Generic;
using System.Linq;
namespace skydiveLogs_api.DomainBusiness
{
public class StatsForLastMonthByDzService : IStatsForLastMonthByDzService
{
#region Public Constructors
public StatsForLastMonthByDzService(IJumpService jumpService,
IIdentityService identityService,
IStatsByDzRepository statsByDzRepository,
IStatsByGearRepository statsByGearRepository,
IStatsByJumpTypeRepository statsByJumpTypeRepository,
IStatsByYearByJumpTypeRepository statsByYearByJumpTypeRepository,
IStatsByYearRepository statsByYearRepository,
IStatsForLastMonthByDzRepository statsForLastMonthByDzRepository,
IStatsForLastMonthByJumpTypeRepository statsForLastMonthByJumpTypeRepository,
IStatsForLastYearByDzRepository statsForLastYearByDzRepository,
IStatsForLastYearByJumpTypeRepository statsForLastYearByJumpTypeRepository)
{
_jumpService = jumpService;
_identityService = identityService;
_statsByDzRepository = statsByDzRepository;
_statsByGearRepository = statsByGearRepository;
_statsByJumpTypeRepository = statsByJumpTypeRepository;
_statsByYearByJumpTypeRepository = statsByYearByJumpTypeRepository;
_statsByYearRepository = statsByYearRepository;
_statsForLastMonthByDzRepository = statsForLastMonthByDzRepository;
_statsForLastMonthByJumpTypeRepository = statsForLastMonthByJumpTypeRepository;
_statsForLastYearByDzRepository = statsForLastYearByDzRepository;
_statsForLastYearByJumpTypeRepository = statsForLastYearByJumpTypeRepository;
}
#endregion Public Constructors
#region Public Methods
public SimpleSummary GetSimpleSummary()
{
var allJumps = _jumpService.GetAllJumps();
var results = new SimpleSummary();
if (allJumps.Any())
{
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
results = new SimpleSummary
{
LastJump = lastJump,
TotalJumps = allJumps.Count(),
TotalCutaways = allJumps.Where(j => j.WithCutaway).Count()
};
}
return results;
}
public IEnumerable<Statistic> GetStatsByAircraft()
{
var allStats = GetAllStats();
if (!allStats.ByAircraft.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
results = [.. allJumps.GroupBy(j => j.Aircraft.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ByAircraft = results;
_userStatsRepository.Update(allStats);
}
return allStats.ByAircraft;
}
public IEnumerable<Statistic> GetStatsByDz()
{
var allStats = GetAllStats();
if (!allStats.ByDz.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
results = [.. allJumps.GroupBy(j => j.DropZone.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ByDz = results;
_userStatsRepository.Update(allStats);
}
return allStats.ByDz;
}
public IEnumerable<Statistic> GetStatsByGear()
{
var allStats = GetAllStats();
if (!allStats.ByGear.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
results = [.. allJumps.GroupBy(j => $"{j.Gear.Name} / {j.Gear.MainCanopy}",
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ByGear = results;
_userStatsRepository.Update(allStats);
}
return allStats.ByGear;
}
public IEnumerable<Statistic> GetStatsByJumpType()
{
var allStats = GetAllStats();
if (!allStats.ByJumpType.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
results = [.. allJumps.GroupBy(j => j.JumpType.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ByJumpType = results;
_userStatsRepository.Update(allStats);
}
return allStats.ByJumpType;
}
public IEnumerable<Statistic> GetStatsByYear()
{
var allStats = GetAllStats();
if (!allStats.ByYear.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
results = [.. allJumps.GroupBy(j => j.JumpDate.Year,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ByYear = results;
_userStatsRepository.Update(allStats);
}
return allStats.ByYear;
}
public IEnumerable<Statistic> GetStatsForLastMonthByDz()
{
var allStats = GetAllStats();
if (!allStats.ForLastMonthByDz.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
var yearOfLastJump = lastJump.JumpDate.Year;
var monthOfLastJump = lastJump.JumpDate.Month;
results = [.. allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
.GroupBy(j => j.DropZone.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ForLastMonthByDz = results;
_userStatsRepository.Update(allStats);
}
return allStats.ForLastMonthByDz;
}
public IEnumerable<Statistic> GetStatsForLastMonthByJumpType()
{
var allStats = GetAllStats();
if (!allStats.ForLastMonthByJumpType.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
var yearOfLastJump = lastJump.JumpDate.Year;
var monthOfLastJump = lastJump.JumpDate.Month;
results = [.. allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump)
.GroupBy(j => j.JumpType.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ForLastMonthByJumpType = results;
_userStatsRepository.Update(allStats);
}
return allStats.ForLastMonthByJumpType;
}
public IEnumerable<Statistic> GetStatsForLastYearByDz()
{
var allStats = GetAllStats();
if (!allStats.ForLastYearByDz.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
var yearOfLastJump = lastJump.JumpDate.Year;
results = [.. allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
.GroupBy(j => j.DropZone.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ForLastYearByDz = results;
_userStatsRepository.Update(allStats);
}
return allStats.ForLastYearByDz;
}
public IEnumerable<Statistic> GetStatsForLastYearByJumpType()
{
var allStats = GetAllStats();
if (!allStats.ForLastYearByJumpType.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
var yearOfLastJump = lastJump.JumpDate.Year;
results = [.. allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
.GroupBy(j => j.JumpType.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})];
}
allStats.ForLastYearByJumpType = results;
_userStatsRepository.Update(allStats);
}
return allStats.ForLastYearByJumpType;
}
public IEnumerable<Statistic> GetStatsByYearByJumpType()
{
var allStats = GetAllStats();
if (!allStats.ByYearByJumpType.Any())
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
if (allJumps.Any())
{
results = [.. allJumps.GroupBy(j => new { j.JumpType.Name, j.JumpDate.Year },
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.Year.ToString(),
Label2 = groupby.Name.ToString(),
Nb = jumps.Count()
})];
}
allStats.ByYearByJumpType = results;
_userStatsRepository.Update(allStats);
}
return allStats.ByYearByJumpType;
}
public void Reset()
{
var resetStats = new UserStats();
var myStats = GetAllStats();
myStats.ByAircraft = resetStats.ByAircraft;
myStats.ByDz = resetStats.ByDz;
myStats.ByGear = resetStats.ByGear;
myStats.ByJumpType = resetStats.ByJumpType;
myStats.ByYear = resetStats.ByYear;
myStats.ForLastMonthByDz = resetStats.ForLastMonthByDz;
myStats.ForLastMonthByJumpType = resetStats.ForLastMonthByJumpType;
myStats.ForLastYearByDz = resetStats.ForLastYearByDz;
myStats.ForLastYearByJumpType = resetStats.ForLastYearByJumpType;
myStats.ByYearByJumpType = resetStats.ByYearByJumpType;
_userStatsRepository.Update(myStats);
}
#endregion Public Methods
#region Private Methods
private UserStats GetAllStats()
{
var allStats = _userStatsRepository.GetAll(_identityService.ConnectedUser);
if (allStats == null)
{
allStats = new UserStats
{
User = _identityService.ConnectedUser
};
_userStatsRepository.Add(allStats);
}
return allStats;
}
#endregion Private Methods
#region Private Fields
private readonly IIdentityService _identityService;
private readonly IJumpService _jumpService;
private readonly IStatsByDzRepository _statsByDzRepository;
private readonly IStatsByGearRepository _statsByGearRepository;
private readonly IStatsByJumpTypeRepository _statsByJumpTypeRepository;
private readonly IStatsByYearByJumpTypeRepository _statsByYearByJumpTypeRepository;
private readonly IStatsByYearRepository _statsByYearRepository;
private readonly IStatsForLastMonthByDzRepository _statsForLastMonthByDzRepository;
private readonly IStatsForLastMonthByJumpTypeRepository _statsForLastMonthByJumpTypeRepository;
private readonly IStatsForLastYearByDzRepository _statsForLastYearByDzRepository;
private readonly IStatsForLastYearByJumpTypeRepository _statsForLastYearByJumpTypeRepository;
#endregion Private Fields
}
}