﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Platnosci.Core.Interface;
using Platnosci.Core.Linq;

namespace Platnosci.Core.Linq
{
    public sealed class RepositoryPlatnosciEcard : IRepositoryPE
    {
        private readonly PlatnosciDataContext _dataContext = new PlatnosciDataContext();

        public int Count()
        {
            return _dataContext.GetTable<PlatnosciEcard>().Count();
        }
        public int Count(Expression<Func<PlatnosciEcard, bool>> expression)
        {
            return _dataContext.GetTable<PlatnosciEcard>().Count(expression);
        }
        public PlatnosciEcard FindOne(int id)
        {
            return FindOne(t => t.ORDERNUMBER == id);
        }
        public PlatnosciEcard FindOne(Expression<Func<PlatnosciEcard, bool>> expression)
        {
            return _dataContext.GetTable<PlatnosciEcard>().Where(expression).SingleOrDefault();
        }

        public bool TryFindOne(Expression<Func<PlatnosciEcard, bool>> expression, out PlatnosciEcard entity)
        {
            entity = FindOne(expression);

            return (entity != null);
        }
        public bool Exists(Expression<Func<PlatnosciEcard, bool>> expression)
        {
            var entity = FindOne(expression);

            return (entity != null);
        }
        public IList<PlatnosciEcard> FindAll()
        {
            return _dataContext.GetTable<PlatnosciEcard>().ToList();
        }
        public IList<PlatnosciEcard> FindAll(Expression<Func<PlatnosciEcard, bool>> expression)
        {
            return _dataContext.GetTable<PlatnosciEcard>().Where(expression).ToList();
        }

        public void Insert(PlatnosciEcard entity)
        {
            _dataContext.GetTable<PlatnosciEcard>().InsertOnSubmit(entity);
            _dataContext.SubmitChanges();
        }
        public void Delete(PlatnosciEcard entity)
        {
            if (entity != null)
            {
                _dataContext.GetTable<PlatnosciEcard>().DeleteOnSubmit(entity);
                _dataContext.SubmitChanges();
            }
        }
        public void Update(PlatnosciEcard entity)
        {
            _dataContext.SubmitChanges();
        }
        public IQueryable<PlatnosciEcard> Find(int id)
        {
            return _dataContext.GetTable<PlatnosciEcard>().Where(t => t.ORDERNUMBER == id);
        }
        public IQueryable<PlatnosciEcard> Find(Expression<Func<PlatnosciEcard, bool>> expression)
        {
            return _dataContext.GetTable<PlatnosciEcard>().Where(expression);
        }
        public IQueryable<PlatnosciEcard> Find()
        {
            return _dataContext.GetTable<PlatnosciEcard>();
        }
    }
}

