Files
SkydiveLogs/Back/skydiveLogs-api.Business/StatsService.cs
Sébastien André 6e804b1ae4 Add controler actions to return stats for
the current season
2020-02-14 16:48:43 +01:00

165 lines
6.3 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(IJumpRepository jumpRepository)
{
_jumpRepository = jumpRepository;
}
public IEnumerable<Statistic> GetStatsByAircraft()
{
var allJumps = _jumpRepository.GetAll();
return allJumps.GroupBy(j => j.Aircraft.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})
.ToList();
}
public IEnumerable<Statistic> GetStatsByDz()
{
var allJumps = _jumpRepository.GetAll();
return allJumps.GroupBy(j => j.DropZone.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})
.ToList();
}
public IEnumerable<Statistic> GetStatsByJumpType()
{
var allJumps = _jumpRepository.GetAll();
return allJumps.GroupBy(j => j.JumpType.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})
.ToList();
}
public IEnumerable<Statistic> GetStatsByGear()
{
var allJumps = _jumpRepository.GetAll();
return allJumps.GroupBy(j => j.Gear.Name,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})
.ToList();
}
public IEnumerable<Statistic> GetStatsByYear()
{
var allJumps = _jumpRepository.GetAll();
return allJumps.GroupBy(j => j.JumpDate.Year,
j => j,
(groupby, jumps) => new Statistic
{
Label = groupby.ToString(),
Nb = jumps.Count()
})
.ToList();
}
public IEnumerable<Statistic> GetStatsForLastYearByDz()
{
var allJumps = _jumpRepository.GetAll();
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
var yearOfLastJump = lastJump.JumpDate.Year;
return 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();
}
public IEnumerable<Statistic> GetStatsForLastYearByJumpType()
{
var allJumps = _jumpRepository.GetAll();
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
var yearOfLastJump = lastJump.JumpDate.Year;
return 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();
}
public IEnumerable<Statistic> GetStatsForLastMonthByDz()
{
var allJumps = _jumpRepository.GetAll();
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
var yearOfLastJump = lastJump.JumpDate.Year;
var monthOfLastJump = lastJump.JumpDate.Month;
return 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();
}
public IEnumerable<Statistic> GetStatsForLastMonthByJumpType()
{
var allJumps = _jumpRepository.GetAll();
var lastJump = allJumps.OrderByDescending(j => j.JumpDate).FirstOrDefault();
var yearOfLastJump = lastJump.JumpDate.Year;
var monthOfLastJump = lastJump.JumpDate.Month;
return 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();
}
private readonly IJumpRepository _jumpRepository;
}
}