2022-08-22 01:32:47 +03:00
|
|
|
using System;
|
2022-08-22 00:14:50 +03:00
|
|
|
using System.Linq;
|
2022-08-22 01:32:47 +03:00
|
|
|
using System.Linq.Expressions;
|
2022-08-22 00:14:50 +03:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BlueWest.Data;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.EF
|
|
|
|
{
|
2022-08-22 02:51:45 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Country table database extensions
|
|
|
|
/// </summary>
|
2022-08-28 21:39:40 +03:00
|
|
|
public static class CountryExtensions2
|
2022-08-22 00:14:50 +03:00
|
|
|
{
|
2022-08-22 02:51:45 +03:00
|
|
|
|
2022-08-23 19:48:16 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Add a new country
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dbContext"></param>
|
|
|
|
/// <param name="countryCreate"></param>
|
|
|
|
/// <returns></returns>
|
2022-08-24 19:56:51 +03:00
|
|
|
/*public static (bool, CountryUnique) AddCountry(this CountryDbContext dbContext, CountryCreate countryCreate)
|
2022-08-23 19:48:16 +03:00
|
|
|
{
|
|
|
|
Country newCountry = new Country(countryCreate);
|
|
|
|
dbContext.Countries.Add(newCountry);
|
|
|
|
bool success = dbContext.SaveChanges() >= 0;
|
|
|
|
return (success, new CountryUnique(newCountry));
|
2022-08-24 19:56:51 +03:00
|
|
|
} */
|
2022-08-22 02:51:45 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Updates a country data.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dbContext">DbContext.</param>
|
|
|
|
/// <param name="countryUpdate">Country data to update.</param>
|
|
|
|
/// <param name="countryId">Country Id.</param>
|
|
|
|
/// <returns></returns>
|
2022-08-29 03:40:57 +03:00
|
|
|
/*public static (bool, Country) UpdateCountry(
|
2022-08-22 01:32:47 +03:00
|
|
|
this CountryDbContext dbContext,
|
|
|
|
CountryUpdate countryUpdate,
|
2022-08-22 00:14:50 +03:00
|
|
|
int countryId)
|
|
|
|
{
|
|
|
|
var country = dbContext.Countries.FirstOrDefault(x => x.Id == countryId);
|
2022-08-22 18:02:48 +03:00
|
|
|
if (country == null) return (false, null);
|
2022-08-22 01:32:47 +03:00
|
|
|
country.Update(countryUpdate);
|
|
|
|
dbContext.Countries.Update(country);
|
|
|
|
var result = dbContext.SaveChanges() >= 0;
|
|
|
|
return (result, country);
|
2022-08-22 00:14:50 +03:00
|
|
|
|
2022-08-29 03:40:57 +03:00
|
|
|
}*/
|
2022-08-22 00:14:50 +03:00
|
|
|
|
2022-08-22 02:51:45 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a new Currency to the specified country
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dbContext"></param>
|
|
|
|
/// <param name="countryId"></param>
|
|
|
|
/// <param name="currencyCreate"></param>
|
|
|
|
/// <returns></returns>
|
2022-08-24 19:56:51 +03:00
|
|
|
/*public static (bool, string, CurrencyUnique) AddCurrency(
|
2022-08-23 19:48:16 +03:00
|
|
|
this CountryDbContext dbContext,
|
|
|
|
int countryId,
|
|
|
|
CurrencyCreate currencyCreate)
|
2022-08-22 00:14:50 +03:00
|
|
|
{
|
2022-08-22 02:51:45 +03:00
|
|
|
var country = dbContext.Countries.FirstOrDefault(d => d.Id == countryId);
|
2022-08-22 01:32:47 +03:00
|
|
|
|
|
|
|
// 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;
|
2022-08-22 18:02:48 +03:00
|
|
|
|
2022-08-24 19:56:51 +03:00
|
|
|
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CurrencyUnique(newCurrency));
|
|
|
|
}*/
|
2022-08-23 19:48:16 +03:00
|
|
|
// country add (add currency) currency create, duplicatioon Validations
|
|
|
|
|
2022-08-22 02:51:45 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Add Currency with optional duplication checks
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dbContext"></param>
|
|
|
|
/// <param name="countryId">Country Id</param>
|
|
|
|
/// <param name="currencyCreate">Data to create currency</param>
|
|
|
|
/// <param name="duplicationValidations">List of expressions</param>
|
|
|
|
/// <returns></returns>
|
2022-08-24 19:56:51 +03:00
|
|
|
/*public static (bool, string, CurrencyUnique) AddCurrency(
|
2022-08-22 01:32:47 +03:00
|
|
|
this CountryDbContext dbContext,
|
|
|
|
int countryId, CurrencyCreate currencyCreate,
|
|
|
|
Expression<Func<Currency,bool>>[] duplicationValidations)
|
|
|
|
{
|
2022-08-22 05:13:53 +03:00
|
|
|
|
2022-08-23 19:48:16 +03:00
|
|
|
var countryQuery = from aCountry in dbContext.Countries
|
|
|
|
where aCountry.Id == countryId
|
|
|
|
let currencies = aCountry.Currencies
|
|
|
|
select aCountry;
|
2022-08-22 05:13:53 +03:00
|
|
|
|
2022-08-23 19:48:16 +03:00
|
|
|
var country = countryQuery.FirstOrDefault();
|
2022-08-22 01:32:47 +03:00
|
|
|
|
|
|
|
if (country == null) return (false, $"{nameof(country)} Not found.", null);
|
2022-08-23 19:48:16 +03:00
|
|
|
|
2022-08-22 01:32:47 +03:00
|
|
|
foreach (var duplicationValidation in duplicationValidations)
|
2022-08-22 00:14:50 +03:00
|
|
|
{
|
2022-08-22 01:32:47 +03:00
|
|
|
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
|
2022-08-22 18:02:48 +03:00
|
|
|
if (currencyToGet != null)
|
|
|
|
{
|
|
|
|
return (false, $"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}", null);
|
|
|
|
}
|
2022-08-22 00:14:50 +03:00
|
|
|
}
|
2022-08-23 19:48:16 +03:00
|
|
|
|
2022-08-22 01:32:47 +03:00
|
|
|
var newCurrency = new Currency(currencyCreate);
|
|
|
|
country.Currencies.Add(newCurrency);
|
|
|
|
var success = dbContext.SaveChanges() >= 0;
|
2022-08-23 19:48:16 +03:00
|
|
|
return !success ? (false, "Error saving the changes in the database.", null) : (true, string.Empty, new CurrencyUnique(newCurrency));
|
2022-08-24 19:56:51 +03:00
|
|
|
}*/
|
2022-08-22 00:14:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|