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 StatsByDzRepository : IStatsByDzRepository { #region Public Constructors /// /// Initializes a new instance of the class /// /// The data provider to use for data access public StatsByDzRepository(IDataProvider dataProvider) { _dataProvider = dataProvider; _col = _dataProvider.CollOfStatsByDz; } #endregion Public Constructors #region Public Methods /// /// Adds a new collection of stats by drop zone to the database /// /// The collection of stats by drop zone instances to add /// The number of rows affected public int Add(IEnumerable newStats) { int result = 0; try { foreach (var newStat in newStats) { var tmp = _col.Insert(newStats); result = tmp; } } catch { result = 0; } return result; } /// /// Retrieves all stats by drop zone for a specific user /// /// The user whose stats by drop zone to retrieve /// A collection of stats by drop zone 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(); } /// /// Updates existing stats by drop zone in the database /// /// The stats by drop zone instances to update /// The user whose stats to update /// True if the update was successful, false otherwise public bool Update(IEnumerable updatedStats, User user) { Delete(user); var tmp = Add(updatedStats); return tmp != 0; } /// /// Deletes all stats by drop zone for a specific user /// /// The user whose stats by drop zone to delete /// True if the delete was successful, false otherwise public bool Delete(User user) { var tmp = _col.DeleteMany(s => s.User.Id == user.Id); return tmp != 0; } #endregion Public Methods #region Private Fields private readonly ILiteCollection _col; private readonly IDataProvider _dataProvider; #endregion Private Fields } }