diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index 09cd8f9..4292bef 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -11,6 +11,11 @@ preview + + + + + @@ -21,6 +26,9 @@ + + + diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index d001a5c..99fb663 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -9,8 +9,10 @@ namespace BlueWest.WebApi.EF /// /// Context for accessing company data /// + [EfGenerator] public sealed class CompanyDbContext : DbContext { + #region Companies /// /// Companies. /// @@ -21,13 +23,11 @@ namespace BlueWest.WebApi.EF keyPropertyMemberName: nameof(Company.Id)) ] - /*[EFUpdateMethods(typeof(int), - "Id", - typeof(CompanyUpdate), - typeof(CompanyUnique))]*/ - public DbSet Companies { get; set; } - + + #endregion + + #region Industries /// /// Industries. /// @@ -41,20 +41,8 @@ namespace BlueWest.WebApi.EF public DbSet Industries { get; set; } - void TestExtensions() - { - // var (result, obj) = this.AddCountry(new CountryCreate()); - } - /// - /// Products. - /// - /* - [EfAddMethods(typeof(ProductCreate))] - [EFUpdateMethods(typeof(int), - "Id", - typeof(ProductUpdate))] - */ - + #endregion + public DbSet Products { get; set; } diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index 232018d..bc020b2 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -14,39 +14,38 @@ namespace BlueWest.WebApi.EF public sealed class CountryDbContext : DbContext { + #region Countries /// /// Countries Database Table /// - - [EfGetOneBy( - nameof(Country.Id), - typeof(CountryUnique)) - ] - - [EfAddEntityToList( - listEntityType: typeof(Currency), - listEntityCreateType: typeof(CurrencyCreate), - listEntityReturnType: typeof(CurrencyUnique), - keyMembernameof: nameof(Country.Id)) - ] - + + [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))] + public DbSet Countries { get; set; } - + + #endregion + + #region Currencies + /// /// Currencies Database Table /// - // Generate Add extension methods + [EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))] [EfUpdateMethods( @@ -54,17 +53,25 @@ namespace BlueWest.WebApi.EF returnType: typeof(CurrencyUnique), keyPropertyMemberName: nameof(Currency.Id)) ] - public DbSet Currencies { get; set; } - - //////////////////////////////////////////////// - //////////////////////////////////////////////// + [EfGetOneBy( + nameof(Currency.Id), + typeof(CurrencyUnique))] + + [EfGetOne( + typeof(CurrencyUnique))] + + public DbSet Currencies { get; set; } + + #endregion + + #region Initialization /// /// App Configuration /// public IConfiguration Configuration; - + /// /// CountryDbContext Constructor. /// @@ -83,6 +90,7 @@ namespace BlueWest.WebApi.EF base.OnModelCreating(modelBuilder); modelBuilder.ConfigureCurrentDbModel(); } + #endregion - } + } } diff --git a/BlueWest.Api/Context/Templates/AddToListInEntity.csx b/BlueWest.Api/Context/Templates/AddToListTemplate.csx similarity index 100% rename from BlueWest.Api/Context/Templates/AddToListInEntity.csx rename to BlueWest.Api/Context/Templates/AddToListTemplate.csx diff --git a/BlueWest.Api/Context/Templates/GetManyTemplate.csx b/BlueWest.Api/Context/Templates/GetManyTemplate.csx new file mode 100644 index 0000000..d9951db --- /dev/null +++ b/BlueWest.Api/Context/Templates/GetManyTemplate.csx @@ -0,0 +1,28 @@ +/// +/// Gets {propertyName}. +/// +/// The database context. +/// How many {propertyName} to skip. +/// How many {propertyName} to take. (Default = 50) +/// Optional Order direction +/// Optional where predicate. +/// Optional order by predicate. +/// A bool if the result is successfull, and the first occurrence of {entityTypeName}> +public static (bool, {returnTypeFullName}[]) Get{propertyName}(this {contextFullName} dbContext, int skip = 0, int take = 50, int orderDir = 1, + Expression > where = null, + Expression > 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()); +} \ No newline at end of file diff --git a/BlueWest.Api/Context/Templates/GetOneByTemplate.csx b/BlueWest.Api/Context/Templates/GetOneByTemplate.csx index d886875..01f74ed 100644 --- a/BlueWest.Api/Context/Templates/GetOneByTemplate.csx +++ b/BlueWest.Api/Context/Templates/GetOneByTemplate.csx @@ -1,9 +1,9 @@ /// -/// Gets the first result in the table, and returns a projection of . +/// Gets the first result in the table, and returns a projection of /// /// The database context. -/// By {byParamPropertyName} -/// A bool if the result is successfull, and the first occurrence of {entityTypeName} as a projection +/// By {byParamPropertyName} parameter type. +/// A bool if the result is successfull, and the first occurrence of {entityTypeName}> public static (bool, {returnTypeFullName}) GetOne{entityTypeName}By{byParamPropertyName} (this {contextFullName} dbContext, {byParamFullType} {byParamVarName}) { diff --git a/BlueWest.Api/Context/Templates/GetOneTemplate.csx b/BlueWest.Api/Context/Templates/GetOneTemplate.csx new file mode 100644 index 0000000..cba3955 --- /dev/null +++ b/BlueWest.Api/Context/Templates/GetOneTemplate.csx @@ -0,0 +1,17 @@ +/// +/// Gets the first result following the query with a expression. +/// +/// The database context. +/// FirstOrDefault argument to pass. +/// A bool if the result is successfull, and the first occurrence of {returnTypeName} + +public static (bool, {returnTypeFullName}) Get{entityTypeName}With (this {contextFullName} dbContext, Expression> with) +{ + var {findEntityVarName} = + dbContext.{propertyName} + .Select(x => new {returnTypeFullName}(x)) + .FirstOrDefault(with); + + return ({findEntityVarName} != null, {findEntityVarName}); + +} \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index 2df9cc0..adc668b 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -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); } diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index a7a5530..5f850d7 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -15,6 +15,8 @@ namespace BlueWest.WebApi.Controllers [Route("[controller]")] public partial class CurrencyController : ControllerBase { + + #region Initialization private readonly CountryDbContext _dbContext; /// @@ -26,6 +28,7 @@ namespace BlueWest.WebApi.Controllers _dbContext = dbContext; } + #endregion /// /// Gets the currency data from currency table /// @@ -67,6 +70,29 @@ namespace BlueWest.WebApi.Controllers return new NotFoundResult(); } + + /// + /// Gets a currency by the currency number (id) + /// + /// The id of the currency to get + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("{currencyCode}", Name = nameof(GetCurrencyWithCode))] + public ActionResult GetCurrencyWithCode(Expression> with) + { + var currency = + _dbContext.Currencies + .Select(currency => new CurrencyUnique(currency)) + .FirstOrDefault(with); + + if (currency != null) + { + return Ok(currency); + } + + return new NotFoundResult(); + } /// /// Add Currency to the table of currencies