Add GetOneFromListTemplate

This commit is contained in:
CodeLiturgy 2022-09-09 04:26:16 +01:00
parent 924865066d
commit 463acc9c7f
9 changed files with 105 additions and 97 deletions

View File

@ -11,11 +11,6 @@
<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" />
@ -41,6 +36,7 @@
<AdditionalFiles Include="Context\Templates\GetOneTemplate.csx" /> <AdditionalFiles Include="Context\Templates\GetOneTemplate.csx" />
<AdditionalFiles Include="Context\Templates\GetListTemplate.csx" /> <AdditionalFiles Include="Context\Templates\GetListTemplate.csx" />
<AdditionalFiles Include="Context\Templates\AddToListTemplate.csx" /> <AdditionalFiles Include="Context\Templates\AddToListTemplate.csx" />
<AdditionalFiles Include="Context\Templates\GetOneFromListTemplate.csx" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -17,12 +17,7 @@ namespace BlueWest.WebApi.EF
/// Companies. /// Companies.
/// </summary> /// </summary>
[EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))] [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))]
[EfUpdateMethods(typeof(CompanyUpdate),returnType: typeof(CompanyUnique),nameof(Company.Id))]
[EfUpdateMethods(
updateType: typeof(CompanyUpdate),
returnType: typeof(CompanyUnique),
keyPropertyMemberName: nameof(Company.Id))
]
public DbSet<Company> Companies { get; set; } public DbSet<Company> Companies { get; set; }

View File

@ -23,12 +23,16 @@ namespace BlueWest.WebApi.EF
[EfGetOneBy(nameof(Country.Id), typeof(CountryUnique))] [EfGetOneBy(nameof(Country.Id), typeof(CountryUnique))]
[EfGetOne(typeof(CountryUnique))] [EfGetOne(typeof(CountryUnique))]
[EfAddMethods(createType: typeof(CountryCreate), returnType: typeof(CountryUnique))] [EfAddMethods(createType: typeof(CountryCreate), returnType: typeof(CountryUnique))]
[EfUpdateMethods( updateType: typeof(CountryUpdate), returnType: typeof(CountryUnique), keyPropertyMemberName: nameof(Country.Id))] [EfUpdateMethods( updateType: typeof(CountryUpdate), returnType: typeof(CountryUnique), keyPropertyMemberName: nameof(Country.Id))]
[EfGetMany(typeof(CountryUnique))] [EfGetMany(typeof(CountryUnique))]
[EfAddToList(nameof(Country.Currencies), typeof(CurrencyCreate), typeof(CurrencyUnique), nameof(Country.Id))]
[EfGetList(nameof(Country.Currencies), typeof(CurrencyUnique), nameof(Country.Id))] [EfGetList(nameof(Country.Currencies), typeof(CurrencyUnique), nameof(Country.Id))]
[EfAddToList(nameof(Country.Currencies), typeof(CurrencyCreate), typeof(CurrencyUnique), nameof(Country.Id))]
[EfGetOneFromList(nameof(Country.Id), nameof(Country.Currencies), nameof(Currency.Id), typeof(CurrencyUnique))]
public DbSet<Country> Countries { get; set; } public DbSet<Country> Countries { get; set; }
#endregion #endregion
@ -40,11 +44,15 @@ namespace BlueWest.WebApi.EF
/// </summary> /// </summary>
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))] [EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
[EfUpdateMethods( updateType: typeof(CurrencyUpdate), returnType: typeof(CurrencyUnique), keyPropertyMemberName: nameof(Currency.Id))] [EfUpdateMethods(typeof(CurrencyUpdate), typeof(CurrencyUnique), nameof(Currency.Id))]
[EfGetOneBy(nameof(Currency.Id), typeof(CurrencyUnique))] [EfGetOneBy(nameof(Currency.Id), typeof(CurrencyUnique))]
[EfGetOne(typeof(CurrencyUnique))] [EfGetOne(typeof(CurrencyUnique))]
[EfGetList(nameof(Currency.Countries), typeof(CountryUnique), nameof(Currency.Id)) [EfGetMany(typeof(CurrencyUnique))]
]
[EfGetList(nameof(Currency.Countries), typeof(CountryUnique), nameof(Currency.Id))]
[EfGetOneFromList(nameof(Currency.Id), nameof(Currency.Countries), nameof(Country.Id), typeof(CountryUnique))]
public DbSet<Currency> Currencies { get; set; } public DbSet<Currency> Currencies { get; set; }
#endregion #endregion

View File

@ -7,7 +7,7 @@
/// <param name="orderDir">Optional Order direction</param> /// <param name="orderDir">Optional Order direction</param>
/// <param name="where">Optional where predicate.</param> /// <param name="where">Optional where predicate.</param>
/// <param name="orderBy">Optional order by predicate.</param> /// <param name="orderBy">Optional order by predicate.</param>
/// <returns>A bool if the result is successful and has at least 1 occurrence of {propertyName}. </returns> /// <returns>A bool if the result is successful and a projection of the first occurrence of {propertyName}. </returns>
public static (bool, {returnTypeFullName}[]) Get{propertyName}(this {contextFullName} dbContext, int skip = 0, int take = 50, int orderDir = 1, 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}, bool> > where = null,
Expression <Func<{returnTypeFullName}, object> > orderBy = null) Expression <Func<{returnTypeFullName}, object> > orderBy = null)

View File

@ -3,7 +3,7 @@
/// </summary> /// </summary>
/// <param name="dbContext">The database context.</param> /// <param name="dbContext">The database context.</param>
/// <param name="{byParamVarName}">By {byParamPropertyName} parameter type.</param> /// <param name="{byParamVarName}">By {byParamPropertyName} parameter type.</param>
/// <returns>A bool if the result is successfull, and the first occurrence of {entityTypeName}> </returns> /// <returns>A projection 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})
{ {
var {findEntityVarName} = dbContext.{propertyName} var {findEntityVarName} = dbContext.{propertyName}

View File

@ -0,0 +1,20 @@
/// <summary>
/// Gets a projection of the first <see cref="{returnTypeFullName}">{listItemTypeName}</see> occurrence.
/// </summary>
/// <param name="dbContext">The <see cref="{contextFullName}">Database</see> context.</param>
/// <param name="{primaryKeyVarName}">{entityTypeName} lookup key.</param>
/// <param name="{listPrimaryKeyVarName}">{listItemTypeName} lookup key.</param>
/// <returns>A <see cref="{returnTypeFullName}">projection</see> of {listItemTypeName} </returns>
public static (bool, {returnTypeFullName}) Get{listItemTypeName}From{entityTypeName} (this {contextFullName} dbContext, {primaryKeyFullName} {primaryKeyVarName},
{listPrimaryKeyFullName} {listPrimaryKeyVarName})
{
var query =
from mainEntity in dbContext.{propertyName}
where mainEntity.{primaryKeyPropertyName} == {primaryKeyVarName}
let list = mainEntity.{listPropertyName}
from listItem in list
where listItem.{listPrimaryKeyPropertyName} == {listPrimaryKeyVarName}
select new {returnTypeFullName}(listItem);
return (query.Any(), query.FirstOrDefault());
}

View File

@ -31,7 +31,6 @@ namespace BlueWest.WebApi.Controllers
#endregion #endregion
#region GetCountries #region GetCountries
/// <summary> /// <summary>
@ -47,7 +46,7 @@ namespace BlueWest.WebApi.Controllers
int orderDir = 1) int orderDir = 1)
{ {
var (success, countries) = _dbContext.GetCountries(skip, take); var (success, countries) = _dbContext.GetCountries(skip, take, orderDir);
if (!success) return new NotFoundResult(); if (!success) return new NotFoundResult();
return Ok(countries); return Ok(countries);
@ -55,7 +54,6 @@ namespace BlueWest.WebApi.Controllers
#endregion #endregion
#region GetCountryById #region GetCountryById
/// <summary> /// <summary>
@ -80,42 +78,34 @@ namespace BlueWest.WebApi.Controllers
#endregion #endregion
#region GetCountryCurrencies
/// <summary> /// <summary>
/// Get currencies of a countryId /// Get currencies of a countryId
/// </summary> /// </summary>
/// <param name="countryId"></param> /// <param name="countryId"></param>
/// <param name="skip">How many records to skip</param>
/// <param name="take">How many records to take</param>
/// <param name="orderDir">The order direction</param>
/// <returns></returns> /// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{countryId}/currencies")] [HttpGet("{countryId}/currencies")]
public ActionResult GetCountryCurrencies(int countryId, int skip = 0, int take = 50, int orderDir = 1, Expression<Func<CurrencyUnique, bool>> orderBy = null) public ActionResult GetCountryCurrencies(int countryId, int skip = 0, int take = 50, int orderDir = 1)
{ {
var countryObj = _dbContext.Countries.FirstOrDefault(d => d.Id == countryId); var (success, result) = _dbContext.GetCountryCurrencies(countryId, skip, take, orderDir);
if (countryObj == null) return new NotFoundResult(); if(success)
var currentTake = take;
if (take > 200) currentTake = 200;
var query = _dbContext
.Countries
.Where(data => data.Id == countryId)
.SelectMany(o => o.Currencies)
.Select(x => new CurrencyUnique(x))
.Skip(skip)
.Take(currentTake);
if(orderBy != null)
{ {
if (orderDir == 1) query = query.OrderBy(orderBy); return Ok(result);
else query = query.OrderByDescending(orderBy);
} }
return Ok(query.ToArray()); return new NotFoundResult();
} }
#endregion
#region AddCountry #region AddCountry
/// <summary> /// <summary>
@ -190,25 +180,17 @@ namespace BlueWest.WebApi.Controllers
[HttpPost("{countryId}/currencies")] [HttpPost("{countryId}/currencies")]
public ActionResult AddCurrencyToCountry(int countryId, CurrencyCreate currencyCreate) public ActionResult AddCurrencyToCountry(int countryId, CurrencyCreate currencyCreate)
{ {
/*var (result, message, country) = _dbContext.AddCurrency(countryId, currencyCreate, var (success, message, newCurrency) = _dbContext.AddCurrencyToCountry(countryId, currencyCreate);
new Expression<Func<Currency, bool>>[]
{
x => x.Code == currencyCreate.Code,
y => y.Num == currencyCreate.Num
});
if (!result) if(success)
{ {
return new ConflictObjectResult(message); return Ok(newCurrency);
} }
return Ok(country);*/ return new NotFoundObjectResult(message);
throw new NotImplementedException();
} }
#endregion #endregion
} }
} }

