using System;
using System.Collections.Generic;
using System.Linq;
using LiteDB;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces;
namespace skydiveLogs_api.Infrastructure
{
public class AircraftRepository : IAircraftRepository
{
#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 aircraft data operations.
///
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 insert into the database
/// The number of rows affected. Returns 0 if the insert operation failed.
///
/// Attempts to insert the new aircraft into the database using LiteDB.
/// If an exception occurs during insertion, the method returns 0.
///
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.
///
/// An enumerable collection containing all aircraft instances stored in the database
///
/// Queries the aircraft collection and retrieves all records,
/// then converts the result to a List for enumeration.
///
public IEnumerable GetAll()
{
return _col.FindAll().ToList();
}
///
/// Retrieves an aircraft by its unique identifier.
///
/// The unique identifier of the aircraft to retrieve
/// The aircraft instance if found, otherwise null
///
/// Searches the aircraft collection for a record with the specified ID
/// and returns the found aircraft or null if no match exists.
///
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 stored in the database
///
/// This method is not currently implemented and throws
/// a when called.
///
public int GetCount()
{
throw new NotImplementedException();
}
///
/// Updates an existing aircraft in the database.
///
/// The aircraft instance containing the updated data
/// if the update was successful; otherwise,
///
/// Updates the aircraft record in the database with the provided aircraft instance.
/// The method returns the result of the underlying LiteDB update operation.
///
public bool Update(Aircraft aircraft)
{
return _col.Update(aircraft);
}
#endregion Public Methods
#region Private Fields
///
/// The LiteDB collection for aircraft records.
///
private readonly ILiteCollection _col;
///
/// The data provider used for database access operations.
///
private readonly IDataProvider _dataProvider;
#endregion Private Fields
}
}