Get one template working
This commit is contained in:
parent
b1af92179a
commit
7d6c9d9b94
|
@ -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>
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace BlueWest.WebApi.EF;
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Property)]
|
|
||||||
public class EfGetManyAttribute : Attribute
|
|
||||||
{
|
|
||||||
public EfGetManyAttribute(Type returnType, string keyMemberName)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace BlueWest.WebApi.EF;
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Property)]
|
|
||||||
public sealed class EfGetOneAttribute : Attribute
|
|
||||||
{
|
|
||||||
public EfGetOneAttribute(Type returnType, string keyMembernameof)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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})
|
||||||
|
|
|
@ -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});
|
||||||
|
}
|
|
@ -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,7 +29,8 @@ namespace BlueWest.WebApi.Controllers
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get countries
|
/// Get countries
|
||||||
|
@ -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
|
Loading…
Reference in New Issue