Remove generic extensions and refactor code
This commit is contained in:
parent
0d410211c8
commit
62ad4f150d
|
@ -1,4 +1,6 @@
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BlueWest.Data;
|
using BlueWest.Data;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
@ -8,53 +10,68 @@ namespace BlueWest.WebApi.EF
|
||||||
internal static class CountryDbExtensions
|
internal static class CountryDbExtensions
|
||||||
{
|
{
|
||||||
|
|
||||||
internal static Country GetCountryById(this DbSet<Country> countries, int countryId) => countries.GetOne(x => x.Id == countryId);
|
internal static (bool, Country) NotFound() => (false, null);
|
||||||
internal static (bool, T) NotFound<T>() where T : class => (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)
|
internal static (bool, Country) UpdateCountry(
|
||||||
{
|
this CountryDbContext dbContext,
|
||||||
Country newCountry = new Country(countryCreate);
|
CountryUpdate countryUpdate,
|
||||||
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,
|
|
||||||
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 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<Country>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task<Country> GetCountryByIdAsync(this DbSet<Country> countries, int countryId) =>
|
internal static async Task<Country> GetCountryByIdAsync(this DbSet<Country> countries, int countryId) =>
|
||||||
await countries.FirstOrDefaultAsync(x => x.Id == 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<Func<Currency,bool>>[] 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);
|
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
|
||||||
return await dbContext.Countries.UpdateAsync(dbContext, updatedCountry);
|
if (currencyToGet != null) return ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NotFound<Country>();
|
// Creates new currency
|
||||||
|
var newCurrency = new Currency(currencyCreate);
|
||||||
|
country.Currencies.Add(newCurrency);
|
||||||
|
var success = dbContext.SaveChanges() >= 0;
|
||||||
|
return Success(success, country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,27 +8,25 @@ namespace BlueWest.WebApi.EF
|
||||||
internal static class CurrencyExtensions
|
internal static class CurrencyExtensions
|
||||||
{
|
{
|
||||||
|
|
||||||
internal static Currency GetCurrencyById(this DbSet<Currency> currencies, int currencyId) => currencies.GetOne(x => x.Id == currencyId);
|
internal static (bool, Currency) NotFound() => (false, null);
|
||||||
internal static (bool, T) NotFound<T>() where T : class => (false, null);
|
|
||||||
|
|
||||||
|
|
||||||
internal static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate)
|
internal static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate)
|
||||||
{
|
{
|
||||||
|
|
||||||
var newCurrency = new Currency(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)
|
internal static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate)
|
||||||
{
|
{
|
||||||
var currency = dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
|
var currency = dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
|
||||||
|
if (currency == null) return NotFound();
|
||||||
if (currency == null) return NotFound<Currency>();
|
currency.Update(currencyToUpdate);
|
||||||
|
dbContext.Update(currency);
|
||||||
var newCurrency = new Currency(currencyToUpdate);
|
var resultOperation = dbContext.SaveChanges() >= 0;
|
||||||
var operationResult = dbContext.Currencies.Update(dbContext, newCurrency);
|
return (resultOperation, currency);
|
||||||
return operationResult;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<T>(this DbSet<T> dbSet, Expression<Func<T,bool>> predicate) where T: class => dbSet.FirstOrDefault(predicate);
|
|
||||||
|
|
||||||
internal static async Task<T> GetOneAsync<T>(this DbSet<T> dbSet, Expression<Func<T,bool>> predicate) where T: class => await dbSet.FirstOrDefaultAsync(predicate);
|
|
||||||
|
|
||||||
|
|
||||||
internal static (bool, T) NotFound<T>() where T : class => (false, null);
|
|
||||||
|
|
||||||
internal static (bool, T) Add<T>(this DbSet<T> 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<T>(this DbSet<T> 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<T>(this DbSet<T> 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<T>(this DbSet<T> dbSet, DbContext dbContext, T objectToUpdate) where T: class
|
|
||||||
{
|
|
||||||
dbSet.Update(objectToUpdate);
|
|
||||||
var result = await dbContext.SaveChangesAsync() >= 0;
|
|
||||||
return (result, objectToUpdate);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
using BlueWest.Data;
|
using BlueWest.Data;
|
||||||
using BlueWest.WebApi.EF;
|
using BlueWest.WebApi.EF;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
@ -47,11 +49,17 @@ namespace BlueWest.WebApi.Controllers
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <response code="201">Returns the newly created country</response>
|
/// <response code="201">Returns the newly created country</response>
|
||||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult AddCountry(CountryCreate countryToCreate)
|
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});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -105,7 +113,7 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpGet("{countryId}", Name = nameof(GetCountryById))]
|
[HttpGet("{countryId}", Name = nameof(GetCountryById))]
|
||||||
public ActionResult GetCountryById(int countryId)
|
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)
|
if (array != null)
|
||||||
{
|
{
|
||||||
|
@ -125,17 +133,46 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpGet("{countryId}/currencies")]
|
[HttpGet("{countryId}/currencies")]
|
||||||
public ActionResult GetCountryCurrencies(int countryId)
|
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();
|
if (country == null) return new NotFoundResult();
|
||||||
|
|
||||||
var array = _dbContext
|
var array = _dbContext
|
||||||
.Countries
|
.Countries
|
||||||
.Where(data => data.Code == countryId)
|
.Where(data => data.Id == countryId)
|
||||||
.SelectMany(o => o.Currencies)
|
.SelectMany(o => o.Currencies)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
return Ok(array);
|
return Ok(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a currency to Country
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="countryId"></param>
|
||||||
|
/// <param name="currencyCreate"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[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<Func<Currency, bool>>[]
|
||||||
|
{
|
||||||
|
x => x.Code == currencyCreate.Code,
|
||||||
|
y => y.Num == currencyCreate.Num
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(country);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -88,7 +88,7 @@ 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.Currencies.GetCurrencyById(currencyId);
|
var currency = _dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
|
||||||
|
|
||||||
if (currency != null)
|
if (currency != null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit aa5b01cdc42a00e873f126049a9c111b1d5220fa
|
Subproject commit a6f511665605041c327c6578d2b9789a3c9351bd
|
Loading…
Reference in New Issue