using LiteDB;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces;
using System.Collections.Generic;
namespace skydiveLogs_api.Infrastructure
{
public class GearRepository : IGearRepository
{
#region Public Constructors
///
/// Initializes a new instance of the class
///
/// The data provider to use for data access
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 add
/// The number of rows affected (0 if insert failed)
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
///
/// A collection of all gear instances
public IEnumerable GetAll()
{
throw new System.NotImplementedException();
}
///
/// Retrieves all gear items for a specific user
///
/// The user whose gear items to retrieve
/// A collection of gear instances belonging to the user
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
/// The gear instance or null if not found
public Gear GetById(int id)
{
return _col.FindById(new BsonValue(id));
}
///
/// Gets the total count of gear items in the database
///
/// The total number of gear items
public int GetCount()
{
throw new System.NotImplementedException();
}
///
/// Updates an existing gear item in the database
///
/// The gear instance to update
/// True if the update was successful, false otherwise
public bool Update(Gear updatedGear)
{
return _col.Update(updatedGear);
}
#endregion Public Methods
#region Private Fields
private readonly ILiteCollection _col;
private readonly IDataProvider _dataProvider;
#endregion Private Fields
}
}