View File

@ -29,6 +29,8 @@ namespace BlueWest.WebApi.Controllers
} }
#endregion #endregion
#region GetCurrencies
/// <summary> /// <summary>
/// Gets the currency data from currency table /// Gets the currency data from currency table
/// </summary> /// </summary>
@ -36,16 +38,20 @@ namespace BlueWest.WebApi.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet] [HttpGet]
public ActionResult GetCurrencies() public ActionResult GetCurrencies(int skip = 0, int take = 50, int orderDir = 1)
{ {
var currencies = _dbContext.Currencies var (success, result) = _dbContext.GetCurrencies(skip, take, orderDir, null, x => x.Id);
.Select(currency => new CurrencyUnique(currency))
.ToArray();
return Ok(currencies); if (success)
{
return Ok(result);
}
return new NotFoundResult();
} }
#endregion
#region GetCurrencyById
/// <summary> /// <summary>
/// Gets a currency by the currency number (id) /// Gets a currency by the currency number (id)
/// </summary> /// </summary>
@ -56,21 +62,20 @@ namespace BlueWest.WebApi.Controllers
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))] [HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
public ActionResult GetCurrencyById(int currencyId) public ActionResult GetCurrencyById(int currencyId)
{ {
var queryResult = var (success, result) = _dbContext.GetOneCurrencyById(currencyId);
from aCurrency in _dbContext.Currencies
where aCurrency.Id == currencyId
select new CurrencyUnique(aCurrency);
var currency = queryResult.SingleOrDefault(); if (success)
if (currency != null)
{ {
return Ok(currency); return Ok(result);
} }
return new NotFoundResult(); return new NotFoundResult();
} }
#endregion
#region GetCurrencyWithCode
/// <summary> /// <summary>
/// Gets a currency by the currency number (id) /// Gets a currency by the currency number (id)
/// </summary> /// </summary>
@ -92,6 +97,9 @@ namespace BlueWest.WebApi.Controllers
return new NotFoundResult(); return new NotFoundResult();
} }
#endregion
#region AddCurrency
/// <summary> /// <summary>
/// Add Currency to the table of currencies /// Add Currency to the table of currencies
/// </summary> /// </summary>
@ -108,10 +116,13 @@ namespace BlueWest.WebApi.Controllers
return new NotFoundResult(); return new NotFoundResult();
} }
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, newCurrency); return CreatedAtRoute(nameof(GetCurrencyById), new { CurrencyId = newCurrency.Code }, newCurrency);
} }
#endregion
#region UpdateCurrency
/// <summary> /// <summary>
/// Update a currency data from the Currency table in the database /// Update a currency data from the Currency table in the database
/// </summary> /// </summary>
@ -133,36 +144,38 @@ namespace BlueWest.WebApi.Controllers
return new NotFoundResult(); return new NotFoundResult();
} }
#endregion
#region GetCountryFromCurrency
/// <summary> /// <summary>
/// Gets a specific country id in a country /// Gets a specific country id in a country
/// </summary> /// </summary>
/// <param name="currencyId">The id of the currency</param> /// <param name="currencyId">The id of the currency</param>
/// <param name="countryId">The id of the country</param> /// <param name="countryId">The id of the country</param>
/// <returns></returns> /// <returns></returns>
/*
* [GetOneFrom(nameof(Currency.Id), nameof(Country.Id), typeof(CountryUnique))]
*/
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{currencyId}/countries/{countryId}", Name = nameof(GetCountryFromCurrency))] [HttpGet("{currencyId}/countries/{countryId}", Name = nameof(GetCountryFromCurrency))]
public ActionResult GetCountryFromCurrency(int currencyId, int countryId) public ActionResult GetCountryFromCurrency(int currencyId, int countryId)
{ {
var countryQuery = var (success, country) = _dbContext.GetCountryFromCurrency(currencyId, countryId);
from aCurrency in _dbContext.Currencies
where aCurrency.Id == currencyId
let countries = aCurrency.Countries
from country in countries
where country.Id == countryId
select new CountryUnique(country);
var queryResult = countryQuery.SingleOrDefault();
if (queryResult == null) if (success)
{ {
return new NotFoundResult(); return Ok(country);
} }
return Ok(queryResult); return new NotFoundResult();
} }
#endregion
#region AddCountry
/// <summary> /// <summary>
/// Add Currency to the table of currencies /// Add Currency to the table of currencies
/// </summary> /// </summary>
@ -171,24 +184,18 @@ namespace BlueWest.WebApi.Controllers
/// <returns></returns> /// <returns></returns>
[ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost("{currencyId}/countries")] [HttpPost("{currencyId}/countries")]
public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate) public ActionResult AddCountry(CountryCreate countryToCreate)
{ {
/*var (success, message, country) = _dbContext.AddCountry(currencyId, countryToCreate, var (success, result) = _dbContext.AddCountry(countryToCreate);
new Expression<Func<Country, bool>>[]
{
x => x.Code == countryToCreate.Code,
y => y.StateName == countryToCreate.StateName,
z => z.Alpha2Code == countryToCreate.Alpha2Code
});
if (!success) if (success)
{ {
return new ConflictObjectResult(message); return Ok(result);
} }
return new BadRequestResult();
return Ok(country);*/
return null;
} }
#endregion
} }
} }

@ -1 +1 @@
Subproject commit dc23c797975f1e4d835b5e67a5c0972813567ce1 Subproject commit 15a0f1a4f43e1ca585dc87e090dab0abd451857d