using System;
using System.Data.Services;
using System.Data.Services.Common;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Web;
using AlfredoLotar.Livro.Aspnet.SchoolModel;
using System.Linq.Expressions;

public class WcfDataService : DataService<SchoolEntities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Person", EntitySetRights.AllRead);
        config.SetEntitySetAccessRule("Department", EntitySetRights.AllRead);
        config.SetEntitySetAccessRule("Course", EntitySetRights.AllRead);
        config.SetEntitySetAccessRule("StudentGrade", EntitySetRights.AllRead);
        //config.SetEntitySetAccessRule("*", EntitySetRights.None);        
        config.SetServiceOperationAccessRule("GetDepartmentById", ServiceOperationRights.AllRead);

        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }       

    [WebGet]
    public IQueryable<Department> GetDepartmentById(int id)
    {
        SchoolEntities contexto = this.CurrentDataSource;

        try
        {
            var selectDepartment = from d in contexto.Department
                                   where d.DepartmentID == id
                                   select d;

            return selectDepartment;
        }
        catch (Exception ex)
        {
            throw new ApplicationException("Ocorreu o seguinte erro: {0}", ex);
        }
    }

}
