From 7d6c9d9b9425973fa440af659a7168c0431c489e Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Mon, 5 Sep 2022 03:58:37 +0100 Subject: [PATCH] Get one template working --- BlueWest.Api/BlueWest.Api.csproj | 2 +- BlueWest.Api/Context/CountryDbContext.cs | 7 ++++--- .../Context/Extensions/EfGetManyAttribute.cs | 12 ------------ .../Context/Extensions/EfGetOneAttribute.cs | 12 ------------ .../Context/Templates/AddToEntityTemplate.csx | 4 ++-- .../Context/Templates/GetOneByTemplate.csx | 16 ++++++++++++++++ BlueWest.Api/Controllers/CountryController.cs | 13 ++++++------- include/BlueWest.MapTo | 2 +- 8 files changed, 30 insertions(+), 38 deletions(-) delete mode 100644 BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs delete mode 100644 BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs create mode 100644 BlueWest.Api/Context/Templates/GetOneByTemplate.csx diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index abae3ba..09cd8f9 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -20,7 +20,7 @@ - + diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index f652ef7..232018d 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -10,6 +10,7 @@ namespace BlueWest.WebApi.EF /// /// Countries and Currencies /// + [EfGenerator] public sealed class CountryDbContext : DbContext { @@ -17,9 +18,9 @@ namespace BlueWest.WebApi.EF /// Countries Database Table /// - [EfGetOne( - returnType: typeof(CountryUnique), - keyMembernameof: nameof(Country.Id)) + [EfGetOneBy( + nameof(Country.Id), + typeof(CountryUnique)) ] [EfAddEntityToList( diff --git a/BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs b/BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs deleted file mode 100644 index 711fa26..0000000 --- a/BlueWest.Api/Context/Extensions/EfGetManyAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace BlueWest.WebApi.EF; - -[AttributeUsage(AttributeTargets.Property)] -public class EfGetManyAttribute : Attribute -{ - public EfGetManyAttribute(Type returnType, string keyMemberName) - { - - } -} \ No newline at end of file diff --git a/BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs b/BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs deleted file mode 100644 index 86a81cf..0000000 --- a/BlueWest.Api/Context/Extensions/EfGetOneAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace BlueWest.WebApi.EF; - -[AttributeUsage(AttributeTargets.Property)] -public sealed class EfGetOneAttribute : Attribute -{ - public EfGetOneAttribute(Type returnType, string keyMembernameof) - { - - } -} \ No newline at end of file diff --git a/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx index b3f3d82..ef7744b 100644 --- a/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx +++ b/BlueWest.Api/Context/Templates/AddToEntityTemplate.csx @@ -1,8 +1,8 @@ /// -/// Adds a new {entityTypeName} to DbSet. +/// Adds a new {entityTypeName} and returns a projection of type {returnTypeFullName}. /// /// The database context. -/// The data type with the add data to create {entityTypeName} +/// Projection data of {entityTypeName} /// The added data. public static (bool, {returnTypeFullName}) Add{entityTypeName}(this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) diff --git a/BlueWest.Api/Context/Templates/GetOneByTemplate.csx b/BlueWest.Api/Context/Templates/GetOneByTemplate.csx new file mode 100644 index 0000000..d886875 --- /dev/null +++ b/BlueWest.Api/Context/Templates/GetOneByTemplate.csx @@ -0,0 +1,16 @@ +/// +/// Gets the first result in the table, and returns a projection of . +/// +/// The database context. +/// By {byParamPropertyName} +/// A bool if the result is successfull, and the first occurrence of {entityTypeName} as a projection + +public static (bool, {returnTypeFullName}) GetOne{entityTypeName}By{byParamPropertyName} (this {contextFullName} dbContext, {byParamFullType} {byParamVarName}) +{ + var {findEntityVarName} = dbContext.{propertyName} + .Where(x => x.{byParamPropertyName} == {byParamVarName}) + .Select(x => new {returnTypeFullName}(x)) + .FirstOrDefault(); + + return ({findEntityVarName} != null, {findEntityVarName}); +} \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index 27cf9e4..2df9cc0 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -16,6 +16,8 @@ namespace BlueWest.WebApi.Controllers [Route("[controller]")] public class CountryController : ControllerBase { + + #region Initialization private readonly CountryDbContext _dbContext; /// @@ -27,7 +29,8 @@ namespace BlueWest.WebApi.Controllers _dbContext = dbContext; } - + #endregion + /// /// Get countries @@ -58,13 +61,9 @@ namespace BlueWest.WebApi.Controllers [HttpGet("{countryId}", Name = nameof(GetCountryById))] public ActionResult GetCountryById(int countryId) { - var country = _dbContext.Countries - .Where(x => x.Id == countryId) - .Select(x => new CountryUnique(x)) - .FirstOrDefault(); + var (success, country) = _dbContext.GetOneCountryById(countryId); - - if (country != null) + if (success) { return Ok(country); } diff --git a/include/BlueWest.MapTo b/include/BlueWest.MapTo index 07c8fc4..32bf670 160000 --- a/include/BlueWest.MapTo +++ b/include/BlueWest.MapTo @@ -1 +1 @@ -Subproject commit 07c8fc48536f3bda6b2f261b4bb3d4e485f0d801 +Subproject commit 32bf670b9e09a8d5c2f130be30b473f9e26cd61f