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 { /// /// Repository class for managing jump data in the database. /// Provides operations to add, delete, retrieve, and update jump records per user. /// /// /// This repository interacts with LiteDB to perform CRUD operations on jump records. /// Each jump record is associated with a user, aircraft, drop zone, gear, and jump type. /// Navigation properties are included for related entities to enable eager loading. /// public class JumpRepository : IJumpRepository { #region Public Constructors /// /// Initializes a new instance of the class. /// /// The data provider used for database access operations. /// /// This constructor initializes the repository with a reference to the data provider /// and sets up the collection for jump data operations. /// The data provider manages the underlying LiteDatabase connection and provides /// typed collection accessors for different entity types. /// public JumpRepository(IDataProvider dataProvider) { _dataProvider = dataProvider; _col = _dataProvider.CollOfJump; } #endregion Public Constructors #region Public Methods /// /// Adds a new jump record to the database. /// /// The jump instance to insert into the database. /// The number of rows affected. Returns 0 if the insert operation failed. /// /// Attempts to insert the new jump record into the database using LiteDB. /// The jump record should have all required properties set before insertion. /// If an exception occurs during insertion, the method returns 0. /// The returned value indicates the number of records affected by the insert operation. /// public int Add(Jump newJump) { int result; try { var tmp = _col.Insert(newJump); result = tmp.AsInt32; } catch { result = 0; } return result; } /// /// Deletes a jump record by its unique identifier. /// /// The unique identifier of the jump to delete. /// if the deletion was successful; otherwise, . /// /// Deletes the jump record with the specified ID from the database. /// The method uses the method to remove the record. /// public bool DeleteById(int id) { return _col.Delete(new BsonValue(id)); } /// /// Retrieves all jump records for a specific user. /// /// The user whose jump records to retrieve. /// An enumerable collection containing all jump records belonging to the specified user. /// /// Queries the jump collection and retrieves all records where the user ID matches the provided user. /// Each returned jump record includes navigation properties to the associated aircraft, drop zone, /// gear, and jump type entities for eager loading. /// Returns an empty collection if the user has no jump records. /// public IEnumerable GetAll(User user) { return _col.Include(x => x.Aircraft) .Include(x => x.DropZone) .Include(x => x.Gear) .Include(x => x.JumpType) .Find(j => j.User.Id == user.Id); } /// /// Retrieves all jump records from the database. /// /// An enumerable collection containing all jump records stored in the database. /// /// This method is not currently implemented and throws a when called. /// Implement this method to retrieve all jump records from the database without filtering. /// /// Thrown when the method is called. public IEnumerable GetAll() { throw new NotImplementedException(); } /// /// Retrieves a range of jump records for a specific user with pagination. /// /// The user whose jump records to retrieve. /// The starting index for pagination (inclusive). /// The ending index for pagination (exclusive). /// An enumerable collection containing the requested range of jump records. /// /// Retrieves a paginated range of jump records for the specified user, ordered by jump date in descending order. /// Uses LiteDB query operators to limit and offset the results for efficient pagination. /// Each jump record includes navigation properties to related entities. /// public IEnumerable GetBetweenIndex(User user, int beginIndex, int endIndex) { return _col.Include(x => x.Aircraft) .Include(x => x.DropZone) .Include(x => x.Gear) .Include(x => x.JumpType) .Query() .OrderByDescending(j => j.JumpDate) .Where(j => j.User.Id == user.Id) .Limit(endIndex - beginIndex) .Offset(beginIndex) .ToList(); } /// /// Retrieves a jump record by its unique identifier. /// /// The unique identifier of the jump to retrieve. /// The jump record instance if found, otherwise null. /// /// Searches the jump collection for a record with the specified ID. /// Returns null if no jump record with the matching ID is found in the database. /// public Jump GetById(int id) { return _col.FindById(new BsonValue(id)); } /// /// Gets the total count of jump records for a specific user. /// /// The user whose jump records to count. /// The total number of jump records for the specified user. /// /// This method is not currently implemented and throws a when called. /// Implement this method to retrieve the count of jump records per user. /// /// Thrown when the method is called. public int GetCount(User user) { return _col.Count(j => j.User.Id == user.Id); } /// /// Gets the total count of jump records in the database. /// /// The total number of jump records stored in the database. /// /// This method is not currently implemented and throws a when called. /// Implement this method to retrieve the count of all jump records. /// /// Thrown when the method is called. public int GetCount() { throw new NotImplementedException(); } /// /// Updates an existing jump record in the database. /// /// The jump record instance containing the updated data. /// if the update was successful; otherwise, . /// /// Updates the jump record in the database with the provided jump record instance. /// The jump record must have a valid ID to be updated. /// The method returns the result of the underlying LiteDB update operation. /// If the jump record does not exist, the update operation will return 0. /// public bool Update(Jump updatedJump) { return _col.Update(updatedJump); } #endregion Public Methods #region Private Fields /// /// The LiteDB collection for jump records. /// private readonly ILiteCollection _col; /// /// The data provider used for database access operations. /// private readonly IDataProvider _dataProvider; #endregion Private Fields } }