using System; using System.Collections.Generic; using LiteDB; using skydiveLogs_api.Domain; using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.Infrastructure.Interfaces; namespace skydiveLogs_api.Infrastructure { public class TunnelFlightRepository : ITunnelFlightRepository { #region Public Constructors public TunnelFlightRepository(IDataProvider dataProvider) { _dataProvider = dataProvider; _col = _dataProvider.CollOfTunnelFlight; } #endregion Public Constructors #region Public Methods /// /// Adds a new tunnel flight to the database. /// /// The tunnel flight instance to insert into the database. /// The number of rows affected. Returns 0 if the insert operation failed. public int Add(TunnelFlight newTunnelFlight) { int result; try { var tmp = _col.Insert(newTunnelFlight); result = tmp.AsInt32; } catch { result = 0; } return result; } /// /// Deletes a tunnel flight by its unique identifier. /// /// The unique identifier of the tunnel flight to delete. /// if the deletion was successful; otherwise, . public bool DeleteById(int id) { return _col.Delete(new BsonValue(id)); } /// /// Retrieves all tunnel flights for a specific user. /// /// The user whose tunnel flight records to retrieve. /// An enumerable collection containing all tunnel flight records belonging to the specified user. public IEnumerable GetAll(User user) { return _col.Include(x => x.Tunnel) .Include(x => x.JumpType) .Find(j => j.User.Id == user.Id); } /// /// Retrieves all tunnel flights from the database. /// /// An enumerable collection containing all tunnel flight records stored in the database. public IEnumerable GetAll() { throw new System.NotImplementedException(); } /// /// Retrieves a range of tunnel flights for a specific user with pagination. /// /// The user whose tunnel flight records to retrieve. /// The starting index for pagination (inclusive). /// The ending index for pagination (exclusive). /// An enumerable collection containing the requested range of tunnel flight records. public IEnumerable GetBetweenIndex(User user, int beginIndex, int endIndex) { return _col.Include(x => x.Tunnel) .Include(x => x.JumpType) .Query() .OrderByDescending(j => j.FlightDate) .Where(j => j.User.Id == user.Id) .Limit(endIndex - beginIndex) .Offset(beginIndex) .ToList(); } /// /// Retrieves a range of tunnel flights within a date range for a specific user. /// /// The user whose tunnel flight records to retrieve. /// The start of the date range (inclusive). /// The end of the date range (exclusive). /// An enumerable collection containing the requested range of tunnel flight records within the specified date range. public IEnumerable GetBetweenDate(User user, DateTime beginDate, DateTime endDate) { return _col.Include(x => x.Tunnel) .Include(x => x.JumpType) .Query() .OrderByDescending(j => j.FlightDate) .Where(j => j.FlightDate >= beginDate) .Where(j => j.FlightDate <= endDate) .ToList(); } /// /// Retrieves a tunnel flight by its unique identifier. /// /// The unique identifier of the tunnel flight to retrieve. /// The tunnel flight record instance if found, otherwise null. public TunnelFlight GetById(int id) { return _col.FindById(new BsonValue(id)); } /// /// Gets the total count of tunnel flights for a specific user. /// /// The user whose tunnel flight records to count. /// The total number of tunnel flight records for the specified user. public int GetCount(User user) { return _col.Count(j => j.User.Id == user.Id); } /// /// Gets the total count of tunnel flights in the database. /// /// The total number of tunnel flight records stored in the database. public int GetCount() { throw new System.NotImplementedException(); } /// /// Updates an existing tunnel flight record in the database. /// /// The tunnel flight instance containing the updated data. /// if the update was successful; otherwise, . public bool Update(TunnelFlight updatedTunnelFlight) { return _col.Update(updatedTunnelFlight); } #endregion Public Methods #region Private Fields private readonly ILiteCollection _col; private readonly IDataProvider _dataProvider; #endregion Private Fields } }