From 55dc8bda8a98c5de36ce738b0af2bb67748943a6 Mon Sep 17 00:00:00 2001 From: Elizabeth Okerio Date: Mon, 15 Jan 2024 17:31:09 +0300 Subject: [PATCH] add more scenarios (#1139) --- .../Controllers/ProductsController.cs | 33 +++++++++++++++++++ sample/BenchmarkServer/EdmModelBuilder.cs | 13 +++++++- sample/BenchmarkServer/Models/Product.cs | 14 +++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/sample/BenchmarkServer/Controllers/ProductsController.cs b/sample/BenchmarkServer/Controllers/ProductsController.cs index dcaa438e1..e578935f4 100644 --- a/sample/BenchmarkServer/Controllers/ProductsController.cs +++ b/sample/BenchmarkServer/Controllers/ProductsController.cs @@ -6,6 +6,7 @@ //------------------------------------------------------------------------------ using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.OData.Formatter; using Microsoft.AspNetCore.OData.Query; using Microsoft.AspNetCore.OData.Routing.Controllers; using ODataPerformanceProfile.Models; @@ -29,6 +30,7 @@ public ProductsController(ProductsContext context) Id = i, Category = "Goods" + i, Color = Color.Red, + Others = new List {"Others1", "Others2", "Others3"}, CreatedDate = new DateTimeOffset(2001, 4, 15, 16, 24, 8, TimeSpan.FromHours(-8)), UpdatedDate = new DateTimeOffset(2011, 2, 15, 16, 24, 8, TimeSpan.FromHours(-8)), Detail = new ProductDetail { Id = "Id" + i, Info = "Info" + i }, @@ -52,6 +54,12 @@ public ProductsController(ProductsContext context) Address = "SupAddre"+i } } + }, + Properties = new Dictionary + { + { "Prop1", new DateTimeOffset(2014, 7, 3, 0, 0, 0, 0, new TimeSpan(0))}, + { "Prop2", new [] { "Leonard G. Lobel", "Eric D. Boyd" }}, + { "Prop3", "Others"} } }; @@ -65,5 +73,30 @@ public IActionResult Get() { return Ok(products); } + + [HttpGet("odata/Products/mostRecent()")] + public IActionResult MostRecent() + { + var maxProductId = products.Max(x => x.Id); + return Ok(maxProductId); + } + + [HttpPost("odata/Products({key})/Rate")] + public IActionResult Rate([FromODataUri] string key, ODataActionParameters parameters) + { + if (!ModelState.IsValid) + { + return BadRequest(); + } + + int rating = (int)parameters["rating"]; + + if (rating < 0) + { + return BadRequest(); + } + + return Ok(new ProductRating() { Id = key, Rating = rating }); + } } } diff --git a/sample/BenchmarkServer/EdmModelBuilder.cs b/sample/BenchmarkServer/EdmModelBuilder.cs index 03af69c8c..c4c8380fd 100644 --- a/sample/BenchmarkServer/EdmModelBuilder.cs +++ b/sample/BenchmarkServer/EdmModelBuilder.cs @@ -20,7 +20,18 @@ public static IEdmModel GetEdmModel() builder.EntitySet("Suppliers"); builder.EntitySet("Orders"); - return builder.GetEdmModel(); + builder.EntityType().Collection + .Function("mostRecent") + .Returns(); + + builder.EntityType() + .Action("rate") + .Parameter("rating"); + + var model = builder.GetEdmModel(); + model.MarkAsImmutable(); + + return model; } } } diff --git a/sample/BenchmarkServer/Models/Product.cs b/sample/BenchmarkServer/Models/Product.cs index 0a9563dc0..36ebb2dd6 100644 --- a/sample/BenchmarkServer/Models/Product.cs +++ b/sample/BenchmarkServer/Models/Product.cs @@ -5,6 +5,8 @@ // //------------------------------------------------------------------------------ +using System.ComponentModel.DataAnnotations; + namespace ODataPerformanceProfile.Models { public class Product @@ -12,11 +14,15 @@ public class Product public int Id { get; set; } public string Category { get; set; } public Color Color { get; set; } + public IList Others { get; set; } public DateTimeOffset CreatedDate { get; set; } + + [ConcurrencyCheck] public DateTimeOffset? UpdatedDate { get; set; } public virtual ProductDetail Detail { get; set; } public virtual ICollection ProductSuppliers { get; set; } - public virtual ICollection ProductOrders { get; set; } + public virtual ICollection ProductOrders { get; set; } + public IDictionary Properties { get; set; } } public class ProductDetail @@ -51,4 +57,10 @@ public class Order public int Id { get; set; } public string OrderNo { get; set; } } + + public class ProductRating + { + public string Id { get; set; } + public int Rating { get; set; } + } }