using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel.Web;
using System.Web;

namespace SimpleDataServices
{
    /* http://localhost:4434/
     * /PostsDataService.svc/Posts?$filter=Title eq 'Ssak1'
     * /PostsDataService.svc/Posts?$filter=startswith(Title,'Ss')
     * 
     * /PostsDataService.svc/TytulPoLiterze?litera='A'&$filter=startswith(Content,'W')
     */
    public class PostsDataService : DataService<Model.MovieProvider>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            //config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetEntitySetAccessRule("Posts", EntitySetRights.All);
            config.SetServiceOperationAccessRule("TytulPoLiterze", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        [WebGet]
        public IQueryable<Model.Post> TytulPoLiterze(string litera)
        {
            return CurrentDataSource.Posts.Where(p => p.Title.StartsWith(litera));
        }

        [QueryInterceptor("Posts")]
        public Expression<Func<Model.Post, bool>> FilterPosts()
        {
            return post => post.Published.Year == DateTime.Now.Year;
        }
    }
}
