Code refactor
This commit is contained in:
parent
7e068abb49
commit
f7c15f371d
|
@ -24,8 +24,4 @@
|
||||||
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Repositories" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace BlueWest.WebApi.EF
|
||||||
int countryId)
|
int countryId)
|
||||||
{
|
{
|
||||||
var country = dbContext.Countries.FirstOrDefault(x => x.Id == 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);
|
country.Update(countryUpdate);
|
||||||
dbContext.Countries.Update(country);
|
dbContext.Countries.Update(country);
|
||||||
var result = dbContext.SaveChanges() >= 0;
|
var result = dbContext.SaveChanges() >= 0;
|
||||||
|
@ -52,7 +52,8 @@ namespace BlueWest.WebApi.EF
|
||||||
var newCurrency = new Currency(currencyCreate);
|
var newCurrency = new Currency(currencyCreate);
|
||||||
country.Currencies.Add(newCurrency);
|
country.Currencies.Add(newCurrency);
|
||||||
var success = dbContext.SaveChanges() >= 0;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -63,7 +64,7 @@ namespace BlueWest.WebApi.EF
|
||||||
/// <param name="currencyCreate">Data to create currency</param>
|
/// <param name="currencyCreate">Data to create currency</param>
|
||||||
/// <param name="duplicationValidations">List of expressions</param>
|
/// <param name="duplicationValidations">List of expressions</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static (bool, string, Country) AddCurrency(
|
public static (bool, string, CountryUnique) AddCurrency(
|
||||||
this CountryDbContext dbContext,
|
this CountryDbContext dbContext,
|
||||||
int countryId, CurrencyCreate currencyCreate,
|
int countryId, CurrencyCreate currencyCreate,
|
||||||
Expression<Func<Currency,bool>>[] duplicationValidations)
|
Expression<Func<Currency,bool>>[] duplicationValidations)
|
||||||
|
@ -84,19 +85,18 @@ namespace BlueWest.WebApi.EF
|
||||||
foreach (var duplicationValidation in duplicationValidations)
|
foreach (var duplicationValidation in duplicationValidations)
|
||||||
{
|
{
|
||||||
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
|
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
|
// Creates new currency
|
||||||
var newCurrency = new Currency(currencyCreate);
|
var newCurrency = new Currency(currencyCreate);
|
||||||
country.Currencies.Add(newCurrency);
|
country.Currencies.Add(newCurrency);
|
||||||
var success = dbContext.SaveChanges() >= 0;
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using BlueWest.Data;
|
using BlueWest.Data;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
@ -13,12 +14,6 @@ namespace BlueWest.WebApi.EF
|
||||||
public static class CurrencyExtensions
|
public static class CurrencyExtensions
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Not found projection
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static (bool, Currency) NotFound() => (false, null);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add new Currency
|
/// Add new Currency
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -43,8 +38,8 @@ namespace BlueWest.WebApi.EF
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate)
|
public static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate)
|
||||||
{
|
{
|
||||||
var currency = dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
|
var currency = dbContext.Currencies.SingleOrDefault(x => x.Id == currencyId);
|
||||||
if (currency == null) return NotFound();
|
if (currency == null) return (false, null);
|
||||||
currency.Update(currencyToUpdate);
|
currency.Update(currencyToUpdate);
|
||||||
dbContext.Update(currency);
|
dbContext.Update(currency);
|
||||||
var resultOperation = dbContext.SaveChanges() >= 0;
|
var resultOperation = dbContext.SaveChanges() >= 0;
|
||||||
|
@ -52,18 +47,27 @@ namespace BlueWest.WebApi.EF
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add Country associated with specified Currency.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbContext"></param>
|
||||||
|
/// <param name="currencyId"></param>
|
||||||
|
/// <param name="countryCreate"></param>
|
||||||
|
/// <param name="duplicationValidations"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public static (bool, string, CountryUnique) AddCountry(
|
public static (bool, string, CountryUnique) AddCountry(
|
||||||
this CountryDbContext dbContext,
|
this CountryDbContext dbContext,
|
||||||
int currencyId, CountryCreate countryCreate,
|
int currencyId, CountryCreate countryCreate,
|
||||||
Expression<Func<Country,bool>>[] duplicationValidations)
|
Expression<Func<Country,bool>>[] duplicationValidations)
|
||||||
{
|
{
|
||||||
|
|
||||||
var currency = dbContext
|
var queryable = from aCurrency in dbContext.Currencies
|
||||||
.Currencies
|
where aCurrency.Id == currencyId
|
||||||
.Where(data => data.Id == currencyId)
|
select aCurrency;
|
||||||
.Include(o => o.Countries)
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
|
queryable.Include(x => x.Countries);
|
||||||
|
|
||||||
|
var currency = queryable.SingleOrDefault();
|
||||||
|
|
||||||
// Check if currency exists
|
// Check if currency exists
|
||||||
if (currency == null) return (false, $"{nameof(Currency)}: {currencyId} - Not found.", null);
|
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
|
// Check if there's currency with the same code
|
||||||
foreach (var duplicationValidation in duplicationValidations)
|
foreach (var duplicationValidation in duplicationValidations)
|
||||||
{
|
{
|
||||||
var currencyToGet = dbContext.Countries.FirstOrDefault(duplicationValidation);
|
var countryToCheck = dbContext.Countries.FirstOrDefault(duplicationValidation);
|
||||||
if (currencyToGet != null) return Projection.ErrorMessage<CountryUnique>($"Duplication Validation failed: {duplicationValidation.Body}");
|
|
||||||
|
if (countryToCheck != null)
|
||||||
|
{
|
||||||
|
return (false, $"Duplication Validation failed: {duplicationValidation.Body}", null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates new currency
|
// Creates new currency
|
||||||
var newCountry = new Country(countryCreate);
|
var newCountry = new Country(countryCreate);
|
||||||
currency.Countries.Add(newCountry);
|
currency.Countries.Add(newCountry);
|
||||||
var success = dbContext.SaveChanges() >= 0;
|
var success = dbContext.SaveChanges() >= 0;
|
||||||
return Projection.Success(success, new CountryUnique(newCountry));
|
return (success, string.Empty, new CountryUnique(newCountry));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
using BlueWest.Data;
|
|
||||||
|
|
||||||
namespace BlueWest.WebApi.EF
|
|
||||||
{
|
|
||||||
public static class Projection
|
|
||||||
{
|
|
||||||
public static readonly (bool, Country) NotFound = (false, null);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns Error Message projection
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="message"></param>
|
|
||||||
/// <returns>(false, message, null)</returns>
|
|
||||||
public static (bool, string, T) ErrorMessage<T>(string message) where T : class => (false, message, null);
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns Success intent projection
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="success">Success message</param>
|
|
||||||
/// <param name="dataObject">Entity Object</param>
|
|
||||||
/// <returns>(bool success, Country country)</returns>
|
|
||||||
public static (bool, string, T) Success<T>(bool success, T dataObject) where T : class => (success, "1", dataObject);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -177,7 +177,7 @@ namespace BlueWest.WebApi.Controllers
|
||||||
return new ConflictObjectResult(message);
|
return new ConflictObjectResult(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(new CountryUnique(country));
|
return Ok(country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using BlueWest.Data;
|
using BlueWest.Data;
|
||||||
|
@ -15,10 +13,10 @@ namespace BlueWest.WebApi.Controllers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class CurrencyController : ControllerBase
|
public partial class CurrencyController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly CountryDbContext _dbContext;
|
private readonly CountryDbContext _dbContext;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Api Controller for Currency data
|
/// Api Controller for Currency data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -40,11 +38,11 @@ namespace BlueWest.WebApi.Controllers
|
||||||
var currencies = _dbContext.Currencies
|
var currencies = _dbContext.Currencies
|
||||||
.Select(currency => new CurrencyUnique(currency))
|
.Select(currency => new CurrencyUnique(currency))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
return Ok(currencies);
|
return Ok(currencies);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a currency by the currency number (id)
|
/// Gets a currency by the currency number (id)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -55,11 +53,12 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
|
[HttpGet("{currencyId}", Name = nameof(GetCurrencyById))]
|
||||||
public ActionResult GetCurrencyById(int currencyId)
|
public ActionResult GetCurrencyById(int currencyId)
|
||||||
{
|
{
|
||||||
var currency = _dbContext
|
var queryResult =
|
||||||
.Currencies
|
from aCurrency in _dbContext.Currencies
|
||||||
.Where(x => x.Id == currencyId)
|
where aCurrency.Id == currencyId
|
||||||
.Select(x => new CurrencyUnique(x))
|
select new CurrencyUnique(aCurrency);
|
||||||
.FirstOrDefault();
|
|
||||||
|
var currency = queryResult.SingleOrDefault();
|
||||||
|
|
||||||
if (currency != null)
|
if (currency != null)
|
||||||
{
|
{
|
||||||
|
@ -78,15 +77,15 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
|
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
|
||||||
{
|
{
|
||||||
|
var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
|
||||||
var (success, newCurrency) = _dbContext.AddCurrency(currencyToCreate);
|
|
||||||
|
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
return new NotFoundResult();
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -110,34 +109,60 @@ namespace BlueWest.WebApi.Controllers
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a currency by the currency number (id)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="currencyId">The id of the currency to get</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add Currency to the table of currencies
|
/// Add Currency to the table of currencies
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="currencyToCreate">Currency data to create</param>
|
/// <param name="currencyId"></param>
|
||||||
|
/// <param name="countryToCreate"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
[HttpPost]
|
[HttpPost("{currencyId}/countries")]
|
||||||
public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate)
|
public ActionResult AddCountry(int currencyId, CountryCreate countryToCreate)
|
||||||
{
|
{
|
||||||
|
var (success, message, country) = _dbContext.AddCountry(currencyId, countryToCreate,
|
||||||
var (result, message, country) = _dbContext.AddCountry(currencyId, countryToCreate,
|
|
||||||
new Expression<Func<Country, bool>>[]
|
new Expression<Func<Country, bool>>[]
|
||||||
{
|
{
|
||||||
x => x.Code == countryToCreate.Code,
|
x => x.Code == countryToCreate.Code,
|
||||||
y => y.StateName == countryToCreate.StateName,
|
y => y.StateName == countryToCreate.StateName,
|
||||||
z => z.Alpha2Code == countryToCreate.Alpha2Code
|
z => z.Alpha2Code == countryToCreate.Alpha2Code
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result)
|
if (!success)
|
||||||
{
|
{
|
||||||
return new ConflictObjectResult(message);
|
return new ConflictObjectResult(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(country);
|
return Ok(country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace BlueWest.WebApi
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
|
public class RepoGeneratorAttribute : Attribute
|
||||||
|
{
|
||||||
|
public RepoGeneratorAttribute()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue