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 DropZoneRepository : IDropZoneRepository
{
#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 drop zone data operations.
/// The data provider manages the underlying LiteDatabase connection.
///
public DropZoneRepository(IDataProvider dataProvider)
{
_dataProvider = dataProvider;
_col = _dataProvider.CollOfDropZone;
}
#endregion Public Constructors
#region Public Methods
///
/// Adds a new drop zone to the database.
///
/// The drop zone instance to insert into the database.
/// The number of rows affected. Returns 0 if the insert operation failed.
///
/// Attempts to insert the new drop zone into the database using LiteDB.
/// 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(DropZone newDropZone)
{
int result;
try
{
var tmp = _col.Insert(newDropZone);
result = tmp.AsInt32;
}
catch
{
result = 0;
}
return result;
}
///
/// Retrieves all drop zones from the database.
///
/// An enumerable collection containing all drop zone instances stored in the database.
///
/// Queries the drop zone collection and retrieves all records,
/// then converts the result to a List for enumeration.
/// Returns an empty list if no drop zones exist in the database.
///
public IEnumerable GetAll()
{
return _col.FindAll().ToList();
}
///
/// Retrieves a drop zone by its unique identifier.
///
/// The unique identifier of the drop zone to retrieve.
/// The drop zone instance if found, otherwise null.
///
/// Searches the drop zone collection for a record with the specified ID
/// and returns the found drop zone or null if no match exists.
/// Use to retrieve all drop zones or filter by criteria.
///
public DropZone GetById(int id)
{
return _col.FindById(new BsonValue(id));
}
///
/// Gets the total count of drop zones in the database.
///
/// The total number of drop zones stored in the database.
///
/// This method is not currently implemented and throws
/// a when called.
/// Implement this method to retrieve the count of all drop zones.
///
/// Thrown when the count is requested.
public int GetCount()
{
throw new NotImplementedException();
}
///
/// Updates an existing drop zone in the database.
///
/// The drop zone instance containing the updated data.
/// if the update was successful; otherwise, .
///
/// Updates the drop zone record in the database with the provided drop zone instance.
/// The drop zone must have a valid ID to be updated.
/// The method returns the result of the underlying LiteDB update operation.
/// If the drop zone does not exist, the update operation will return 0.
///
public bool Update(DropZone dropZone)
{
return _col.Update(dropZone);
}
#endregion Public Methods
#region Private Fields
///
/// The LiteDB collection for drop zone records.
///
private readonly ILiteCollection _col;
///
/// The data provider used for database access operations.
///
private readonly IDataProvider _dataProvider;
#endregion Private Fields
}
}