using LiteDB; using skydiveLogs_api.Domain; using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.Infrastructure.Interfaces; using System.Collections.Generic; using System.Linq; namespace skydiveLogs_api.Infrastructure { public class AircraftRepository : IAircraftRepository { #region Public Constructors /// /// Initializes a new instance of the class /// /// The data provider to use for data access public AircraftRepository(IDataProvider dataProvider) { _dataProvider = dataProvider; _col = _dataProvider.CollOfAircraft; } #endregion Public Constructors #region Public Methods /// /// Adds a new aircraft to the database /// /// The aircraft instance to add /// The number of rows affected (0 if insert failed) public int Add(Aircraft newAircraft) { int result; try { var tmp = _col.Insert(newAircraft); result = tmp.AsInt32; } catch { result = 0; } return result; } /// /// Retrieves all aircraft from the database /// /// A collection of all aircraft instances public IEnumerable GetAll() { return _col.FindAll().ToList(); } /// /// Retrieves an aircraft by its unique identifier /// /// The unique identifier of the aircraft /// The aircraft instance or null if not found public Aircraft GetById(int id) { return _col.FindById(new BsonValue(id)); } /// /// Gets the total count of aircraft in the database /// /// The total number of aircraft public int GetCount() { throw new System.NotImplementedException(); } /// /// Updates an existing aircraft in the database /// /// The aircraft instance to update /// True if the update was successful, false otherwise public bool Update(Aircraft aircraft) { return _col.Update(aircraft); } #endregion Public Methods #region Private Fields private readonly ILiteCollection _col; private readonly IDataProvider _dataProvider; #endregion Private Fields } }