using System.Collections.Generic;
using SQLite;

namespace CounterDrone.Core.Repository
{
    /// <summary>泛型仓储基类</summary>
    public abstract class BaseRepository<T> where T : new()
    {
        protected readonly SQLiteConnection Db;

        protected BaseRepository(SQLiteConnection db)
        {
            Db = db;
        }

        public virtual T GetById(object id)
        {
            return Db.Find<T>(id);
        }

        public virtual List<T> GetAll()
        {
            return Db.Table<T>().ToList();
        }

        public virtual int Insert(T entity)
        {
            return Db.Insert(entity);
        }

        public virtual int Update(T entity)
        {
            return Db.Update(entity);
        }

        public virtual int Delete(object id)
        {
            return Db.Delete<T>(id);
        }

        public virtual void InsertAll(IEnumerable<T> entities)
        {
            Db.InsertAll(entities);
        }
    }
}
