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
|
|
|
|
{
|
|
|
|
internal static class CountryDbExtensions
|
|
|
|
{
|
|
|
|
|
2022-08-22 01:32:47 +03:00
|
|
|
internal static (bool, Country) NotFound() => (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);
|
2022-08-22 00:14:50 +03:00
|
|
|
|
2022-08-22 01:32:47 +03:00
|
|
|
internal static (bool, Country) UpdateCountry(
|
|
|
|
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 01:32:47 +03:00
|
|
|
if (country == null) return NotFound();
|
|
|
|
country.Update(countryUpdate);
|
|
|
|
dbContext.Countries.Update(country);
|
|
|
|
var result = dbContext.SaveChanges() >= 0;
|
|
|
|
return (result, country);
|
2022-08-22 00:14:50 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static async Task<Country> GetCountryByIdAsync(this DbSet<Country> countries, int countryId) =>
|
|
|
|
await countries.FirstOrDefaultAsync(x => x.Id == countryId);
|
|
|
|
|
2022-08-22 01:32:47 +03:00
|
|
|
|
|
|
|
internal static (bool, string, Country) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate)
|
2022-08-22 00:14:50 +03:00
|
|
|
{
|
2022-08-22 01:32:47 +03:00
|
|
|
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
|
2022-08-22 00:14:50 +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);
|
|
|
|
if (currencyToGet != null) return ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
|
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
}
|
|
|
|
|
2022-08-22 01:32:47 +03:00
|
|
|
// Creates new currency
|
|
|
|
var newCurrency = new Currency(currencyCreate);
|
|
|
|
country.Currencies.Add(newCurrency);
|
|
|
|
var success = dbContext.SaveChanges() >= 0;
|
|
|
|
return Success(success, country);
|
2022-08-22 00:14:50 +03:00
|
|
|
}
|
2022-08-22 01:32:47 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|