From f7c15f371d204ed5534f80235fbeab81c3c991c8 Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Mon, 22 Aug 2022 16:02:48 +0100 Subject: [PATCH] Code refactor --- BlueWest.Api/BlueWest.Api.csproj | 4 - .../Context/Extensions/CountryDbExtensions.cs | 18 ++--- .../Context/Extensions/CurrencyExtensions.cs | 42 ++++++---- BlueWest.Api/Context/Extensions/Projection.cs | 26 ------- BlueWest.Api/Controllers/CountryController.cs | 2 +- .../Controllers/CurrencyController.cs | 77 ++++++++++++------- BlueWest.Api/RepoGeneratorAttribute.cs | 14 ++++ 7 files changed, 100 insertions(+), 83 deletions(-) delete mode 100644 BlueWest.Api/Context/Extensions/Projection.cs create mode 100644 BlueWest.Api/RepoGeneratorAttribute.cs diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index f85aeec..386a0eb 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -24,8 +24,4 @@ - - - - diff --git a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs index 0332a65..a90b3b4 100644 --- a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs @@ -26,7 +26,7 @@ namespace BlueWest.WebApi.EF int countryId) { var country = dbContext.Countries.FirstOrDefault(x => x.Id == countryId); - if (country == null) return Projection.NotFound; + if (country == null) return (false, null); country.Update(countryUpdate); dbContext.Countries.Update(country); var result = dbContext.SaveChanges() >= 0; @@ -52,7 +52,8 @@ namespace BlueWest.WebApi.EF var newCurrency = new Currency(currencyCreate); country.Currencies.Add(newCurrency); var success = dbContext.SaveChanges() >= 0; - return Projection.Success(success, country); + + return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, country); } /// @@ -63,7 +64,7 @@ namespace BlueWest.WebApi.EF /// Data to create currency /// List of expressions /// - public static (bool, string, Country) AddCurrency( + public static (bool, string, CountryUnique) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate, Expression>[] duplicationValidations) @@ -84,19 +85,18 @@ namespace BlueWest.WebApi.EF foreach (var duplicationValidation in duplicationValidations) { var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation); - if (currencyToGet != null) return Projection.ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}"); - + if (currencyToGet != null) + { + return (false, $"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}", null); + } } // Creates new currency var newCurrency = new Currency(currencyCreate); country.Currencies.Add(newCurrency); var success = dbContext.SaveChanges() >= 0; - return Projection.Success(success, country); + return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CountryUnique(country)); } - - - } } diff --git a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs index 0dac00a..f7406e4 100644 --- a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading.Tasks; using BlueWest.Data; using Microsoft.EntityFrameworkCore; @@ -13,12 +14,6 @@ namespace BlueWest.WebApi.EF public static class CurrencyExtensions { - /// - /// Not found projection - /// - /// - public static (bool, Currency) NotFound() => (false, null); - /// /// Add new Currency /// @@ -43,8 +38,8 @@ namespace BlueWest.WebApi.EF /// public static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate) { - var currency = dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId); - if (currency == null) return NotFound(); + 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; @@ -52,18 +47,27 @@ namespace BlueWest.WebApi.EF } + /// + /// Add Country associated with specified Currency. + /// + /// + /// + /// + /// + /// public static (bool, string, CountryUnique) AddCountry( this CountryDbContext dbContext, int currencyId, CountryCreate countryCreate, Expression>[] duplicationValidations) { - var currency = dbContext - .Currencies - .Where(data => data.Id == currencyId) - .Include(o => o.Countries) - .FirstOrDefault(); + 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); @@ -71,17 +75,21 @@ namespace BlueWest.WebApi.EF // Check if there's currency with the same code foreach (var duplicationValidation in duplicationValidations) { - var currencyToGet = dbContext.Countries.FirstOrDefault(duplicationValidation); - if (currencyToGet != null) return Projection.ErrorMessage($"Duplication Validation failed: {duplicationValidation.Body}"); + 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 Projection.Success(success, new CountryUnique(newCountry)); + return (success, string.Empty, new CountryUnique(newCountry)); } - + } } diff --git a/BlueWest.Api/Context/Extensions/Projection.cs b/BlueWest.Api/Context/Extensions/Projection.cs deleted file mode 100644 index 64ec0c8..0000000 --- a/BlueWest.Api/Context/Extensions/Projection.cs +++ /dev/null @@ -1,26 +0,0 @@ -using BlueWest.Data; - -namespace BlueWest.WebApi.EF -{ - public static class Projection - { - public static readonly (bool, Country) NotFound = (false, null); - - /// - /// Returns Error Message projection - /// - /// - /// (false, message, null) - public static (bool, string, T) ErrorMessage(string message) where T : class => (false, message, null); - - - /// - /// Returns Success intent projection - /// - /// Success message - /// Entity Object - /// (bool success, Country country) - public static (bool, string, T) Success(bool success, T dataObject) where T : class => (success, "1", dataObject); - } -} - diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index d125de5..794dc74 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -177,7 +177,7 @@ namespace BlueWest.WebApi.Controllers return new ConflictObjectResult(message); } - return Ok(new CountryUnique(country)); + return Ok(country); } diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index ac5acd9..c739418 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Collections.Immutable; using System.Linq; using System.Linq.Expressions; using BlueWest.Data; @@ -15,10 +13,10 @@ namespace BlueWest.WebApi.Controllers /// [ApiController] [Route("[controller]")] - public class CurrencyController : ControllerBase + public partial class CurrencyController : ControllerBase { private readonly CountryDbContext _dbContext; - + /// /// Api Controller for Currency data /// @@ -40,11 +38,11 @@ namespace BlueWest.WebApi.Controllers var currencies = _dbContext.Currencies .Select(currency => new CurrencyUnique(currency)) .ToArray(); - + return Ok(currencies); } - - + + /// /// Gets a currency by the currency number (id) /// @@ -55,11 +53,12 @@ namespace BlueWest.WebApi.Controllers [HttpGet("{currencyId}", Name = nameof(GetCurrencyById))] public ActionResult GetCurrencyById(int currencyId) { - var currency = _dbContext - .Currencies - .Where(x => x.Id == currencyId) - .Select(x => new CurrencyUnique(x)) - .FirstOrDefault(); + var queryResult = + from aCurrency in _dbContext.Currencies + where aCurrency.Id == currencyId + select new CurrencyUnique(aCurrency); + + var currency = queryResult.SingleOrDefault(); if (currency != null) { @@ -78,15 +77,15 @@ namespace BlueWest.WebApi.Controllers [HttpPost] public ActionResult AddCurrency(CurrencyCreate currencyToCreate) { - - var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate); + var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate); if (!success) { return new NotFoundResult(); } - - return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, new CurrencyUnique(newCurrency)); + + return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, + new CurrencyUnique(newCurrency)); } /// @@ -110,34 +109,60 @@ 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("{currencyId}/countries/{countryId}", Name = nameof(GetCurrencyById))] + public ActionResult GetCountry(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 queryResult = countryQuery.SingleOrDefault(); + + if (queryResult == null) + { + return new NotFoundResult(); + } + + return Ok(queryResult); + + } + /// /// Add Currency to the table of currencies /// - /// Currency data to create + /// + /// /// [ProducesResponseType(StatusCodes.Status201Created)] - [HttpPost] + [HttpPost("{currencyId}/countries")] public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate) { - - var (result, message, country) = _dbContext.AddCountry(currencyId, 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 - }); - if (!result) + if (!success) { return new ConflictObjectResult(message); } - + return Ok(country); } - - - + } } \ No newline at end of file diff --git a/BlueWest.Api/RepoGeneratorAttribute.cs b/BlueWest.Api/RepoGeneratorAttribute.cs new file mode 100644 index 0000000..8e9def1 --- /dev/null +++ b/BlueWest.Api/RepoGeneratorAttribute.cs @@ -0,0 +1,14 @@ +using System; + +namespace BlueWest.WebApi +{ + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] + public class RepoGeneratorAttribute : Attribute + { + public RepoGeneratorAttribute() + { + + } + } +} +