
namespace SalesAnalysis.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // Implements application logic using the northwndEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class NorthWindDomainService : LinqToEntitiesDomainService<northwndEntities>
    {
        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Invoices' query.
        public IQueryable<Invoice> GetInvoices()
        {
            return this.ObjectContext.Invoices;
        }

        // get sales by country
        // the data is grouped, aggregated, and sorted on the server
        // the final result is all that is sent to the client (only 15 records)
        public IQueryable<ChartDataPoint> GetSalesByCountry()
        {
            return (
                from inv in this.ObjectContext.Invoices // get invoices
                    group inv by inv.Country into g // group by country
                    let amount = g.Sum(inv => inv.ExtendedPrice) // add sales amounts
                    orderby amount descending // sort by amount
                    select new ChartDataPoint() { Item = g.Key + " ", Amount = amount }) // select result
                .Take(15);
        }

        // get sales by product
        // the data is grouped, aggregated, and sorted on the server
        // the final result is all that is sent to the client (only 15 records)
        public IQueryable<ChartDataPoint> GetSalesByProduct()
        {
            return (
                from inv in this.ObjectContext.Invoices // get invoices
                    group inv by inv.ProductName into g // group by product
                    let amount = g.Sum(inv => inv.ExtendedPrice) // add sales amounts
                    orderby amount descending // sort by amount
                    select new ChartDataPoint() { Item = g.Key + " ", Amount = amount }) // select result
                .Take(15); // select top 15 items
        }
    }

    // class exposed to the client
    // a parameterless constructor is required for serialization
    // for details, see 
    //  http://blogs.msdn.com/b/deepm/archive/2009/11/20/wcf-ria-services-presentation-model-explained.aspx
    public class ChartDataPoint
    {
        public ChartDataPoint() { }
        [Key]
        public string Item { get; set; }
        public decimal? Amount { get; set; }
    }
}


