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>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
|
@ -11,6 +11,11 @@
|
|||
<AnalysisLevel>preview</AnalysisLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Context\Templates\GetManyTemplate.csx" />
|
||||
<None Remove="Context\Templates\GetOneTemplate.csx" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authorization.Policy" Version="2.2.0" />
|
||||
|
@ -21,6 +26,9 @@
|
|||
<AdditionalFiles Include=".\Context\Templates\AddToEntityTemplate.csx" />
|
||||
<AdditionalFiles Include=".\Context\Templates\UpdateEntityTemplate.csx" />
|
||||
<AdditionalFiles Include=".\Context\Templates\GetOneByTemplate.csx" />
|
||||
<AdditionalFiles Include="Context\Templates\GetManyTemplate.csx" />
|
||||
<AdditionalFiles Include="Context\Templates\GetOneTemplate.csx" />
|
||||
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -9,8 +9,10 @@ namespace BlueWest.WebApi.EF
|
|||
/// <summary>
|
||||
/// Context for accessing company data
|
||||
/// </summary>
|
||||
[EfGenerator]
|
||||
public sealed class CompanyDbContext : DbContext
|
||||
{
|
||||
#region Companies
|
||||
/// <summary>
|
||||
/// Companies.
|
||||
/// </summary>
|
||||
|
@ -21,13 +23,11 @@ namespace BlueWest.WebApi.EF
|
|||
keyPropertyMemberName: nameof(Company.Id))
|
||||
]
|
||||
|
||||
/*[EFUpdateMethods(typeof(int),
|
||||
"Id",
|
||||
typeof(CompanyUpdate),
|
||||
typeof(CompanyUnique))]*/
|
||||
|
||||
public DbSet<Company> Companies { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Industries
|
||||
/// <summary>
|
||||
/// Industries.
|
||||
/// </summary>
|
||||
|
@ -41,19 +41,7 @@ namespace BlueWest.WebApi.EF
|
|||
|
||||
public DbSet<Industry> Industries { get; set; }
|
||||
|
||||
void TestExtensions()
|
||||
{
|
||||
// var (result, obj) = this.AddCountry(new CountryCreate());
|
||||
}
|
||||
/// <summary>
|
||||
/// Products.
|
||||
/// </summary>
|
||||
/*
|
||||
[EfAddMethods(typeof(ProductCreate))]
|
||||
[EFUpdateMethods(typeof(int),
|
||||
"Id",
|
||||
typeof(ProductUpdate))]
|
||||
*/
|
||||
#endregion
|
||||
|
||||
public DbSet<Product> Products { get; set; }
|
||||
|
||||
|
|
|
@ -14,39 +14,38 @@ namespace BlueWest.WebApi.EF
|
|||
public sealed class CountryDbContext : DbContext
|
||||
{
|
||||
|
||||
#region Countries
|
||||
/// <summary>
|
||||
/// Countries Database Table
|
||||
/// </summary>
|
||||
|
||||
[EfGetOneBy(
|
||||
nameof(Country.Id),
|
||||
typeof(CountryUnique))
|
||||
]
|
||||
[EfGetOneBy(nameof(Country.Id),typeof(CountryUnique))]
|
||||
|
||||
[EfAddEntityToList(
|
||||
listEntityType: typeof(Currency),
|
||||
listEntityCreateType: typeof(CurrencyCreate),
|
||||
listEntityReturnType: typeof(CurrencyUnique),
|
||||
keyMembernameof: nameof(Country.Id))
|
||||
]
|
||||
[EfGetOne(typeof(CountryUnique))]
|
||||
|
||||
[EfAddMethods(
|
||||
createType: typeof(CountryCreate),
|
||||
returnType: typeof(CountryUnique))
|
||||
]
|
||||
|
||||
[EfUpdateMethods(
|
||||
updateType: typeof(CountryUpdate),
|
||||
returnType: typeof(CountryUnique),
|
||||
keyPropertyMemberName: nameof(Country.Id))
|
||||
]
|
||||
|
||||
[EfGetMany(typeof(CountryUnique))]
|
||||
|
||||
|
||||
public DbSet<Country> Countries { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Currencies
|
||||
|
||||
/// <summary>
|
||||
/// Currencies Database Table
|
||||
/// </summary>
|
||||
// Generate Add extension methods
|
||||
|
||||
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
|
||||
|
||||
[EfUpdateMethods(
|
||||
|
@ -54,11 +53,19 @@ namespace BlueWest.WebApi.EF
|
|||
returnType: typeof(CurrencyUnique),
|
||||
keyPropertyMemberName: nameof(Currency.Id))
|
||||
]
|
||||
|
||||
[EfGetOneBy(
|
||||
nameof(Currency.Id),
|
||||
typeof(CurrencyUnique))]
|
||||
|
||||
[EfGetOne(
|
||||
typeof(CurrencyUnique))]
|
||||
|
||||
public DbSet<Currency> Currencies { get; set; }
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
#endregion
|
||||
|
||||
#region Initialization
|
||||
/// <summary>
|
||||
/// App Configuration
|
||||
/// </summary>
|
||||
|
@ -83,6 +90,7 @@ namespace BlueWest.WebApi.EF
|
|||
base.OnModelCreating(modelBuilder);
|
||||
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>
|
||||
/// 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>
|
||||
/// <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>
|
||||
/// <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}> </returns>
|
||||
|
||||
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.Status404NotFound)]
|
||||
[HttpGet]
|
||||
public ActionResult GetCountries()
|
||||
public ActionResult GetCountries(
|
||||
int skip = 0,
|
||||
int take = 50,
|
||||
int orderDir = 1)
|
||||
{
|
||||
var array = _dbContext
|
||||
.Countries
|
||||
.Select(x => new CountryUnique(x))
|
||||
.ToArray();
|
||||
|
||||
return Ok(array);
|
||||
var (success, countries) = _dbContext.GetCountries(skip, take);
|
||||
if (!success) return new NotFoundResult();
|
||||
return Ok(countries);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ namespace BlueWest.WebApi.Controllers
|
|||
[Route("[controller]")]
|
||||
public partial class CurrencyController : ControllerBase
|
||||
{
|
||||
|
||||
#region Initialization
|
||||
private readonly CountryDbContext _dbContext;
|
||||
|
||||
/// <summary>
|
||||
|
@ -26,6 +28,7 @@ namespace BlueWest.WebApi.Controllers
|
|||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// Gets the currency data from currency table
|
||||
/// </summary>
|
||||
|
@ -68,6 +71,29 @@ namespace BlueWest.WebApi.Controllers
|
|||
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>
|
||||
/// Add Currency to the table of currencies
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue