From 6e804b1ae413c0305d57dfb08b69c061a46b5e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Andr=C3=A9?= Date: Fri, 14 Feb 2020 16:48:43 +0100 Subject: [PATCH] Add controler actions to return stats for the current season --- .../Interface/IStatsService.cs | 8 ++ Back/skydiveLogs-api.Business/StatsService.cs | 74 +++++++++++++++++++ .../Controllers/StatsController.cs | 28 +++++++ .../DataContract/StatisticForLastMonthResp.cs | 11 +++ .../DataContract/StatisticForLastYearResp.cs | 11 +++ 5 files changed, 132 insertions(+) create mode 100644 Back/skydiveLogs-api/DataContract/StatisticForLastMonthResp.cs create mode 100644 Back/skydiveLogs-api/DataContract/StatisticForLastYearResp.cs diff --git a/Back/skydiveLogs-api.Business/Interface/IStatsService.cs b/Back/skydiveLogs-api.Business/Interface/IStatsService.cs index cc87bb2..2f23af2 100644 --- a/Back/skydiveLogs-api.Business/Interface/IStatsService.cs +++ b/Back/skydiveLogs-api.Business/Interface/IStatsService.cs @@ -14,5 +14,13 @@ namespace skydiveLogs_api.Business.Interface IEnumerable GetStatsByGear(); IEnumerable GetStatsByYear(); + + IEnumerable GetStatsForLastYearByDz(); + + IEnumerable GetStatsForLastYearByJumpType(); + + IEnumerable GetStatsForLastMonthByDz(); + + IEnumerable GetStatsForLastMonthByJumpType(); } } diff --git a/Back/skydiveLogs-api.Business/StatsService.cs b/Back/skydiveLogs-api.Business/StatsService.cs index b314186..1475135 100644 --- a/Back/skydiveLogs-api.Business/StatsService.cs +++ b/Back/skydiveLogs-api.Business/StatsService.cs @@ -85,6 +85,80 @@ namespace skydiveLogs_api.Business .ToList(); } + public IEnumerable 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 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 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 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; } } diff --git a/Back/skydiveLogs-api/Controllers/StatsController.cs b/Back/skydiveLogs-api/Controllers/StatsController.cs index 68c8f56..a7c1780 100644 --- a/Back/skydiveLogs-api/Controllers/StatsController.cs +++ b/Back/skydiveLogs-api/Controllers/StatsController.cs @@ -66,6 +66,34 @@ namespace skydiveLogs_api.Controllers return _mapper.Map>(result); } + [HttpGet("ForLastYear")] + [EnableCors] + public StatisticForLastYearResp ForLastYear() + { + var resultByDz = _statsService.GetStatsForLastYearByDz(); + var resultByJumpType = _statsService.GetStatsForLastYearByJumpType(); + + var result = new StatisticForLastYearResp(); + result.ByDz = _mapper.Map>(resultByDz); + result.ByJumpType = _mapper.Map>(resultByJumpType); + + return result; + } + + [HttpGet("ForLastMonth")] + [EnableCors] + public StatisticForLastMonthResp ForLastMonth() + { + var resultByDz = _statsService.GetStatsForLastMonthByDz(); + var resultByJumpType = _statsService.GetStatsForLastMonthByJumpType(); + + var result = new StatisticForLastMonthResp(); + result.ByDz = _mapper.Map>(resultByDz); + result.ByJumpType = _mapper.Map>(resultByJumpType); + + return result; + } + #region Private properties private readonly IStatsService _statsService; diff --git a/Back/skydiveLogs-api/DataContract/StatisticForLastMonthResp.cs b/Back/skydiveLogs-api/DataContract/StatisticForLastMonthResp.cs new file mode 100644 index 0000000..b5cd5e7 --- /dev/null +++ b/Back/skydiveLogs-api/DataContract/StatisticForLastMonthResp.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace skydiveLogs_api.DataContract +{ + public class StatisticForLastMonthResp + { + public IEnumerable ByDz { get; set; } + + public IEnumerable ByJumpType { get; set; } + } +} diff --git a/Back/skydiveLogs-api/DataContract/StatisticForLastYearResp.cs b/Back/skydiveLogs-api/DataContract/StatisticForLastYearResp.cs new file mode 100644 index 0000000..49cebfe --- /dev/null +++ b/Back/skydiveLogs-api/DataContract/StatisticForLastYearResp.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace skydiveLogs_api.DataContract +{ + public class StatisticForLastYearResp + { + public IEnumerable ByDz { get; set; } + + public IEnumerable ByJumpType { get; set; } + } +}