<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using WebApiOData.V4.Samples.Models;

namespace WebApiOData.V4.Samples.Controllers
{
    public class ProductsController : ODataController
    {
        private static readonly ConcurrentDictionary<int, Product> _data;

        static ProductsController()
        {
            _data = new ConcurrentDictionary<int, Product>();
            var rand = new Random();

            Enumerable.Range(0, 100)
                .Select(i => new Product
                {
                    Id = i,
                    Name = "Product " + i,
                    Price = rand.NextDouble() * 1000
                })
                .ToList()
                .ForEach(p => _data.TryAdd(p.Id, p));
        }

        public ProductsController()
        {
        }

        [Route("*")]
        public IHttpActionResult Default()
        {
            return Ok("OK!!!");
        }

        [EnableQuery]
        public IQueryable<Product> Get()
        {
            return _data.Values.AsQueryable();
        }

        public IHttpActionResult GetProduct(int key)
        {
            Product retval;
            if (_data.TryGetValue(key, out retval))
            {
                return Ok(retval);
            }
            else
            {
                return NotFound();
            }
        }

        [HttpGet]
        public IHttpActionResult MostExpensive()
        {
            var retval = _data.Max(pair => pair.Value.Price);

            return Ok(retval);
        }

        // Returns the top ten most expensive products
        [HttpGet]
        public IHttpActionResult Top10()
        {
            var retval = _data.Values.OrderByDescending(p => p.Price).Take(10).ToList();

            return Ok(retval);
        }

        [HttpGet]
        public IHttpActionResult GetPriceRank(int key)
        {
            Product p;
            if (_data.TryGetValue(key, out p))
            {
                // NOTE: Use where clause to get the rank of the price may not
                // offer the good time complexity. The following code is intended
                // for demostration only.
                return Ok(_data.Values.Where(one => one.Price > p.Price).Count());
            }
            else
            {
                return NotFound();
            }
        }

        [HttpGet]
        public IHttpActionResult CalculateGeneralSalesTax(int key, string state)
        {
            double taxRate = GetRate(state);

            Product product;
            if (_data.TryGetValue(key, out product))
            {
                double tax = product.Price * taxRate / 100;
                return Ok(tax);
            }
            else
            {
                return NotFound();
            }
        }

        [HttpGet]
        [ODataRoute("GetSalesTaxRate(state={state})")]
        public IHttpActionResult GetSalesTaxRate([FromODataUri] string state)
        {
            return Ok(GetRate(state));
        }
        
        [HttpGet]
        [EnableQuery(MaxTop = Int32.MaxValue)]
        [ODataRoute("Products({key})/Default.Placements()")]
        public IHttpActionResult Placements([FromODataUri] int key, ODataQueryOptions<Movie> options)
        {
            var source = new MoviesContext().Movies.Where(x=>x.ID < key).AsQueryable();
            return Ok(source);
        }

        private static double GetRate(string state)
        {
            double taxRate = 0;
            switch (state)
            {
                case "AZ": taxRate = 5.6; break;
                case "CA": taxRate = 7.5; break;
                case "CT": taxRate = 6.35; break;
                case "GA": taxRate = 4; break;
                case "IN": taxRate = 7; break;
                case "KS": taxRate = 6.15; break;
                case "KY": taxRate = 6; break;
                case "MA": taxRate = 6.25; break;
                case "NV": taxRate = 6.85; break;
                case "NJ": taxRate = 7; break;
                case "NY": taxRate = 4; break;
                case "NC": taxRate = 4.75; break;
                case "ND": taxRate = 5; break;
                case "PA": taxRate = 6; break;
                case "TN": taxRate = 7; break;
                case "TX": taxRate = 6.25; break;
                case "VA": taxRate = 4.3; break;
                case "WA": taxRate = 6.5; break;
                case "WV": taxRate = 6; break;
                case "WI": taxRate = 5; break;

                default:
                    taxRate = 0;
                    break;
            }

            return taxRate;
        }
    }
}
<commit_msg>remove needless action attribute<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using WebApiOData.V4.Samples.Models;

