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

View File

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

View File

@ -23,12 +23,16 @@ namespace BlueWest.WebApi.EF
[EfGetOneBy(nameof(Country.Id), typeof(CountryUnique))]
[EfGetOne(typeof(CountryUnique))]
[EfAddMethods(createType: typeof(CountryCreate), returnType: typeof(CountryUnique))]
[EfUpdateMethods( updateType: typeof(CountryUpdate), returnType: typeof(CountryUnique), keyPropertyMemberName: nameof(Country.Id))]
[EfGetMany(typeof(CountryUnique))]
[EfAddToList(nameof(Country.Currencies), typeof(CurrencyCreate), 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; }
#endregion
@ -40,11 +44,15 @@ namespace BlueWest.WebApi.EF
/// </summary>
[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))]
[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; }
#endregion

View File

@ -7,7 +7,7 @@
/// <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 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,
Expression <Func<{returnTypeFullName}, bool> > where = null,
Expression <Func<{returnTypeFullName}, object> > orderBy = null)

View File

@ -3,7 +3,7 @@
/// </summary>
/// <param name="dbContext">The database context.</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})
{
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
#region GetCountries
/// <summary>
@ -47,7 +46,7 @@ namespace BlueWest.WebApi.Controllers
int orderDir = 1)
{
var (success, countries) = _dbContext.GetCountries(skip, take);
var (success, countries) = _dbContext.GetCountries(skip, take, orderDir);
if (!success) return new NotFoundResult();
return Ok(countries);
@ -55,7 +54,6 @@ namespace BlueWest.WebApi.Controllers
#endregion
#region GetCountryById
/// <summary>
@ -80,42 +78,34 @@ namespace BlueWest.WebApi.Controllers
#endregion
#region GetCountryCurrencies
/// <summary>
/// Get currencies of a countryId
/// </summary>
/// <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>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[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();
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(success)
{
if (orderDir == 1) query = query.OrderBy(orderBy);
else query = query.OrderByDescending(orderBy);
return Ok(result);
}
return Ok(query.ToArray());
return new NotFoundResult();
}
#endregion
#region AddCountry
/// <summary>
@ -190,25 +180,17 @@ namespace BlueWest.WebApi.Controllers
[HttpPost("{countryId}/currencies")]
public ActionResult AddCurrencyToCountry(int countryId, CurrencyCreate currencyCreate)
{
/*var (result, message, country) = _dbContext.AddCurrency(countryId, currencyCreate,
new Expression<Func<Currency, bool>>[]
{
x => x.Code == currencyCreate.Code,
y => y.Num == currencyCreate.Num
});
var (success, message, newCurrency) = _dbContext.AddCurrencyToCountry(countryId, currencyCreate);
if (!result)
if(success)
{
return new ConflictObjectResult(message);
return Ok(newCurrency);
}
return Ok(country);*/
throw new NotImplementedException();
return new NotFoundObjectResult(message);
}
#endregion
}
}

View File

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

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