| <%@ ServiceHost Language="C#" Factory="System.Data.Services.DataServiceHostFactory, Microsoft.Data.Services, Version=5.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35" |
| Service="DataJS.Tests.V1.FoodStoreDataService" %> |
| |
| // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation |
| // files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, |
| // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
| // Software is furnished to do so, subject to the following conditions: |
| // |
| // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| // |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| |
| namespace DataJS.Tests.V1 |
| { |
| using System; |
| using System.Collections.Generic; |
| using System.Data.Services; |
| using System.Data.Services.Common; |
| using System.Linq; |
| using System.ServiceModel.Web; |
| using System.Web; |
| |
| [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)] |
| public class FoodStoreDataService : DataService<FoodContainer> |
| { |
| // This method is called only once to initialize service-wide policies. |
| public static void InitializeService(DataServiceConfiguration config) |
| { |
| config.SetEntitySetAccessRule("*", EntitySetRights.All); |
| config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); |
| config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V1; |
| config.UseVerboseErrors = true; |
| } |
| |
| [WebInvoke] |
| public void ResetData() |
| { |
| this.CurrentDataSource.ResetData(); |
| } |
| |
| [WebGet] |
| public IQueryable<string> FoodsAvailable() |
| { |
| return this.CurrentDataSource.Foods.Select(food => food.Name); |
| } |
| |
| [WebGet] |
| public IQueryable<Package> PackagingTypes() |
| { |
| return this.CurrentDataSource.Foods.Select(food => food.Packaging); |
| } |
| |
| [WebGet] |
| public string UserNameAndPassword() |
| { |
| var request = WebOperationContext.Current.IncomingRequest; |
| string authorization = request.Headers["Authorization"]; |
| if (String.IsNullOrEmpty(authorization)) |
| { |
| WebOperationContext.Current.OutgoingResponse.Headers["WWW-Authenticate"] = "Basic realm=\"localhost\""; |
| throw new DataServiceException(401, "Access denied in UserNameAndPassword"); |
| } |
| |
| return authorization; |
| } |
| } |
| |
| public class FoodContainer : ReflectionDataContext, IUpdatable |
| { |
| private static bool dataInitialized; |
| |
| public IQueryable<Category> Categories |
| { |
| get { return this.GetResourceSetEntities<Category>("Categories").AsQueryable(); } |
| } |
| |
| public IQueryable<Food> Foods |
| { |
| get { return this.GetResourceSetEntities<Food>("Foods").AsQueryable(); } |
| } |
| |
| public IQueryable<SpecialDay> SpecialDays |
| { |
| get { return this.GetResourceSetEntities<SpecialDay>("SpecialDays").AsQueryable(); } |
| } |
| |
| public void ResetData() |
| { |
| this.ClearData(); |
| |
| int i = 0; |
| Category[] categories = new Category[] |
| { |
| new Category { CategoryID = i++, Name = "Baking Supplies", Foods = new List<Food>() }, |
| new Category { CategoryID = i++, Name = "Condiments", Foods = new List<Food>() }, |
| new Category { CategoryID = i++, Name = "Empty Category", Foods = new List<Food>() } |
| }; |
| Array.ForEach(categories, (category) => this.GetResourceSetEntities<Category>("Categories").Add(category)); |
| |
| i = 0; |
| Food[] foods = new Food[] |
| { |
| new Food() |
| { |
| FoodID = i++, |
| Name = "flour", |
| UnitPrice = .19999, |
| ServingSize = 1, |
| MeasurementUnit = "Cup", |
| ProteinGrams = 3, |
| FatGrams = 1, |
| CarbohydrateGrams = 20, |
| CaloriesPerServing = 140, |
| IsAvailable = true, |
| ExpirationDate = new DateTime(2010, 12, 25, 12, 0, 0), |
| ItemGUID = new Guid("27272727272727272727272727272727"), |
| Weight = 10f, |
| AvailableUnits = 1, |
| |
| Packaging = new Package(){ |
| Type = null, |
| Color = String.Empty, |
| NumberPerPackage = int.MaxValue, |
| RequiresRefridgeration = false, |
| PackageDimensions = new Dimensions() |
| { |
| Length = Decimal.MaxValue, |
| Height = Int16.MaxValue, |
| Width = Int64.MaxValue, |
| Volume = double.MaxValue, |
| }, |
| ShipDate = new DateTime(2000, 12, 29) |
| }, |
| |
| Category = categories[0], |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "sugar", |
| UnitPrice = .2, |
| ServingSize = 1, |
| MeasurementUnit = "tsp", |
| ProteinGrams = 0, |
| FatGrams = 0, |
| CarbohydrateGrams = 4, |
| CaloriesPerServing = 16, |
| IsAvailable = false, |
| ExpirationDate = new DateTime(2011, 12, 28), |
| ItemGUID = new Guid("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
| Weight = 0.1f, |
| AvailableUnits = 0, |
| |
| Packaging = new Package(){ |
| Type = " ", |
| Color = "BLUE", |
| NumberPerPackage = int.MinValue, |
| RequiresRefridgeration = true, |
| PackageDimensions = new Dimensions(){ |
| Length = Decimal.MinValue, |
| Height = Int16.MinValue, |
| Width = Int64.MinValue, |
| Volume = double.MinValue, |
| }, |
| ShipDate = new DateTime(2000, 12, 29), |
| }, |
| |
| Category = categories[1], |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "1 Chicken Egg", |
| UnitPrice = 0.55, |
| MeasurementUnit = null, |
| ServingSize = 1, |
| ProteinGrams = 6, |
| FatGrams = 1, |
| CarbohydrateGrams = 1, |
| CaloriesPerServing = 70, |
| IsAvailable = true, |
| ExpirationDate = new DateTime(2000, 12, 29), |
| ItemGUID = new Guid("00000000000000000000000000000000"), |
| Weight = 0, |
| AvailableUnits = -128, |
| |
| Packaging = new Package(){ |
| Type = "18 - Carton", |
| Color = " brown ", |
| NumberPerPackage = 0, |
| RequiresRefridgeration = true, |
| PackageDimensions = null, |
| ShipDate = new DateTime(2000, 12, 29), |
| }, |
| |
| Category = null, |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Brown Sugar", |
| UnitPrice = 1.6, |
| ServingSize = 1, |
| MeasurementUnit = "TSP.", |
| ProteinGrams = 0, |
| FatGrams = 0, |
| CarbohydrateGrams = 5, |
| CaloriesPerServing = 16, |
| IsAvailable = true, |
| ExpirationDate = new DateTime(2011, 12, 28), |
| ItemGUID = new Guid("0123456789abcdef0123456789abcdef"), |
| Weight = 4.5f, |
| AvailableUnits = 127, |
| Packaging = null, |
| Category = categories[1], |
| }, |
| |
| new PreparedFood() |
| { |
| FoodID = i++, |
| Name = "Cobb Salad", |
| UnitPrice = 1.99, |
| ServingSize = -1, |
| MeasurementUnit = "cups", |
| ProteinGrams = 6, |
| FatGrams = 1, |
| CarbohydrateGrams = 3, |
| CaloriesPerServing = 5, |
| IsAvailable = true, |
| ExpirationDate = new DateTime(2000, 12, 29), |
| ItemGUID = new Guid("0123456789abcdef0123456789abcdef"), |
| Weight = 5.674f, |
| AvailableUnits = 127, |
| Packaging = null, |
| Category = categories[1], |
| Instructions = "1.) Open 2.) Eat", |
| NumberOfIngredients = 4, |
| }, |
| |
| new PreparedFood() |
| { |
| FoodID = i++, |
| Name = "Lasagna", |
| UnitPrice = 0, |
| ServingSize = 8, |
| MeasurementUnit = " servings", |
| ProteinGrams = 100, |
| FatGrams = 4, |
| CarbohydrateGrams = 27, |
| CaloriesPerServing = 389, |
| IsAvailable = true, |
| ExpirationDate = new DateTime(1904, 2, 29), |
| ItemGUID = new Guid("0123456789abcdef0123456789abcdef"), |
| Weight = 0, |
| AvailableUnits = 4, |
| Packaging = new Package(){ |
| Type = "box", |
| Color = " 1 ", |
| NumberPerPackage = 1, |
| RequiresRefridgeration = true, |
| PackageDimensions = new Dimensions(){ |
| Length = 3, |
| Height = 1, |
| Width = 5, |
| Volume = 1.5, |
| }, |
| ShipDate = new DateTime(2000, 12, 29), |
| }, |
| Category = categories[0], |
| Instructions = "Bake in oven", |
| NumberOfIngredients = 15, |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Chocolate" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Pizza" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Avocados" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Quinoa" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Oatmeal" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Peanut Butter" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Banana" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Yam" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Clam" |
| }, |
| |
| new Food() |
| { |
| FoodID = i++, |
| Name = "Spam" |
| } |
| }; |
| Array.ForEach(foods, (food) => this.GetResourceSetEntities<Food>("Foods").Add(food)); |
| |
| categories[0].Foods.Add(foods[0]); |
| categories[1].Foods.Add(foods[2]); |
| categories[1].Foods.Add(foods[3]); |
| |
| SpecialDay[] specialDays = new SpecialDay[] |
| { |
| new SpecialDay { ID = 0, Name = "Some date", Date = new DateTime(2010, 12, 29, 1, 2, 3, 456) }, |
| |
| // These entries deliberately inject "date-like" XML/JSON strings into string properties |
| new SpecialDay { ID = 1, Name = "2010-12-29T01:02:03.456", Date = new DateTime(2010, 12, 29, 1, 2, 3, 456) }, |
| new SpecialDay { ID = 2, Name = "/Date(1293584523456)/", Date = new DateTime(2010, 12, 29, 1, 2, 3, 456) } |
| }; |
| Array.ForEach(specialDays, (specialDay) => this.GetResourceSetEntities<SpecialDay>("SpecialDays").Add(specialDay)); |
| } |
| |
| protected override void EnsureDataIsInitialized() |
| { |
| if (!dataInitialized) |
| { |
| this.ResetData(); |
| dataInitialized = true; |
| } |
| } |
| } |
| |
| public class Category |
| { |
| public int CategoryID { get; set; } |
| public string Name { get; set; } |
| public List<Food> Foods { get; set; } |
| } |
| |
| public class Food |
| { |
| // Primitive types |
| public int FoodID { get; set; } |
| public string Name { get; set; } |
| public double UnitPrice { get; set; } |
| public Decimal ServingSize { get; set; } |
| public string MeasurementUnit { get; set; } |
| public Byte ProteinGrams { get; set; } |
| public Int16 FatGrams { get; set; } |
| public Int32 CarbohydrateGrams { get; set; } |
| public Int64 CaloriesPerServing { get; set; } |
| public Boolean IsAvailable { get; set; } |
| public DateTime ExpirationDate { get; set; } |
| public Guid ItemGUID { get; set; } |
| public Single Weight { get; set; } |
| public sbyte AvailableUnits { get; set; } |
| |
| // Complex types |
| public Package Packaging { get; set; } |
| |
| // Navigation properties |
| public Category Category { get; set; } |
| } |
| |
| public class Package |
| { |
| public string Type { get; set; } |
| public string Color { get; set; } |
| public int NumberPerPackage { get; set; } |
| public Boolean RequiresRefridgeration { get; set; } |
| public DateTime ShipDate { get; set; } |
| public Dimensions PackageDimensions { get; set; } |
| } |
| |
| public class Dimensions |
| { |
| public Decimal Length { get; set; } |
| public Int16 Height { get; set; } |
| public Int64 Width { get; set; } |
| public double Volume { get; set; } |
| } |
| |
| public class PreparedFood : Food |
| { |
| public string Instructions { get; set; } |
| public float NumberOfIngredients { get; set; } |
| } |
| |
| public class SpecialDay |
| { |
| public int ID { get; set; } |
| public string Name { get; set; } |
| public DateTime Date { get; set; } |
| } |
| } |