﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Akalib.Entity.Repository
{
	/// <summary>
	///
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public abstract class GenericRepository<T> : IGenericRepository<T>
	  where T : class
	{
		protected DbContext _entities;
		protected readonly IDbSet<T> _dbset;

		public DbContext Entities { get { return _entities; } }

		public GenericRepository(DbContext context)
		{
			_entities = context;
			_dbset = context.Set<T>();
		}

		public virtual IEnumerable<T> GetAll()
		{
			return _dbset.AsEnumerable<T>();
		}

		public IEnumerable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
		{
			IEnumerable<T> query = _dbset.Where(predicate).AsEnumerable();
			return query;
		}

		public virtual T Add(T entity)
		{
			return _dbset.Add(entity);
		}

		public virtual T Delete(T entity)
		{
			return _dbset.Remove(entity);
		}

		public virtual void Edit(T entity)
		{
			_entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
		}

		public virtual void Save()
		{
			_entities.SaveChanges();
		}
	}
}