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

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

        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);
        }
    }
}