Files
SkydiveLogs/Back/skydiveLogs-api.Business/StatsService.cs
Sébastien André 498ec2c741 fix
2020-08-25 16:25:40 +02:00

239 lines
8.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.Data.Interface;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business
{
public class StatsService : IStatsService
{
public StatsService(IJumpService jumpService)
{
_jumpService = jumpService;
}
public IEnumerable<Statistic> GetStatsByAircraft(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsByDz(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsByJumpType(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsByGear(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
if (allJumps.Any())
{
results = allJumps.GroupBy(j => j.Gear.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsByYear(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsForLastYearByDz(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsForLastYearByJumpType(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsForLastMonthByDz(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public IEnumerable<Statistic> GetStatsForLastMonthByJumpType(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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()
})
.ToList();
}
return results;
}
public SimpleSummary GetSimpleSummary(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
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;
}
private readonly IJumpService _jumpService;
}
}