using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;

namespace Commands2.Web.Services
{
    [EnableClientAccess]
    public class DataService : LinqToEntitiesDomainService<TaskItCommandsEntities>
    {
        public IQueryable<Task> GetTasks()
        {
            return ObjectContext.Tasks;
        }

        public IQueryable<Task> GetTasksByCategory(long id)
        {
            return ObjectContext.Tasks.Where(t => t.TaskCategoryID == id);
        }

        public void InsertTask(Task task)
        {
            if ((task.EntityState != EntityState.Detached))
            {
                ObjectContext.ObjectStateManager.ChangeObjectState(task, EntityState.Added);
            }
            else
            {
                ObjectContext.Tasks.AddObject(task);
            }
        }

        public void UpdateTask(Task currentTask)
        {
            ObjectContext.Tasks.AttachAsModified(currentTask, ChangeSet.GetOriginal(currentTask));
        }

        public void DeleteTask(Task task)
        {
            if ((task.EntityState == EntityState.Detached))
            {
                ObjectContext.Tasks.Attach(task);
            }
            ObjectContext.Tasks.DeleteObject(task);
        }

        public IQueryable<TaskCategory> GetTaskCategories()
        {
            return ObjectContext.TaskCategories;
        }
    }
}