From dc06f256b4762e1f225508584d45bb6ca634f21f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20ANDRE?= Date: Wed, 16 Aug 2023 17:38:50 +0200 Subject: [PATCH] Add methods used for the graph of the tunnel flights --- .../Interfaces/ITunnelFlightService.cs | 6 ++ .../TunnelFlightService.cs | 61 +++++++++++++++---- .../Repositories/IJumpRepository.cs | 4 +- .../Repositories/ITunnelFlightRepository.cs | 2 + .../Controllers/TunnelFlightController.cs | 39 +++++++----- 5 files changed, 83 insertions(+), 29 deletions(-) diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelFlightService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelFlightService.cs index 78a9472..c1d0a87 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelFlightService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelFlightService.cs @@ -17,8 +17,14 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces IEnumerable GetTunnelFlightByDates(string beginDate, string endDate); + IEnumerable GetTunnelFlightGroupByMonth(string beginDate, string endDate); + void AddNewFlight(int tunnelId, TunnelFlight newFlight); + void DeleteTunnelFlightById(int id); + + void UpdateTunnelFlight(int id, TunnelFlight updatedTunnelFlight); + #endregion Public Methods } } \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/TunnelFlightService.cs b/Back/skydiveLogs-api.DomainBusiness/TunnelFlightService.cs index 4cfb426..0f8578d 100644 --- a/Back/skydiveLogs-api.DomainBusiness/TunnelFlightService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/TunnelFlightService.cs @@ -3,6 +3,7 @@ using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; using System; using System.Collections.Generic; +using System.Linq; namespace skydiveLogs_api.DomainBusiness { @@ -33,6 +34,46 @@ namespace skydiveLogs_api.DomainBusiness return _tunnelFlightRepository.GetById(id); } + public int GetTunnelFlightCount() + { + return _tunnelFlightRepository.GetCount(_identityService.ConnectedUser); + } + + public IEnumerable GetTunnelFlightsByIndexes(int beginIndex, int endIndex) + { + return _tunnelFlightRepository.GetBetweenIndex(_identityService.ConnectedUser, beginIndex, endIndex); + } + + public IEnumerable GetTunnelFlightGroupByMonth(string beginDate, string endDate) + { + var convertedBeginDate = Convert.ToDateTime(beginDate); + var convertedEndDate = Convert.ToDateTime(endDate); + + var allTunnelFlights = _tunnelFlightRepository.GetBetweenDate(_identityService.ConnectedUser, convertedBeginDate, convertedEndDate); + var results = Enumerable.Empty(); + + if (allTunnelFlights.Any()) + { + results = allTunnelFlights.GroupBy(j => j.FlightDate.ToString("yy-MM"), + j => j, + (groupby, tunnelFlights) => new Statistic + { + Label = groupby.ToString(), + Nb = tunnelFlights.Sum(t => t.NbMinutes) + }) + .ToList(); + } + + return results; + } + public IEnumerable GetTunnelFlightByDates(string beginDate, string endDate) + { + var convertedBeginDate = Convert.ToDateTime(beginDate); + var convertedEndDate = Convert.ToDateTime(endDate); + + return _tunnelFlightRepository.GetBetweenDate(_identityService.ConnectedUser, convertedBeginDate, convertedEndDate); + } + public void AddNewFlight(int tunnelId, TunnelFlight newFlight) { var tmp = _dropZoneService.GetDzById(tunnelId); @@ -51,23 +92,19 @@ namespace skydiveLogs_api.DomainBusiness _tunnelFlightRepository.Add(newFlight); } - public int GetTunnelFlightCount() + public void DeleteTunnelFlightById(int id) { - return _tunnelFlightRepository.GetCount(_identityService.ConnectedUser); + _tunnelFlightRepository.DeleteById(id); } - public IEnumerable GetTunnelFlightsByIndexes(int beginIndex, int endIndex) + public void UpdateTunnelFlight(int id, TunnelFlight updatedTunnelFlight) { - return _tunnelFlightRepository.GetBetweenIndex(_identityService.ConnectedUser, beginIndex, endIndex); - } - - public IEnumerable GetTunnelFlightByDates(string beginDate, string endDate) - { - var convertedBeginDate = Convert.ToDateTime(beginDate); - var convertedEndDate = Convert.ToDateTime(endDate); - - return _tunnelFlightRepository.GetBetweenDate(_identityService.ConnectedUser, convertedBeginDate, convertedEndDate); + var myTunnelFlight = GetTunnelFlightById(id); + myTunnelFlight.FlightDate = updatedTunnelFlight.FlightDate; + myTunnelFlight.NbMinutes = updatedTunnelFlight.NbMinutes; + myTunnelFlight.Notes = updatedTunnelFlight.Notes; + _tunnelFlightRepository.Update(myTunnelFlight); } #endregion Public Methods diff --git a/Back/skydiveLogs-api.DomainService/Repositories/IJumpRepository.cs b/Back/skydiveLogs-api.DomainService/Repositories/IJumpRepository.cs index 3ad26da..26fd545 100644 --- a/Back/skydiveLogs-api.DomainService/Repositories/IJumpRepository.cs +++ b/Back/skydiveLogs-api.DomainService/Repositories/IJumpRepository.cs @@ -7,14 +7,14 @@ namespace skydiveLogs_api.DomainService.Repositories { #region Public Methods - bool DeleteById(int id); - IEnumerable GetAll(User user); IEnumerable GetBetweenIndex(User user, int beginIndex, int endIndex); int GetCount(User user); + bool DeleteById(int id); + #endregion Public Methods } } \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainService/Repositories/ITunnelFlightRepository.cs b/Back/skydiveLogs-api.DomainService/Repositories/ITunnelFlightRepository.cs index e868d1e..0ba65d8 100644 --- a/Back/skydiveLogs-api.DomainService/Repositories/ITunnelFlightRepository.cs +++ b/Back/skydiveLogs-api.DomainService/Repositories/ITunnelFlightRepository.cs @@ -16,6 +16,8 @@ namespace skydiveLogs_api.DomainService.Repositories int GetCount(User user); + bool DeleteById(int id); + #endregion Public Methods } } \ No newline at end of file diff --git a/Back/skydiveLogs-api/Controllers/TunnelFlightController.cs b/Back/skydiveLogs-api/Controllers/TunnelFlightController.cs index 567e02d..f0a4804 100644 --- a/Back/skydiveLogs-api/Controllers/TunnelFlightController.cs +++ b/Back/skydiveLogs-api/Controllers/TunnelFlightController.cs @@ -23,14 +23,6 @@ namespace skydiveLogs_api.Controllers #region Public Methods - // DELETE: api/Tunnel/5 - //[HttpDelete("{id}")] - //[EnableCors] - //public void Delete(int id) - //{ - // _tunnelService.DeleteTunnelById(id); - //} - // GET: api/TunnelFlight [HttpGet] [EnableCors] @@ -58,6 +50,15 @@ namespace skydiveLogs_api.Controllers return _mapper.Map>(result); } + // GET: api/TunnelFlight/month/20230101/20230701 + [HttpGet("month/{beginDate}/{endDate}")] + [EnableCors] + public IEnumerable GetGroupByMonth(string beginDate, string endDate) + { + var result = _tunnelFlightService.GetTunnelFlightGroupByMonth(beginDate, endDate); + return _mapper.Map>(result); + } + // POST: api/Tunnel [HttpPost] [EnableCors] @@ -67,13 +68,21 @@ namespace skydiveLogs_api.Controllers _mapper.Map(value)); } - // PUT: api/Tunnel/5 - //[HttpPut("{id}")] - //[EnableCors] - //public void Put(int id, [FromBody] TunnelReq value) - //{ - // _tunnelService.UpdateJumpType(id, _mapper.Map(value)); - //} + // PUT: api/TunnelFlight/5 + [HttpPut("{id}")] + [EnableCors] + public void Put(int id, [FromBody] TunnelFlightReq value) + { + _tunnelFlightService.UpdateTunnelFlight(id, _mapper.Map(value)); + } + + // DELETE: api/TunnelFlight/5 + [HttpDelete("{id}")] + [EnableCors] + public void Delete(int id) + { + _tunnelFlightService.DeleteTunnelFlightById(id); + } #endregion Public Methods