Get one template working

This commit is contained in:
CodeLiturgy 2022-09-05 03:58:37 +01:00
parent b1af92179a
commit 7d6c9d9b94
8 changed files with 30 additions and 38 deletions

View File

@ -20,7 +20,7 @@
<ItemGroup> <ItemGroup>
<AdditionalFiles Include=".\Context\Templates\AddToEntityTemplate.csx" /> <AdditionalFiles Include=".\Context\Templates\AddToEntityTemplate.csx" />
<AdditionalFiles Include=".\Context\Templates\UpdateEntityTemplate.csx" /> <AdditionalFiles Include=".\Context\Templates\UpdateEntityTemplate.csx" />
<AdditionalFiles Include=".\Context\Templates\GetOneByTemplate.csx" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -10,6 +10,7 @@ namespace BlueWest.WebApi.EF
/// <summary> /// <summary>
/// Countries and Currencies /// Countries and Currencies
/// </summary> /// </summary>
[EfGenerator]
public sealed class CountryDbContext : DbContext public sealed class CountryDbContext : DbContext
{ {
@ -17,9 +18,9 @@ namespace BlueWest.WebApi.EF
/// Countries Database Table /// Countries Database Table
/// </summary> /// </summary>
[EfGetOne( [EfGetOneBy(
returnType: typeof(CountryUnique), nameof(Country.Id),
keyMembernameof: nameof(Country.Id)) typeof(CountryUnique))
] ]
[EfAddEntityToList( [EfAddEntityToList(

View File

@ -1,12 +0,0 @@
using System;
namespace BlueWest.WebApi.EF;
[AttributeUsage(AttributeTargets.Property)]
public class EfGetManyAttribute : Attribute
{
public EfGetManyAttribute(Type returnType, string keyMemberName)
{
}
}

View File

@ -1,12 +0,0 @@
using System;
namespace BlueWest.WebApi.EF;
[AttributeUsage(AttributeTargets.Property)]
public sealed class EfGetOneAttribute : Attribute
{
public EfGetOneAttribute(Type returnType, string keyMembernameof)
{
}
}

View File

@ -1,8 +1,8 @@
/// <summary> /// <summary>
/// Adds a new {entityTypeName} to <see cref="{contextFullName}"></see> DbSet. /// Adds a new {entityTypeName} and returns a projection of type {returnTypeFullName}.
/// </summary> /// </summary>
/// <param name="dbContext">The database context.</param> /// <param name="dbContext">The database context.</param>
/// <param name="{toCreateVarName}">The data type with the add data to create {entityTypeName}</param> /// <param name="{toCreateVarName}">Projection data of {entityTypeName}</param>
/// <returns>The added data.</returns> /// <returns>The added data.</returns>
public static (bool, {returnTypeFullName}) Add{entityTypeName}(this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) public static (bool, {returnTypeFullName}) Add{entityTypeName}(this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName})

View File

@ -0,0 +1,16 @@
/// <summary>
/// Gets the first result in the table, and returns a projection of <see cref="{returnTypeFullName}"></see> .
/// </summary>
/// <param name="dbContext">The database context.</param>
/// <param name="{byParamVarName}">By {byParamPropertyName} <see cref="{byParamFullType}"></see></param>
/// <returns>A bool if the result is successfull, and the first occurrence of {entityTypeName} as a projection <see cref="{returnTypeFullName}"> </returns>
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});
}

View File

@ -16,6 +16,8 @@ namespace BlueWest.WebApi.Controllers
[Route("[controller]")] [Route("[controller]")]
public class CountryController : ControllerBase public class CountryController : ControllerBase
{ {
#region Initialization
private readonly CountryDbContext _dbContext; private readonly CountryDbContext _dbContext;
/// <summary> /// <summary>
@ -27,6 +29,7 @@ namespace BlueWest.WebApi.Controllers
_dbContext = dbContext; _dbContext = dbContext;
} }
#endregion
/// <summary> /// <summary>
@ -58,13 +61,9 @@ namespace BlueWest.WebApi.Controllers
[HttpGet("{countryId}", Name = nameof(GetCountryById))] [HttpGet("{countryId}", Name = nameof(GetCountryById))]
public ActionResult GetCountryById(int countryId) public ActionResult GetCountryById(int countryId)
{ {
var country = _dbContext.Countries var (success, country) = _dbContext.GetOneCountryById(countryId);
.Where(x => x.Id == countryId)
.Select(x => new CountryUnique(x))
.FirstOrDefault();
if (success)
if (country != null)
{ {
return Ok(country); return Ok(country);
} }

@ -1 +1 @@
Subproject commit 07c8fc48536f3bda6b2f261b4bb3d4e485f0d801 Subproject commit 32bf670b9e09a8d5c2f130be30b473f9e26cd61f