diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index d911de2..9fa919f 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -11,11 +11,6 @@ preview - - - - - @@ -41,6 +36,7 @@ + diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index 19278e9..68e3693 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -17,12 +17,7 @@ namespace BlueWest.WebApi.EF /// Companies. /// [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 Companies { get; set; } diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index fc2c7ac..fbfe200 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -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 Countries { get; set; } #endregion @@ -40,11 +44,15 @@ namespace BlueWest.WebApi.EF /// [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 Currencies { get; set; } #endregion diff --git a/BlueWest.Api/Context/Templates/GetManyTemplate.csx b/BlueWest.Api/Context/Templates/GetManyTemplate.csx index 2832db8..dc71750 100644 --- a/BlueWest.Api/Context/Templates/GetManyTemplate.csx +++ b/BlueWest.Api/Context/Templates/GetManyTemplate.csx @@ -7,7 +7,7 @@ /// Optional Order direction /// Optional where predicate. /// Optional order by predicate. -/// A bool if the result is successful and has at least 1 occurrence of {propertyName}. +/// A bool if the result is successful and a projection of the first occurrence of {propertyName}. public static (bool, {returnTypeFullName}[]) Get{propertyName}(this {contextFullName} dbContext, int skip = 0, int take = 50, int orderDir = 1, Expression > where = null, Expression > orderBy = null) diff --git a/BlueWest.Api/Context/Templates/GetOneByTemplate.csx b/BlueWest.Api/Context/Templates/GetOneByTemplate.csx index af1473e..9c7837e 100644 --- a/BlueWest.Api/Context/Templates/GetOneByTemplate.csx +++ b/BlueWest.Api/Context/Templates/GetOneByTemplate.csx @@ -3,7 +3,7 @@ /// /// The database context. /// By {byParamPropertyName} parameter type. -/// A bool if the result is successfull, and the first occurrence of {entityTypeName}> +/// A projection of {entityTypeName}> public static (bool, {returnTypeFullName}) GetOne{entityTypeName}By{byParamPropertyName} (this {contextFullName} dbContext, {byParamFullType} {byParamVarName}) { var {findEntityVarName} = dbContext.{propertyName} diff --git a/BlueWest.Api/Context/Templates/GetOneFromListTemplate.csx b/BlueWest.Api/Context/Templates/GetOneFromListTemplate.csx new file mode 100644 index 0000000..e3044e2 --- /dev/null +++ b/BlueWest.Api/Context/Templates/GetOneFromListTemplate.csx @@ -0,0 +1,20 @@ +/// +/// Gets a projection of the first {listItemTypeName} occurrence. +/// +/// The Database context. +/// {entityTypeName} lookup key. +/// {listItemTypeName} lookup key. +/// A projection of {listItemTypeName} +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()); +} diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index 8e84f39..620ac5c 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -31,7 +31,6 @@ namespace BlueWest.WebApi.Controllers #endregion - #region GetCountries /// @@ -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 /// @@ -80,42 +78,34 @@ namespace BlueWest.WebApi.Controllers #endregion - + #region GetCountryCurrencies /// /// Get currencies of a countryId /// /// + /// How many records to skip + /// How many records to take + /// The order direction + /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [HttpGet("{countryId}/currencies")] - public ActionResult GetCountryCurrencies(int countryId, int skip = 0, int take = 50, int orderDir = 1, Expression> 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 /// @@ -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>[] - { - 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 - - } } \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index bc70823..f811c5d 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -29,6 +29,8 @@ namespace BlueWest.WebApi.Controllers } #endregion + + #region GetCurrencies /// /// Gets the currency data from currency table /// @@ -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 /// /// Gets a currency by the currency number (id) /// @@ -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 + /// /// Gets a currency by the currency number (id) /// @@ -92,6 +97,9 @@ namespace BlueWest.WebApi.Controllers return new NotFoundResult(); } + #endregion + + #region AddCurrency /// /// Add Currency to the table of currencies /// @@ -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 /// /// Update a currency data from the Currency table in the database /// @@ -133,36 +144,38 @@ namespace BlueWest.WebApi.Controllers return new NotFoundResult(); } + #endregion + + #region GetCountryFromCurrency /// /// Gets a specific country id in a country /// /// The id of the currency /// The id of the country /// + /* + * [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 /// /// Add Currency to the table of currencies /// @@ -171,24 +184,18 @@ namespace BlueWest.WebApi.Controllers /// [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>[] - { - 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 + } } \ No newline at end of file diff --git a/include/BlueWest.EfMethods b/include/BlueWest.EfMethods index dc23c79..15a0f1a 160000 --- a/include/BlueWest.EfMethods +++ b/include/BlueWest.EfMethods @@ -1 +1 @@ -Subproject commit dc23c797975f1e4d835b5e67a5c0972813567ce1 +Subproject commit 15a0f1a4f43e1ca585dc87e090dab0abd451857d