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 gear data in the database. /// Provides operations to add, retrieve, count, and update gear records per user. /// /// /// This repository interacts with LiteDB to perform CRUD operations on gear items. /// Each gear item is associated with a user, allowing for user-specific gear management. /// public class GearRepository : IGearRepository { #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 gear data operations. /// The data provider manages the underlying LiteDatabase connection and provides /// typed collection accessors for different entity types. /// public GearRepository(IDataProvider dataProvider) { _dataProvider = dataProvider; _col = _dataProvider.CollOfGear; } #endregion Public Constructors #region Public Methods /// /// Adds a new gear item to the database. /// /// The gear instance to insert into the database. /// The number of rows affected. Returns 0 if the insert operation failed. /// /// Attempts to insert the new gear item into the database using LiteDB. /// The gear item 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(Gear newGear) { int result; try { var tmp = _col.Insert(newGear); result = tmp.AsInt32; } catch { result = 0; } return result; } /// /// Retrieves all gear items from the database. /// /// An enumerable collection containing all gear instances stored in the database. /// /// This method is not currently implemented and throws a when called. /// Implement this method to retrieve all gear records from the database, potentially filtered by user. /// /// Thrown when the method is called. public IEnumerable GetAll() { throw new NotImplementedException(); } /// /// Retrieves all gear items for a specific user. /// /// The user whose gear items to retrieve. /// An enumerable collection containing all gear instances belonging to the specified user. /// /// Queries the gear collection and retrieves all records where the user ID matches the provided user. /// Each returned gear record includes navigation properties to the associated user entity. /// Returns an empty collection if the user has no gear items. /// public IEnumerable GetAll(User user) { return _col.Include(x => x.User) .Query() .Where(j => j.User.Id == user.Id) .ToList(); } /// /// Retrieves a gear item by its unique identifier. /// /// The unique identifier of the gear item to retrieve. /// The gear item instance if found, otherwise null. /// /// Searches the gear collection for a record with the specified ID. /// Returns null if no gear item with the matching ID is found in the database. /// public Gear GetById(int id) { return _col.FindById(new BsonValue(id)); } /// /// Gets the total count of gear items for a specific user. /// /// The user whose gear items to count. /// The total number of gear items for the specified user. /// /// This method is not currently implemented and throws a when called. /// Implement this method to retrieve the count of gear items per user. /// /// Thrown when the method is called. public int GetCount() { throw new NotImplementedException(); } /// /// Updates an existing gear item in the database. /// /// The gear item instance containing the updated data. /// if the update was successful; otherwise, . /// /// Updates the gear record in the database with the provided gear item instance. /// The gear item must have a valid ID to be updated. /// The method returns the result of the underlying LiteDB update operation. /// If the gear item does not exist, the update operation will return 0. /// public bool Update(Gear updatedGear) { return _col.Update(updatedGear); } #endregion Public Methods #region Private Fields /// /// The LiteDB collection for gear records. /// private readonly ILiteCollection _col; /// /// The data provider used for database access operations. /// private readonly IDataProvider _dataProvider; #endregion Private Fields } }