Files
SkydiveLogs/Back/skydiveLogs-api.Infrastructure/JumpRepository.cs
T
2026-04-17 17:26:20 +02:00

152 lines
4.9 KiB
C#

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