Code refactor

This commit is contained in:
CodeLiturgy 2022-08-22 16:02:48 +01:00
parent 7e068abb49
commit f7c15f371d
7 changed files with 100 additions and 83 deletions

View File

@ -24,8 +24,4 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Repositories" />
</ItemGroup>
</Project>

View File

@ -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);
}
/// <summary>
@ -63,7 +64,7 @@ namespace BlueWest.WebApi.EF
/// <param name="currencyCreate">Data to create currency</param>
/// <param name="duplicationValidations">List of expressions</param>
/// <returns></returns>
public static (bool, string, Country) AddCurrency(
public static (bool, string, CountryUnique) AddCurrency(
this CountryDbContext dbContext,
int countryId, CurrencyCreate currencyCreate,
Expression<Func<Currency,bool>>[] 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));
}
}
}

View File

@ -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
{
/// <summary>
/// Not found projection
/// </summary>
/// <returns></returns>
public static (bool, Currency) NotFound() => (false, null);
/// <summary>
/// Add new Currency
/// </summary>
@ -43,8 +38,8 @@ namespace BlueWest.WebApi.EF
/// <returns></returns>
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
}
/// <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(
this CountryDbContext dbContext,
int currencyId, CountryCreate countryCreate,
Expression<Func<Country,bool>>[] 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<CountryUnique>($"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));
}
}
}

View File

@ -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);
}
}

View File

@ -177,7 +177,7 @@ namespace BlueWest.WebApi.Controllers
return new ConflictObjectResult(message);
}
return Ok(new CountryUnique(country));
return Ok(country);
}

View File

@ -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
/// </summary>
[ApiController]
[Route("[controller]")]
public class CurrencyController : ControllerBase
public partial class CurrencyController : ControllerBase
{
private readonly CountryDbContext _dbContext;
/// <summary>
/// Api Controller for Currency data
/// </summary>
@ -40,11 +38,11 @@ namespace BlueWest.WebApi.Controllers
var currencies = _dbContext.Currencies
.Select(currency => new CurrencyUnique(currency))
.ToArray();
return Ok(currencies);
}
/// <summary>
/// Gets a currency by the currency number (id)
/// </summary>
@ -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));
}
/// <summary>
@ -110,34 +109,60 @@ namespace BlueWest.WebApi.Controllers
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>
/// Add Currency to the table of currencies
/// </summary>
/// <param name="currencyToCreate">Currency data to create</param>
/// <param name="currencyId"></param>
/// <param name="countryToCreate"></param>
/// <returns></returns>
[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<Func<Country, bool>>[]
{
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);
}
}
}

View File

@ -0,0 +1,14 @@
using System;
namespace BlueWest.WebApi
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class RepoGeneratorAttribute : Attribute
{
public RepoGeneratorAttribute()
{
}
}
}