namespace WebApiOData.V4.Samples.Controllers
{
    public class ProductsController : ODataController
    {
        private static readonly ConcurrentDictionary<int, Product> _data;

        static ProductsController()
        {
            _data = new ConcurrentDictionary<int, Product>();
            var rand = new Random();

            Enumerable.Range(0, 100)
                .Select(i => new Product
                {
                    Id = i,
                    Name = "Product " + i,
                    Price = rand.NextDouble() * 1000
                })
                .ToList()
                .ForEach(p => _data.TryAdd(p.Id, p));
        }

        public ProductsController()
        {
        }

        [Route("*")]
        public IHttpActionResult Default()
        {
            return Ok("OK!!!");
        }

        [EnableQuery]
        public IQueryable<Product> Get()
        {
            return _data.Values.AsQueryable();
        }

        public IHttpActionResult GetProduct(int key)
        {
            Product retval;
            if (_data.TryGetValue(key, out retval))
            {
                return Ok(retval);
            }
            else
            {
                return NotFound();
            }
        }

        [HttpGet]
        public IHttpActionResult MostExpensive()
        {
            var retval = _data.Max(pair => pair.Value.Price);

            return Ok(retval);
        }

        // Returns the top ten most expensive products
        [HttpGet]
        public IHttpActionResult Top10()
        {
            var retval = _data.Values.OrderByDescending(p => p.Price).Take(10).ToList();

            return Ok(retval);
        }

        [HttpGet]
        public IHttpActionResult GetPriceRank(int key)
        {
            Product p;
            if (_data.TryGetValue(key, out p))
            {
                // NOTE: Use where clause to get the rank of the price may not
                // offer the good time complexity. The following code is intended
                // for demostration only.
                return Ok(_data.Values.Where(one => one.Price > p.Price).Count());
            }
            else
            {
                return NotFound();
            }
        }

        [HttpGet]
        public IHttpActionResult CalculateGeneralSalesTax(int key, string state)
        {
            double taxRate = GetRate(state);

            Product product;
            if (_data.TryGetValue(key, out product))
            {
                double tax = product.Price * taxRate / 100;
                return Ok(tax);
            }
            else
            {
                return NotFound();
            }
        }

        [HttpGet]
        [ODataRoute("GetSalesTaxRate(state={state})")]
        public IHttpActionResult GetSalesTaxRate([FromODataUri] string state)
        {
            return Ok(GetRate(state));
        }
        
        [HttpGet]
        [EnableQuery]
        [ODataRoute("Products({key})/Default.Placements()")]
        public IHttpActionResult Placements([FromODataUri] int key, ODataQueryOptions<Movie> options)
        {
            var source = new MoviesContext().Movies.Where(x=>x.ID < key).AsQueryable();
            return Ok(source);
        }

        private static double GetRate(string state)
        {
            double taxRate = 0;
            switch (state)
            {
                case "AZ": taxRate = 5.6; break;
                case "CA": taxRate = 7.5; break;
                case "CT": taxRate = 6.35; break;
                case "GA": taxRate = 4; break;
                case "IN": taxRate = 7; break;
                case "KS": taxRate = 6.15; break;
                case "KY": taxRate = 6; break;
                case "MA": taxRate = 6.25; break;
                case "NV": taxRate = 6.85; break;
                case "NJ": taxRate = 7; break;
                case "NY": taxRate = 4; break;
                case "NC": taxRate = 4.75; break;
                case "ND": taxRate = 5; break;
                case "PA": taxRate = 6; break;
                case "TN": taxRate = 7; break;
                case "TX": taxRate = 6.25; break;
                case "VA": taxRate = 4.3; break;
                case "WA": taxRate = 6.5; break;
                case "WV": taxRate = 6; break;
                case "WI": taxRate = 5; break;

                default:
                    taxRate = 0;
                    break;
            }

            return taxRate;
        }
    }
}

{'fraction_non_alphanumeric': 0.10182876142975894, 'fraction_numerical': 0.011637572734829594, 'mean_word_length': 1.3231957518706252, 'pattern_counts': {'":': 40, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2765030', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2515, 'n_words': 654}