Files
SkydiveLogs/Back/skydiveLogs-api.Infrastructure/JumpRepository.cs
2022-05-08 16:42:04 +02:00

102 lines
2.6 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
public JumpRepository(IDataProvider dataProvider)
{
_dataProvider = dataProvider;
_col = _dataProvider.CollOfJump;
}
#endregion Public Constructors
#region Public Methods
public int Add(Jump newJump)
{
int result;
try
{
var tmp = _col.Insert(newJump);
result = tmp.AsInt32;
}
catch
{
result = 0;
}
return result;
}
public bool DeleteById(int id)
{
return _col.Delete(new BsonValue(id));
}
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);
}
public IEnumerable<Jump> GetAll()
{
throw new System.NotImplementedException();
}
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();
}
public Jump GetById(int id)
{
return _col.FindById(new BsonValue(id));
}
public int GetCount(User user)
{
return _col.Count(j => j.User.Id == user.Id);
}
public int GetCount()
{
throw new System.NotImplementedException();
}
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
}
}