diff --git a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs
index 4f388a2..0332a65 100644
--- a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs
+++ b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs
@@ -12,30 +12,6 @@ namespace BlueWest.WebApi.EF
///
public static class CountryDbExtensions
{
- static CountryDbExtensions()
- {
- }
-
- ///
- /// Returns Not found projection
- ///
- ///
- public static (bool, Country) NotFound() => (false, null);
-
- ///
- /// Returns Error Message projection
- ///
- ///
- /// (false, message, null)
- public static (bool, string, Country) ErrorMessage(string message) => (false, message, null);
-
- ///
- /// Returns Success intent projection
- ///
- /// Success message
- /// Entity object
- /// (bool success, Country country)
- public static (bool, string, Country) Success(bool success, Country country) => (success, "1", null);
///
/// Updates a country data.
@@ -50,7 +26,7 @@ namespace BlueWest.WebApi.EF
int countryId)
{
var country = dbContext.Countries.FirstOrDefault(x => x.Id == countryId);
- if (country == null) return NotFound();
+ if (country == null) return Projection.NotFound;
country.Update(countryUpdate);
dbContext.Countries.Update(country);
var result = dbContext.SaveChanges() >= 0;
@@ -76,7 +52,7 @@ namespace BlueWest.WebApi.EF
var newCurrency = new Currency(currencyCreate);
country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0;
- return Success(success, country);
+ return Projection.Success(success, country);
}
///
@@ -108,7 +84,7 @@ namespace BlueWest.WebApi.EF
foreach (var duplicationValidation in duplicationValidations)
{
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
- if (currencyToGet != null) return ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
+ if (currencyToGet != null) return Projection.ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
}
@@ -116,8 +92,9 @@ namespace BlueWest.WebApi.EF
var newCurrency = new Currency(currencyCreate);
country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0;
- return Success(success, country);
+ return Projection.Success(success, country);
}
+
}
diff --git a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs
index f47b425..0dac00a 100644
--- a/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs
+++ b/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs
@@ -1,5 +1,7 @@
+using System;
using System.Collections.Generic;
using System.Linq;
+using System.Linq.Expressions;
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
@@ -49,6 +51,37 @@ namespace BlueWest.WebApi.EF
return (resultOperation, 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();
+
+
+ // 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 currencyToGet = dbContext.Countries.FirstOrDefault(duplicationValidation);
+ if (currencyToGet != null) return Projection.ErrorMessage($"Duplication Validation failed: {duplicationValidation.Body}");
+ }
+
+ // Creates new currency
+ var newCountry = new Country(countryCreate);
+ currency.Countries.Add(newCountry);
+ var success = dbContext.SaveChanges() >= 0;
+ return Projection.Success(success, new CountryUnique(newCountry));
+ }
+
}
}
diff --git a/BlueWest.Api/Context/Extensions/Projection.cs b/BlueWest.Api/Context/Extensions/Projection.cs
new file mode 100644
index 0000000..64ec0c8
--- /dev/null
+++ b/BlueWest.Api/Context/Extensions/Projection.cs
@@ -0,0 +1,26 @@
+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/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs
index 4e2659a..ac5acd9 100644
--- a/BlueWest.Api/Controllers/CurrencyController.cs
+++ b/BlueWest.Api/Controllers/CurrencyController.cs
@@ -1,6 +1,8 @@
+using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
+using System.Linq.Expressions;
using BlueWest.Data;
using BlueWest.WebApi.EF;
using Microsoft.AspNetCore.Http;
@@ -41,6 +43,31 @@ namespace BlueWest.WebApi.Controllers
return Ok(currencies);
}
+
+
+ ///
+ /// Gets a currency by the currency number (id)
+ ///
+ /// The id of the currency to get
+ ///
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ [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();
+
+ if (currency != null)
+ {
+ return Ok(currency);
+ }
+
+ return new NotFoundResult();
+ }
///
/// Add Currency to the table of currencies
@@ -82,26 +109,35 @@ namespace BlueWest.WebApi.Controllers
return new NotFoundResult();
}
-
-
+
///
- /// Gets a currency by the currency number (id)
+ /// Add Currency to the table of currencies
///
- /// The id of the currency to get
+ /// Currency data to create
///
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- [HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
- public ActionResult GetCurrencyById(int currencyId)
+ [ProducesResponseType(StatusCodes.Status201Created)]
+ [HttpPost]
+ public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate)
{
- var currency = _dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
+
+ var (result, message, country) = _dbContext.AddCountry(currencyId, countryToCreate,
+ new Expression>[]
+ {
+ x => x.Code == countryToCreate.Code,
+ y => y.StateName == countryToCreate.StateName,
+ z => z.Alpha2Code == countryToCreate.Alpha2Code
+
+ });
- if (currency != null)
+ if (!result)
{
- return Ok(new CurrencyUnique(currency));
+ return new ConflictObjectResult(message);
}
-
- return new NotFoundResult();
+
+ return Ok(country);
}
+
+
+
}
}
\ No newline at end of file