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 JumpRepository : IJumpRepository
{
#region Public Constructors
///
/// Initializes a new instance of the class
///
/// The data provider to use for data access
public JumpRepository(IDataProvider dataProvider)
{
_dataProvider = dataProvider;
_col = _dataProvider.CollOfJump;
}
#endregion Public Constructors
#region Public Methods
///
/// Adds a new jump to the database
///
/// The jump instance to add
/// The number of rows affected (0 if insert failed)
public int Add(Jump newJump)
{
int result;
try
{
var tmp = _col.Insert(newJump);
result = tmp.AsInt32;
}
catch
{
result = 0;
}
return result;
}
///
/// Deletes a jump by its unique identifier
///
/// The unique identifier of the jump to delete
/// True if the deletion was successful, false otherwise
public bool DeleteById(int id)
{
return _col.Delete(new BsonValue(id));
}
///
/// Retrieves all jumps for a specific user
///
/// The user whose jumps to retrieve
/// A collection of jump instances belonging to the user
public IEnumerable GetAll(User user)
{
return _col.Include(x => x.Aircraft)
.Include(x => x.DropZone)
.Include(x => x.Gear)
.Include(x => x.JumpType)
.Find(j => j.User.Id == user.Id);
}
///
/// Retrieves all jumps from the database
///
/// A collection of all jump instances
public IEnumerable GetAll()
{
throw new System.NotImplementedException();
}
///
/// Retrieves a range of jumps for a specific user
///
/// The user whose jumps to retrieve
/// The starting index
/// The ending index
/// A collection of jump instances
public IEnumerable GetBetweenIndex(User user, int beginIndex, int endIndex)
{
return _col.Include(x => x.Aircraft)
.Include(x => x.DropZone)
.Include(x => x.Gear)
.Include(x => x.JumpType)
.Query()
.OrderByDescending(j => j.JumpDate)
.Where(j => j.User.Id == user.Id)
.Limit(endIndex - beginIndex)
.Offset(beginIndex)
.ToList();
}
///
/// Retrieves a jump by its unique identifier
///
/// The unique identifier of the jump
/// The jump instance or null if not found
public Jump GetById(int id)
{
return _col.FindById(new BsonValue(id));
}
///
/// Gets the total count of jumps for a specific user
///
/// The user whose jumps to count
/// The total number of jumps
public int GetCount(User user)
{
return _col.Count(j => j.User.Id == user.Id);
}
///
/// Gets the total count of jumps in the database
///
/// The total number of jumps
public int GetCount()
{
throw new System.NotImplementedException();
}
///
/// Updates an existing jump in the database
///
/// The jump instance to update
/// True if the update was successful, false otherwise
public bool Update(Jump updatedJump)
{
return _col.Update(updatedJump);
}
#endregion Public Methods
#region Private Fields
private readonly ILiteCollection _col;
private readonly IDataProvider _dataProvider;
#endregion Private Fields
}
}