using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
namespace skydiveLogs_api.DomainBusiness
{
public class TunnelFlightService : ITunnelFlightService
{
#region Public Constructors
public TunnelFlightService(IJumpTypeService jumpTypeService,
ITunnelFlightRepository tunnelFlightRepository,
IDropZoneService dropZoneService,
IIdentityService identityService)
{
_dropZoneService = dropZoneService;
_jumpTypeService = jumpTypeService;
_identityService = identityService;
_tunnelFlightRepository = tunnelFlightRepository;
}
#endregion Public Constructors
#region Public Methods
///
/// Retrieves all tunnel flights.
///
/// A collection of TunnelFlight entities containing all tunnel flights.
public IEnumerable GetAllTunnelFlights()
{
return _tunnelFlightRepository.GetAll(_identityService.ConnectedUser);
}
///
/// Retrieves a tunnel flight by its ID.
///
/// The tunnel flight ID to retrieve.
/// A TunnelFlight entity containing the tunnel flight details.
public TunnelFlight GetTunnelFlightById(int id)
{
return _tunnelFlightRepository.GetById(id);
}
///
/// Retrieves the total count of tunnel flights.
///
/// The total number of tunnel flights.
public int GetTunnelFlightCount()
{
return _tunnelFlightRepository.GetCount(_identityService.ConnectedUser);
}
///
/// Retrieves a range of tunnel flights with pagination.
///
/// The starting index for pagination.
/// The ending index for pagination.
/// A collection of TunnelFlight entities containing the requested range.
public IEnumerable GetTunnelFlightsByIndexes(int beginIndex, int endIndex)
{
return _tunnelFlightRepository.GetBetweenIndex(_identityService.ConnectedUser, beginIndex, endIndex);
}
///
/// Retrieves tunnel flights grouped by month within a date range.
///
/// The start date for grouping.
/// The end date for grouping.
/// A collection of Statistic entities grouped by month.
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 => new { FlightDate = j.FlightDate.ToString("yy-MM"), j.JumpType },
j => j,
(groupby, tunnelFlights) => new Statistic
{
Label = groupby.FlightDate,
Label2 = groupby.JumpType.Name,
Nb = tunnelFlights.Sum(t => t.NbMinutes)
})
.ToList();
}
return results;
}
///
/// Retrieves tunnel flights filtered by date range.
///
/// The start date for filtering.
/// The end date for filtering.
/// A collection of TunnelFlight entities filtered by the specified date range.
public IEnumerable GetTunnelFlightByDates(string beginDate, string endDate)
{
var convertedBeginDate = Convert.ToDateTime(beginDate);
var convertedEndDate = Convert.ToDateTime(endDate);
return _tunnelFlightRepository.GetBetweenDate(_identityService.ConnectedUser, convertedBeginDate, convertedEndDate);
}
///
/// Adds a new tunnel flight to the system.
///
/// The tunnel ID to add the flight to.
/// The jump type ID for the flight.
/// TunnelFlight entity containing the new flight data.
public void AddNewFlight(int tunnelId, int jumpTypeId, TunnelFlight newFlight)
{
var tmp = _dropZoneService.GetDzById(tunnelId);
var selectedTunnel = new Tunnel
{
Id = tmp.Id,
Name = tmp.Name,
Website = tmp.Website,
Address = tmp.Address,
Email = tmp.Email,
Latitude = tmp.Latitude,
Longitude = tmp.Longitude
};
var selectedJumpType = _jumpTypeService.GetJumpTypeById(jumpTypeId);
newFlight.Tunnel = selectedTunnel;
newFlight.JumpType = selectedJumpType;
_tunnelFlightRepository.Add(newFlight);
}
///
/// Deletes a tunnel flight by its ID.
///
/// The tunnel flight ID to delete.
public void DeleteTunnelFlightById(int id)
{
_tunnelFlightRepository.DeleteById(id);
}
///
/// Updates an existing tunnel flight.
///
/// The tunnel flight ID to update.
/// TunnelFlight entity containing the updated tunnel flight data.
public void UpdateTunnelFlight(int id, TunnelFlight updatedTunnelFlight)
{
var myTunnelFlight = GetTunnelFlightById(id);
myTunnelFlight.FlightDate = updatedTunnelFlight.FlightDate;
myTunnelFlight.NbMinutes = updatedTunnelFlight.NbMinutes;
myTunnelFlight.Notes = updatedTunnelFlight.Notes;
_tunnelFlightRepository.Update(myTunnelFlight);
}
#endregion Public Methods
#region Private Fields
private readonly IDropZoneService _dropZoneService;
private readonly IJumpTypeService _jumpTypeService;
private readonly ITunnelFlightRepository _tunnelFlightRepository;
private readonly IIdentityService _identityService;
#endregion Private Fields
}
}