Fix miscelaneous errors

This commit is contained in:
CodeLiturgy 2022-09-05 23:37:52 +01:00
parent d0c3c4b960
commit 0981be48ca
12 changed files with 55 additions and 116 deletions

View File

@ -17,6 +17,7 @@ namespace BlueWest.WebApi.EF
/// Companies. /// Companies.
/// </summary> /// </summary>
[EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))] [EfAddMethods(typeof(CompanyCreate), typeof(CompanyUnique))]
[EfUpdateMethods( [EfUpdateMethods(
updateType: typeof(CompanyUpdate), updateType: typeof(CompanyUpdate),
returnType: typeof(CompanyUnique), returnType: typeof(CompanyUnique),
@ -32,6 +33,7 @@ namespace BlueWest.WebApi.EF
/// Industries. /// Industries.
/// </summary> /// </summary>
[EfAddMethods(typeof(IndustryCreate), typeof(IndustryUnique))] [EfAddMethods(typeof(IndustryCreate), typeof(IndustryUnique))]
[EfUpdateMethods( [EfUpdateMethods(
updateType: typeof(IndustryUpdate), updateType: typeof(IndustryUpdate),
returnType: typeof(IndustryUnique), returnType: typeof(IndustryUnique),
@ -43,9 +45,25 @@ namespace BlueWest.WebApi.EF
#endregion #endregion
#region Products
/// <summary>
/// Products
/// </summary>
[EfAddMethods(typeof(ProductCreate), typeof(ProductUnique))]
[EfUpdateMethods(
updateType: typeof(ProductUpdate),
returnType: typeof(ProductUnique),
keyPropertyMemberName: nameof(Product.Id)
)
]
public DbSet<Product> Products { get; set; } public DbSet<Product> Products { get; set; }
#endregion
#region Initialization
/// <summary> /// <summary>
/// CompanyDbContext constructor. /// CompanyDbContext constructor.
@ -66,6 +84,9 @@ namespace BlueWest.WebApi.EF
modelBuilder.ConfigureCurrentDbModel(); modelBuilder.ConfigureCurrentDbModel();
} }
#endregion
} }
} }

View File

@ -1,95 +0,0 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.EF
{
/// <summary>
/// Currency table data extensions
/// </summary>
public static partial class CurrencyExtensions
{
/// <summary>
/// Add new Currency
/// </summary>
/// <param name="dbContext"></param>
/// <param name="currencyToCreate"></param>
/// <returns></returns>
/*
public static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate)
{
var newCurrency = new Currency(currencyToCreate);
dbContext.Add(newCurrency);
var resultOperation = dbContext.SaveChanges() >= 0;
return (resultOperation, newCurrency);
}
*/
/// <summary>
/// Updates currency
/// </summary>
/// <param name="dbContext"></param>
/// <param name="currencyId"></param>
/// <param name="currencyToUpdate"></param>
/// <returns></returns>
/*public static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate)
{
var currency = dbContext.Currencies.SingleOrDefault(x => x.Id == currencyId);
if (currency == null) return (false, null);
currency.Update(currencyToUpdate);
dbContext.Update(currency);
var resultOperation = dbContext.SaveChanges() >= 0;
return (resultOperation, currency);
}*/
/// <summary>
/// Add Country associated with specified Currency.
/// </summary>
/// <param name="dbContext"></param>
/// <param name="currencyId"></param>
/// <param name="countryCreate"></param>
/// <param name="duplicationValidations"></param>
/// <returns></returns>
/*public static (bool, string, CountryUnique) AddCountry(
this CountryDbContext dbContext,
int currencyId, CountryCreate countryCreate,
Expression<Func<Country,bool>>[] duplicationValidations)
{
var queryable = from aCurrency in dbContext.Currencies
where aCurrency.Id == currencyId
select aCurrency;
queryable.Include(x => x.Countries);
var currency = queryable.SingleOrDefault();
// Check if currency exists
if (currency == null) return (false, $"{nameof(Currency)}: {currencyId} - Not found.", null);
// Check if there's currency with the same code
foreach (var duplicationValidation in duplicationValidations)
{
var countryToCheck = dbContext.Countries.FirstOrDefault(duplicationValidation);
if (countryToCheck != null)
{
return (false, $"Duplication Validation failed: {duplicationValidation.Body}", null);
}
}
// Creates new currency
var newCountry = new Country(countryCreate);
currency.Countries.Add(newCountry);
var success = dbContext.SaveChanges() >= 0;
return (success, string.Empty, new CountryUnique(newCountry));
}*/
}
}

View File

@ -1,9 +0,0 @@
using System;
namespace BlueWest.WebApi.EF;
[AttributeUsage(AttributeTargets.Property)]
public class EfAddEntityToListAttribute : Attribute
{
public EfAddEntityToListAttribute(Type listEntityType, Type listEntityCreateType, Type listEntityReturnType, string keyMembernameof) { }
}

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 successfull, and the first occurrence of {entityTypeName}> </returns> /// <returns>A bool if the result is successful and has at least 1 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

@ -2,7 +2,7 @@
/// 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}">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 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})

View File

@ -3,7 +3,7 @@
/// </summary> /// </summary>
/// <param name="dbContext">The database context.</param> /// <param name="dbContext">The database context.</param>
/// <param name="with">FirstOrDefault argument to pass.</param> /// <param name="with">FirstOrDefault argument to pass.</param>
/// <returns>A bool if the result is successfull, and the first occurrence of {returnTypeName}</returns> /// <returns>A bool if the first occurrence of {returnTypeName} is returned.</returns>
public static (bool, {returnTypeFullName}) Get{entityTypeName}With (this {contextFullName} dbContext, Expression<Func<{returnTypeFullName},bool>> with) public static (bool, {returnTypeFullName}) Get{entityTypeName}With (this {contextFullName} dbContext, Expression<Func<{returnTypeFullName},bool>> with)
{ {

View File

@ -3,6 +3,7 @@
/// </summary> /// </summary>
/// <param name="dbContext">Database context</param> /// <param name="dbContext">Database context</param>
/// <param name="{updateVarName}">The data type with the add data to update {entityTypeName}</param> /// <param name="{updateVarName}">The data type with the add data to update {entityTypeName}</param>
/// <param name="{keyVarName}"> The primary key.</param>
/// <returns>Returns the current data.</returns> /// <returns>Returns the current data.</returns>
public static (bool, {returnTypeFullName}) Update{entityTypeName}( this {contextFullName} dbContext, {updateTypeFullName} {updateVarName}, {keyTypeFullName} {keyVarName}) public static (bool, {returnTypeFullName}) Update{entityTypeName}( this {contextFullName} dbContext, {updateTypeFullName} {updateVarName}, {keyTypeFullName} {keyVarName})
{ {

View File

@ -79,14 +79,12 @@ namespace BlueWest.WebApi.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{currencyCode}", Name = nameof(GetCurrencyWithCode))] [HttpGet("{currencyCode}", Name = nameof(GetCurrencyWithCode))]
public ActionResult GetCurrencyWithCode(Expression<Func<CurrencyUnique, bool>> with) public ActionResult GetCurrencyWithCode(string currencyCode)
{ {
var currency = var (success, currency) =
_dbContext.Currencies _dbContext.GetCurrencyWith(x => x.Code == currencyCode);
.Select(currency => new CurrencyUnique(currency))
.FirstOrDefault(with);
if (currency != null) if (success)
{ {
return Ok(currency); return Ok(currency);
} }

View File

@ -26,6 +26,11 @@ RUN dotnet restore "BlueWest.Api/BlueWest.Api.csproj"
COPY ["include/BlueWest.MapTo/src/BlueWest.MapTo/BlueWest.MapTo.csproj", "include/BlueWest.MapTo/src/BlueWest.MapTo/"] COPY ["include/BlueWest.MapTo/src/BlueWest.MapTo/BlueWest.MapTo.csproj", "include/BlueWest.MapTo/src/BlueWest.MapTo/"]
RUN dotnet restore "include/BlueWest.MapTo/src/BlueWest.MapTo/BlueWest.MapTo.csproj" RUN dotnet restore "include/BlueWest.MapTo/src/BlueWest.MapTo/BlueWest.MapTo.csproj"
COPY ["include/BlueWest.EfMethods/src/BlueWest.EfMethods/BlueWest.EfMethods.csproj", "include/BlueWest.EfMethods/src/BlueWest.EfMethods/"]
RUN dotnet restore "include/BlueWest.EfMethods/src/BlueWest.EfMethods/BlueWest.EfMethods.csproj"
COPY ["include/Math-Expression-Evaluator/SimpleExpressionEvaluator/SimpleExpressionEvaluator.csproj", "include/Math-Expression-Evaluator/SimpleExpressionEvaluator/"] COPY ["include/Math-Expression-Evaluator/SimpleExpressionEvaluator/SimpleExpressionEvaluator.csproj", "include/Math-Expression-Evaluator/SimpleExpressionEvaluator/"]
RUN dotnet restore "include/Math-Expression-Evaluator/SimpleExpressionEvaluator/SimpleExpressionEvaluator.csproj" RUN dotnet restore "include/Math-Expression-Evaluator/SimpleExpressionEvaluator/SimpleExpressionEvaluator.csproj"

View File

@ -3,12 +3,15 @@ using MapTo;
namespace BlueWest.Data namespace BlueWest.Data
{ {
[MapFrom(new []{typeof(ProductUpdate), typeof(ProductCreate)})] [MapFrom(new []{typeof(ProductUpdate), typeof(ProductCreate), typeof(ProductUnique)})]
public partial class Product public partial class Product
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Size { get; set; } public string Size { get; set; }
public double Price { get; set; }
public Currency Currency { get; set; }
public Industry Industry { get; set; } public Industry Industry { get; set; }
public List<Company> Seller { get; set; } public List<Company> Seller { get; set; }
} }

View File

@ -0,0 +1,15 @@
using MapTo;
namespace BlueWest.Data
{
[MapFrom(typeof(Product))]
public partial class ProductUnique
{
public int Id { get; set; }
public string Name { get; set; }
public string Size { get; set; }
public Industry Industry { get; set; }
}
}