-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from will-chan-sage/master
Support for Enums types as keys. Closes #108
- Loading branch information
Showing
15 changed files
with
676 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace SwashbuckleODataSample.Models | ||
{ | ||
public class ProductWithEnumKey | ||
{ | ||
[Key] | ||
public MyEnum EnumValue { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public double Price { get; set; } | ||
} | ||
|
||
public class ProductWithCompositeEnumIntKey | ||
{ | ||
[Key] | ||
public MyEnum EnumValue { get; set; } | ||
|
||
[Key] | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public double Price { get; set; } | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
Swashbuckle.OData.Sample/ODataControllers/ProductWithCompositeEnumIntKeysController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Http; | ||
using System.Web.OData; | ||
using SwashbuckleODataSample.Models; | ||
using System; | ||
|
||
namespace SwashbuckleODataSample.ODataControllers | ||
{ | ||
public class ProductWithCompositeEnumIntKeysController : ODataController | ||
{ | ||
private static readonly List<ProductWithCompositeEnumIntKey> DataCompositeKey; | ||
|
||
static ProductWithCompositeEnumIntKeysController() | ||
{ | ||
DataCompositeKey = new List<ProductWithCompositeEnumIntKey>() | ||
{ | ||
{ | ||
new ProductWithCompositeEnumIntKey | ||
{ | ||
EnumValue = MyEnum.ValueOne, | ||
Id = 1, | ||
Name = "ValueOneName", | ||
Price = 101 | ||
} | ||
}, | ||
{ | ||
new ProductWithCompositeEnumIntKey | ||
{ | ||
EnumValue = MyEnum.ValueTwo, | ||
Id = 2, | ||
Name = "ValueTwoName", | ||
Price = 102 | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Query products | ||
/// </summary> | ||
[EnableQuery] | ||
public IQueryable<ProductWithCompositeEnumIntKey> Get() | ||
{ | ||
return DataCompositeKey.AsQueryable(); | ||
} | ||
|
||
/// <summary> | ||
/// Query products by keys | ||
/// </summary> | ||
/// <param name="keyenumValue">key enum value</param> | ||
/// <param name="keyid">key id</param> | ||
/// <returns>composite enum-int key model</returns> | ||
[EnableQuery] | ||
public IHttpActionResult Get([FromODataUri]MyEnum keyenumValue, [FromODataUri]int keyid) | ||
{ | ||
return Ok(DataCompositeKey | ||
.Where(x => x.EnumValue == keyenumValue | ||
&& x.Id == keyid)); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
Swashbuckle.OData.Sample/ODataControllers/ProductWithEnumKeysController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Http; | ||
using System.Web.OData; | ||
using SwashbuckleODataSample.Models; | ||
|
||
namespace SwashbuckleODataSample.ODataControllers | ||
{ | ||
public class ProductWithEnumKeysController : ODataController | ||
{ | ||
private static readonly Dictionary<MyEnum, | ||
ProductWithEnumKey> DataEnumAsKey; | ||
|
||
static ProductWithEnumKeysController() | ||
{ | ||
DataEnumAsKey = new Dictionary<MyEnum, ProductWithEnumKey>() | ||
{ | ||
{ | ||
MyEnum.ValueOne, | ||
new ProductWithEnumKey { | ||
EnumValue = MyEnum.ValueOne, | ||
Name = "ValueOneName", | ||
Price = 101 | ||
} | ||
}, | ||
{ | ||
MyEnum.ValueTwo, | ||
new ProductWithEnumKey | ||
{ | ||
EnumValue = MyEnum.ValueTwo, | ||
Name = "ValueTwoName", | ||
Price = 102 | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Query products | ||
/// </summary> | ||
[HttpGet] | ||
[EnableQuery] | ||
public IQueryable<ProductWithEnumKey> Get() | ||
{ | ||
return DataEnumAsKey.Values.AsQueryable(); | ||
} | ||
|
||
/// <summary> | ||
/// Query product by enum key | ||
/// </summary> | ||
/// <param name="Key">key enum value</param> | ||
/// <returns>project enum model</returns> | ||
[HttpGet] | ||
public IHttpActionResult Get([FromODataUri]MyEnum Key) | ||
{ | ||
return Ok(DataEnumAsKey[Key]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.