Using GetOne and GetMany templates
This commit is contained in:
parent
7d6c9d9b94
commit
e08a11e9ce
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
@ -11,6 +11,11 @@
|
||||||
<AnalysisLevel>preview</AnalysisLevel>
|
<AnalysisLevel>preview</AnalysisLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Context\Templates\GetManyTemplate.csx" />
|
||||||
|
<None Remove="Context\Templates\GetOneTemplate.csx" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authorization.Policy" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Authorization.Policy" Version="2.2.0" />
|
||||||
|
@ -21,6 +26,9 @@
|
||||||
<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" />
|
<AdditionalFiles Include=".\Context\Templates\GetOneByTemplate.csx" />
|
||||||
|
<AdditionalFiles Include="Context\Templates\GetManyTemplate.csx" />
|
||||||
|
<AdditionalFiles Include="Context\Templates\GetOneTemplate.csx" />
|
||||||
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -9,8 +9,10 @@ namespace BlueWest.WebApi.EF
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Context for accessing company data
|
/// Context for accessing company data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[EfGenerator]
|
||||||
public sealed class CompanyDbContext : DbContext
|
public sealed class CompanyDbContext : DbContext
|
||||||
{
|
{
|
||||||
|
#region Companies
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Companies.
|
/// Companies.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -21,13 +23,11 @@ namespace BlueWest.WebApi.EF
|
||||||
keyPropertyMemberName: nameof(Company.Id))
|
keyPropertyMemberName: nameof(Company.Id))
|
||||||
]
|
]
|
||||||
|
|
||||||
/*[EFUpdateMethods(typeof(int),
|
|
||||||
"Id",
|
|
||||||
typeof(CompanyUpdate),
|
|
||||||
typeof(CompanyUnique))]*/
|
|
||||||
|
|
||||||
public DbSet<Company> Companies { get; set; }
|
public DbSet<Company> Companies { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Industries
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Industries.
|
/// Industries.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -41,20 +41,8 @@ namespace BlueWest.WebApi.EF
|
||||||
|
|
||||||
public DbSet<Industry> Industries { get; set; }
|
public DbSet<Industry> Industries { get; set; }
|
||||||
|
|
||||||
void TestExtensions()
|
#endregion
|
||||||
{
|
|
||||||
// var (result, obj) = this.AddCountry(new CountryCreate());
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Products.
|
|
||||||
/// </summary>
|
|
||||||
/*
|
|
||||||
[EfAddMethods(typeof(ProductCreate))]
|
|
||||||
[EFUpdateMethods(typeof(int),
|
|
||||||
"Id",
|
|
||||||
typeof(ProductUpdate))]
|
|
||||||
*/
|
|
||||||
|
|
||||||
public DbSet<Product> Products { get; set; }
|
public DbSet<Product> Products { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,39 +14,38 @@ namespace BlueWest.WebApi.EF
|
||||||
public sealed class CountryDbContext : DbContext
|
public sealed class CountryDbContext : DbContext
|
||||||
{
|
{
|
||||||
|
|
||||||
|
#region Countries
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Countries Database Table
|
/// Countries Database Table
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
[EfGetOneBy(
|
[EfGetOneBy(nameof(Country.Id),typeof(CountryUnique))]
|
||||||
nameof(Country.Id),
|
|
||||||
typeof(CountryUnique))
|
[EfGetOne(typeof(CountryUnique))]
|
||||||
]
|
|
||||||
|
|
||||||
[EfAddEntityToList(
|
|
||||||
listEntityType: typeof(Currency),
|
|
||||||
listEntityCreateType: typeof(CurrencyCreate),
|
|
||||||
listEntityReturnType: typeof(CurrencyUnique),
|
|
||||||
keyMembernameof: nameof(Country.Id))
|
|
||||||
]
|
|
||||||
|
|
||||||
[EfAddMethods(
|
[EfAddMethods(
|
||||||
createType: typeof(CountryCreate),
|
createType: typeof(CountryCreate),
|
||||||
returnType: typeof(CountryUnique))
|
returnType: typeof(CountryUnique))
|
||||||
]
|
]
|
||||||
|
|
||||||
[EfUpdateMethods(
|
[EfUpdateMethods(
|
||||||
updateType: typeof(CountryUpdate),
|
updateType: typeof(CountryUpdate),
|
||||||
returnType: typeof(CountryUnique),
|
returnType: typeof(CountryUnique),
|
||||||
keyPropertyMemberName: nameof(Country.Id))
|
keyPropertyMemberName: nameof(Country.Id))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[EfGetMany(typeof(CountryUnique))]
|
||||||
|
|
||||||
|
|
||||||
public DbSet<Country> Countries { get; set; }
|
public DbSet<Country> Countries { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Currencies
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Currencies Database Table
|
/// Currencies Database Table
|
||||||
/// </summary>
|
/// </summary>
|
||||||
// Generate Add extension methods
|
|
||||||
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
|
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
|
||||||
|
|
||||||
[EfUpdateMethods(
|
[EfUpdateMethods(
|
||||||
|
@ -54,17 +53,25 @@ namespace BlueWest.WebApi.EF
|
||||||
returnType: typeof(CurrencyUnique),
|
returnType: typeof(CurrencyUnique),
|
||||||
keyPropertyMemberName: nameof(Currency.Id))
|
keyPropertyMemberName: nameof(Currency.Id))
|
||||||
]
|
]
|
||||||
public DbSet<Currency> Currencies { get; set; }
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
|
[EfGetOneBy(
|
||||||
|
nameof(Currency.Id),
|
||||||
|
typeof(CurrencyUnique))]
|
||||||
|
|
||||||
|
[EfGetOne(
|
||||||
|
typeof(CurrencyUnique))]
|
||||||
|
|
||||||
|
public DbSet<Currency> Currencies { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Initialization
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// App Configuration
|
/// App Configuration
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IConfiguration Configuration;
|
public IConfiguration Configuration;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// CountryDbContext Constructor.
|
/// CountryDbContext Constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -83,6 +90,7 @@ namespace BlueWest.WebApi.EF
|
||||||
base.OnModelCreating(modelBuilder);
|
base.OnModelCreating(modelBuilder);
|
||||||
modelBuilder.ConfigureCurrentDbModel();
|
modelBuilder.ConfigureCurrentDbModel();
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
/// <summary>
|
||||||
|
/// Gets {propertyName}.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbContext">The database context.</param>
|
||||||
|
/// <param name="skip">How many {propertyName} to skip.</param>
|
||||||
|
/// <param name="take">How many {propertyName} to take. (Default = 50)</param>
|
||||||
|
/// <param name="orderDir">Optional Order direction</param>
|
||||||
|
/// <param name="where">Optional where predicate.</param>
|
||||||
|
/// <param name="orderBy">Optional order by predicate.</param>
|
||||||
|
/// <returns>A bool if the result is successfull, and the first occurrence of {entityTypeName}> </returns>
|
||||||
|
public static (bool, {returnTypeFullName}[]) Get{propertyName}(this {contextFullName} dbContext, int skip = 0, int take = 50, int orderDir = 1,
|
||||||
|
Expression <Func<{returnTypeFullName}, bool> > where = null,
|
||||||
|
Expression <Func<{returnTypeFullName}, object> > orderBy = null)
|
||||||
|
{
|
||||||
|
var query = dbContext
|
||||||
|
.{propertyName}
|
||||||
|
.Select(x => new {returnTypeFullName}(x));
|
||||||
|
|
||||||
|
// limit take by 200 records
|
||||||
|
if (take > 200) take = 200;
|
||||||
|
query.Skip(skip).Take(take);
|
||||||
|
|
||||||
|
if (where != null) query.Where(where);
|
||||||
|
if (orderDir == 1) query.OrderBy(orderBy);
|
||||||
|
else query.OrderByDescending(orderBy);
|
||||||
|
|
||||||
|
return (query.Any(), query.ToArray());
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the first result in the table, and returns a projection of <see cref="{returnTypeFullName}"></see> .
|
/// Gets the first result in the table, and returns a projection of <see cref="{returnTypeFullName}"></see>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dbContext">The database context.</param>
|
/// <param name="dbContext">The database context.</param>
|
||||||
/// <param name="{byParamVarName}">By {byParamPropertyName} <see cref="{byParamFullType}"></see></param>
|
/// <param name="{byParamVarName}">By {byParamPropertyName} <see cref="{byParamFullType}">parameter type.</param>
|
||||||
/// <returns>A bool if the result is successfull, and the first occurrence of {entityTypeName} as a projection <see cref="{returnTypeFullName}"> </returns>
|
/// <returns>A bool if the result is successfull, and the first occurrence of {entityTypeName}> </returns>
|
||||||
|
|
||||||
public static (bool, {returnTypeFullName}) GetOne{entityTypeName}By{byParamPropertyName} (this {contextFullName} dbContext, {byParamFullType} {byParamVarName})
|
public static (bool, {returnTypeFullName}) GetOne{entityTypeName}By{byParamPropertyName} (this {contextFullName} dbContext, {byParamFullType} {byParamVarName})
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the first result following the query with a expression.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbContext">The database context.</param>
|
||||||
|
/// <param name="with">FirstOrDefault argument to pass.</param>
|
||||||
|
/// <returns>A bool if the result is successfull, and the first occurrence of {returnTypeName}</returns>
|
||||||
|
|
||||||
|
public static (bool, {returnTypeFullName}) Get{entityTypeName}With (this {contextFullName} dbContext, Expression<Func<{returnTypeFullName},bool>> with)
|
||||||
|
{
|
||||||
|
var {findEntityVarName} =
|
||||||
|
dbContext.{propertyName}
|
||||||
|
.Select(x => new {returnTypeFullName}(x))
|
||||||
|
.FirstOrDefault(with);
|
||||||
|
|
||||||
|
return ({findEntityVarName} != null, {findEntityVarName});
|
||||||
|
|
||||||
|
}
|
|
@ -39,14 +39,14 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public ActionResult GetCountries()
|
public ActionResult GetCountries(
|
||||||
|
int skip = 0,
|
||||||
|
int take = 50,
|
||||||
|
int orderDir = 1)
|
||||||
{
|
{
|
||||||
var array = _dbContext
|
var (success, countries) = _dbContext.GetCountries(skip, take);
|
||||||
.Countries
|
if (!success) return new NotFoundResult();
|
||||||
.Select(x => new CountryUnique(x))
|
return Ok(countries);
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
return Ok(array);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,8 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public partial class CurrencyController : ControllerBase
|
public partial class CurrencyController : ControllerBase
|
||||||
{
|
{
|
||||||
|
|
||||||
|
#region Initialization
|
||||||
private readonly CountryDbContext _dbContext;
|
private readonly CountryDbContext _dbContext;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -26,6 +28,7 @@ namespace BlueWest.WebApi.Controllers
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the currency data from currency table
|
/// Gets the currency data from currency table
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -67,6 +70,29 @@ namespace BlueWest.WebApi.Controllers
|
||||||
|
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a currency by the currency number (id)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="currencyId">The id of the currency to get</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[HttpGet("{currencyCode}", Name = nameof(GetCurrencyWithCode))]
|
||||||
|
public ActionResult GetCurrencyWithCode(Expression<Func<CurrencyUnique, bool>> with)
|
||||||
|
{
|
||||||
|
var currency =
|
||||||
|
_dbContext.Currencies
|
||||||
|
.Select(currency => new CurrencyUnique(currency))
|
||||||
|
.FirstOrDefault(with);
|
||||||
|
|
||||||
|
if (currency != null)
|
||||||
|
{
|
||||||
|
return Ok(currency);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NotFoundResult();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add Currency to the table of currencies
|
/// Add Currency to the table of currencies
|
||||||
|
|
Loading…
Reference in New Issue