From 62ad4f150d1958b3b210dab6bb3068becfa7622d Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Sun, 21 Aug 2022 23:32:47 +0100 Subject: [PATCH] Remove generic extensions and refactor code --- .../Context/Extensions/CountryDbExtensions.cs | 81 +++++++++++-------- .../Context/Extensions/CurrencyExtensions.cs | 20 +++-- .../Context/Extensions/GenericExtensions.cs | 51 ------------ BlueWest.Api/Controllers/CountryController.cs | 47 +++++++++-- .../Controllers/CurrencyController.cs | 2 +- include/BlueWest.MapTo | 2 +- 6 files changed, 102 insertions(+), 101 deletions(-) delete mode 100644 BlueWest.Api/Context/Extensions/GenericExtensions.cs diff --git a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs index 725a79b..a5699a7 100644 --- a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs @@ -1,4 +1,6 @@ +using System; using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; using BlueWest.Data; using Microsoft.EntityFrameworkCore; @@ -8,53 +10,68 @@ namespace BlueWest.WebApi.EF internal static class CountryDbExtensions { - internal static Country GetCountryById(this DbSet countries, int countryId) => countries.GetOne(x => x.Id == countryId); - internal static (bool, T) NotFound() where T : class => (false, null); + internal static (bool, Country) NotFound() => (false, null); + internal static (bool, string, Country) ErrorMessage(string message) => (false, message, null); + internal static (bool, string, Country) Success(bool success, Country country) => (success, "1", null); - internal static (bool, Country) AddCountry (this CountryDbContext dbContext, CountryCreate countryCreate) - { - Country newCountry = new Country(countryCreate); - return dbContext.Countries.Add(dbContext, newCountry); - } - - internal static async Task<(bool, Country)> AddCountryAsync (this CountryDbContext dbContext, CountryCreate countryCreate) - { - var newCountry = new Country(countryCreate); - return await dbContext.Countries.AddAsync(dbContext, newCountry); - } - - internal static (bool, Country) UpdateCountry(this CountryDbContext dbContext, CountryUpdate countryUpdate, + internal static (bool, Country) UpdateCountry( + this CountryDbContext dbContext, + CountryUpdate countryUpdate, int countryId) { var country = dbContext.Countries.FirstOrDefault(x => x.Id == countryId); + if (country == null) return NotFound(); + country.Update(countryUpdate); + dbContext.Countries.Update(country); + var result = dbContext.SaveChanges() >= 0; + return (result, country); - if (country != null) - { - var updatedCountry = new Country(countryUpdate); - return dbContext.Countries.Update(dbContext, updatedCountry); - } - - return NotFound(); } internal static async Task GetCountryByIdAsync(this DbSet countries, int countryId) => await countries.FirstOrDefaultAsync(x => x.Id == countryId); - - internal static async Task<(bool, Country)> UpdateCountryAsync(this CountryDbContext dbContext, CountryUpdate countryUpdate, - int countryCode) - { - var country = await dbContext.Countries.FirstOrDefaultAsync(x => x.Code == countryCode); - if (country != null) + internal static (bool, string, Country) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate) + { + var country = dbContext.Countries.FirstOrDefault(d => d.Code == countryId); + + // Check if currency exists + if (country == null) return (false, "Country Not found.", null); + + // Creates new currency + var newCurrency = new Currency(currencyCreate); + country.Currencies.Add(newCurrency); + var success = dbContext.SaveChanges() >= 0; + return Success(success, country); + } + + internal static (bool, string, Country) AddCurrency( + this CountryDbContext dbContext, + int countryId, CurrencyCreate currencyCreate, + Expression>[] duplicationValidations) + { + var country = dbContext.Countries.FirstOrDefault(d => d.Code == countryId); + + // Check if currency exists + if (country == null) return (false, $"{nameof(country)} Not found.", null); + + // Check if there's currency with the same code + + foreach (var duplicationValidation in duplicationValidations) { - var updatedCountry = new Country(countryUpdate); - return await dbContext.Countries.UpdateAsync(dbContext, updatedCountry); + var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation); + if (currencyToGet != null) return ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}"); + } - return NotFound(); + // Creates new currency + var newCurrency = new Currency(currencyCreate); + country.Currencies.Add(newCurrency); + var success = dbContext.SaveChanges() >= 0; + return Success(success, country); } - + } } diff --git a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs index d2c7b8e..26213f7 100644 --- a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs @@ -8,27 +8,25 @@ namespace BlueWest.WebApi.EF internal static class CurrencyExtensions { - internal static Currency GetCurrencyById(this DbSet currencies, int currencyId) => currencies.GetOne(x => x.Id == currencyId); - internal static (bool, T) NotFound() where T : class => (false, null); - + internal static (bool, Currency) NotFound() => (false, null); internal static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate) { - var newCurrency = new Currency(currencyToCreate); - return dbContext.Currencies.Add(dbContext, newCurrency); + dbContext.Add(newCurrency); + var resultOperation = dbContext.SaveChanges() >= 0; + return (resultOperation, newCurrency); } internal 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 newCurrency = new Currency(currencyToUpdate); - var operationResult = dbContext.Currencies.Update(dbContext, newCurrency); - return operationResult; + if (currency == null) return NotFound(); + currency.Update(currencyToUpdate); + dbContext.Update(currency); + var resultOperation = dbContext.SaveChanges() >= 0; + return (resultOperation, currency); } } diff --git a/BlueWest.Api/Context/Extensions/GenericExtensions.cs b/BlueWest.Api/Context/Extensions/GenericExtensions.cs deleted file mode 100644 index 184400f..0000000 --- a/BlueWest.Api/Context/Extensions/GenericExtensions.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Linq.Expressions; -using System.Threading.Tasks; -using BlueWest.Data; -using Microsoft.EntityFrameworkCore; - -namespace BlueWest.WebApi.EF; - -public static class GenericExtensions -{ - - - internal static T GetOne(this DbSet dbSet, Expression> predicate) where T: class => dbSet.FirstOrDefault(predicate); - - internal static async Task GetOneAsync(this DbSet dbSet, Expression> predicate) where T: class => await dbSet.FirstOrDefaultAsync(predicate); - - - internal static (bool, T) NotFound() where T : class => (false, null); - - internal static (bool, T) Add(this DbSet dbSet, DbContext dbContext, T objectToAdd) where T: class - { - var newEntity = dbSet.Add(objectToAdd).Entity; - return (dbContext.SaveChanges() >= 0, newEntity); - } - - internal static async Task<(bool, T)> AddAsync(this DbSet dbSet, DbContext dbContext, T objectToAdd) where T: class - { - var newEntity = dbSet.Add(objectToAdd); - bool resultOperation = await dbContext.SaveChangesAsync() >= 0; - return (resultOperation, objectToAdd); - } - - - internal static (bool, T) Update(this DbSet dbSet, DbContext dbContext, T objectToUpdate) where T: class - { - - dbContext.Update(objectToUpdate); - var resultOperation = dbContext.SaveChanges() >= 0; - return resultOperation ? (true, objectToUpdate) : (false, null); - } - - internal static async Task<(bool, T)> UpdateAsync(this DbSet dbSet, DbContext dbContext, T objectToUpdate) where T: class - { - dbSet.Update(objectToUpdate); - var result = await dbContext.SaveChangesAsync() >= 0; - return (result, objectToUpdate); - } - -} \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index 667d040..4258ba8 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -1,5 +1,7 @@ +using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using BlueWest.Data; using BlueWest.WebApi.EF; using Microsoft.AspNetCore.Http; @@ -47,11 +49,17 @@ namespace BlueWest.WebApi.Controllers /// /// Returns the newly created country [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status406NotAcceptable)] + [HttpPost] public ActionResult AddCountry(CountryCreate countryToCreate) { - _dbContext.AddCountry(countryToCreate); - return CreatedAtRoute(nameof(GetCountryById), new {countryId = countryToCreate.Code}); + + Country newCountry = new Country(countryToCreate); + _dbContext.Countries.Add(newCountry); + bool success = _dbContext.SaveChanges() >= 0; + if (!success) return new BadRequestResult(); + return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Id}); } /// @@ -105,7 +113,7 @@ namespace BlueWest.WebApi.Controllers [HttpGet("{countryId}", Name = nameof(GetCountryById))] public ActionResult GetCountryById(int countryId) { - var array = _dbContext.Countries.FirstOrDefault(x => x.Code == countryId); + var array = _dbContext.Countries.FirstOrDefault(x => x.Id == countryId); if (array != null) { @@ -125,17 +133,46 @@ namespace BlueWest.WebApi.Controllers [HttpGet("{countryId}/currencies")] public ActionResult GetCountryCurrencies(int countryId) { - var country = _dbContext.Countries.FirstOrDefault(d => d.Code == countryId); + var country = _dbContext.Countries.FirstOrDefault(d => d.Id == countryId); if (country == null) return new NotFoundResult(); var array = _dbContext .Countries - .Where(data => data.Code == countryId) + .Where(data => data.Id == countryId) .SelectMany(o => o.Currencies) .ToArray(); return Ok(array); } + + + /// + /// Adds a currency to Country + /// + /// + /// + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status409Conflict)] + [HttpPost("{countryId}/currencies")] + public ActionResult AddCurrency(int countryId, CurrencyCreate currencyCreate) + { + var (result, message, country) = _dbContext.AddCurrency(countryId, currencyCreate, + new Expression>[] + { + x => x.Code == currencyCreate.Code, + y => y.Num == currencyCreate.Num + }); + + if (!result) + { + return new ConflictObjectResult(message); + } + + return Ok(country); + } + + } } \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index 598fe86..e713ac0 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -88,7 +88,7 @@ namespace BlueWest.WebApi.Controllers [HttpGet("{currencyId}", Name = nameof(GetCurrencyById))] public ActionResult GetCurrencyById(int currencyId) { - var currency = _dbContext.Currencies.GetCurrencyById(currencyId); + var currency = _dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId); if (currency != null) { diff --git a/include/BlueWest.MapTo b/include/BlueWest.MapTo index aa5b01c..a6f5116 160000 --- a/include/BlueWest.MapTo +++ b/include/BlueWest.MapTo @@ -1 +1 @@ -Subproject commit aa5b01cdc42a00e873f126049a9c111b1d5220fa +Subproject commit a6f511665605041c327c6578d2b9789a3c9351bd