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
{
public class FavoriteDropZoneRepository : IFavoriteDropZoneRepository
{
#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 favorite drop zone data operations.
/// The data provider manages the underlying LiteDatabase connection and provides
/// typed collection accessors for different entity types.
///
public FavoriteDropZoneRepository(IDataProvider dataProvider)
{
_dataProvider = dataProvider;
_col = _dataProvider.CollOfFavoriteDropZone;
}
#endregion Public Constructors
#region Public Methods
///
/// Adds a new favorite drop zone to the database.
///
/// The favorite 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 favorite 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(FavoriteDropZone favoriteToAdd)
{
int result;
try
{
var tmp = _col.Insert(favoriteToAdd);
result = tmp.AsInt32;
}
catch
{
result = 0;
}
return result;
}
///
/// Deletes favorite drop zone entries by drop zone ID and user ID.
///
/// The unique identifier of the drop zone to delete favorites for.
/// The unique identifier of the user whose favorites to delete.
/// The number of rows affected. Returns 0 if the delete operation failed or no records matched.
///
/// Deletes all favorite drop zone records where both the drop zone ID and user ID match the provided values.
/// This operation is commonly used to remove a specific user's favorites for a particular drop zone.
///
public int Delete(int dropZoneId, int userId)
{
return _col.DeleteMany(d => d.DropZone.Id == dropZoneId && d.User.Id == userId);
}
///
/// Retrieves all favorite drop zones for a specific user.
///
/// The user whose favorite drop zones to retrieve.
/// An enumerable collection containing all favorite drop zone instances belonging to the specified user.
///
/// Queries the favorite drop zone collection and retrieves all records where the user ID matches the provided user.
/// Each returned record includes navigation properties to the associated user and drop zone entities.
/// Returns an empty collection if the user has no favorite drop zones.
///
public IEnumerable GetAll(User user)
{
return _col.Query()
.Where(j => j.User.Id == user.Id)
.ToList();
}
///
/// Retrieves all favorite drop zones from the database.
///
/// An enumerable collection containing all favorite drop zone instances in the database.
///
/// This method is not currently implemented and throws a when called.
/// Implement this method to retrieve all favorite drop zones across all users in the system.
///
/// Thrown when the method is called.
public IEnumerable GetAll()
{
throw new NotImplementedException();
}
///
/// Retrieves a favorite drop zone by its unique identifier.
///
/// The unique identifier of the favorite drop zone to retrieve.
/// The favorite drop zone instance if found, otherwise null.
///
/// Searches the favorite drop zone collection for a record with the specified ID.
/// This method currently returns null as it's not implemented with proper filtering by ID.
/// Implement this method to retrieve a specific favorite drop zone by its database ID.
///
/// Thrown when attempting to retrieve by ID.
public FavoriteDropZone GetById(int id)
{
throw new NotImplementedException();
}
///
/// Gets the total count of favorite drop zones for a specific user.
///
/// The user whose favorite drop zones to count.
/// The total number of favorite drop zones for the specified user.
///
/// This method is not currently implemented and throws a when called.
/// Implement this method to retrieve the count of favorite drop zones per user.
///
/// Thrown when the method is called.
public int GetCount()
{
throw new NotImplementedException();
}
///
/// Updates an existing favorite drop zone in the database.
///
/// The favorite drop zone instance containing the updated data.
/// if the update was successful; otherwise, .
///
/// This method is not currently implemented and throws a when called.
/// Implement this method to update existing favorite drop zone records in the database.
/// Note: Favorite drop zones may not typically be updated; consider using Add for new favorites.
///
/// Thrown when attempting to update.
public bool Update(FavoriteDropZone updated)
{
throw new NotImplementedException();
}
#endregion Public Methods
#region Private Fields
///
/// The LiteDB collection for favorite drop zone records.
///
private readonly ILiteCollection _col;
///
/// The data provider used for database access operations.
///
private readonly IDataProvider _dataProvider;
#endregion Private Fields
}
}