using LiteDB;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces;
using System.Collections.Generic;
using System.Linq;

namespace skydiveLogs_api.Infrastructure
{
    public class JumpTypeRepository : IJumpTypeRepository
    {
        #region Public Constructors

        public JumpTypeRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfJumpType;
        }

        #endregion Public Constructors

        #region Public Methods

        public int Add(JumpType newJumpType)
        {
            int result;

            try
            {
                var tmp = _col.Insert(newJumpType);
                result = tmp.AsInt32;
            }
            catch
            {
                result = 0;
            }

            return result;
        }

        public IEnumerable<JumpType> GetAll()
        {
            return _col.FindAll().ToList();
        }

        public JumpType GetById(int id)
        {
            return _col.FindById(new BsonValue(id));
        }

        public bool Update(JumpType updatedJumpType)
        {
            return _col.Update(updatedJumpType);
        }

        #endregion Public Methods

        #region Private Fields

        private readonly ILiteCollection<JumpType> _col;
        private readonly IDataProvider _dataProvider;

        #endregion Private Fields
    }
}