CodeLiturgy.Dashboard/BlueWest.Api/Context/Extensions/CurrencyExtensions.cs

96 lines
3.4 KiB
C#
Raw Normal View History

2022-08-22 05:57:06 +03:00
using System;
2022-08-22 00:14:50 +03:00
using System.Collections.Generic;
using System.Linq;
2022-08-22 05:57:06 +03:00
using System.Linq.Expressions;
2022-08-22 18:02:48 +03:00
using System.Threading.Tasks;
2022-08-22 00:14:50 +03:00
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.EF
{
2022-08-22 02:51:45 +03:00
/// <summary>
/// Currency table data extensions
/// </summary>
public static class CurrencyExtensions
2022-08-22 00:14:50 +03:00
{
2022-08-22 02:51:45 +03:00
/// <summary>
/// Add new Currency
/// </summary>
/// <param name="dbContext"></param>
/// <param name="currencyToCreate"></param>
/// <returns></returns>
public static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate)
2022-08-22 00:14:50 +03:00
{
var newCurrency = new Currency(currencyToCreate);
dbContext.Add(newCurrency);
var resultOperation = dbContext.SaveChanges() >= 0;
return (resultOperation, newCurrency);
2022-08-22 00:14:50 +03:00
}
2022-08-22 02:51:45 +03:00
/// <summary>
/// Updates currency
/// </summary>
/// <param name="dbContext"></param>
/// <param name="currencyId"></param>
/// <param name="currencyToUpdate"></param>
/// <returns></returns>
public static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate)
2022-08-22 00:14:50 +03:00
{
2022-08-22 18:02:48 +03:00
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;
return (resultOperation, currency);
2022-08-22 00:14:50 +03:00
}
2022-08-22 05:57:06 +03:00
2022-08-22 18:02:48 +03:00
/// <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>
2022-08-22 05:57:06 +03:00
public static (bool, string, CountryUnique) AddCountry(
this CountryDbContext dbContext,
int currencyId, CountryCreate countryCreate,
Expression<Func<Country,bool>>[] duplicationValidations)
{
2022-08-22 18:02:48 +03:00
var queryable = from aCurrency in dbContext.Currencies
where aCurrency.Id == currencyId
select aCurrency;
2022-08-22 05:57:06 +03:00
2022-08-22 18:02:48 +03:00
queryable.Include(x => x.Countries);
var currency = queryable.SingleOrDefault();
2022-08-22 05:57:06 +03:00
// Check if currency exists
if (currency == null) return (false, $"{nameof(Currency)}: {currencyId} - Not found.", null);
// Check if there's currency with the same code
foreach (var duplicationValidation in duplicationValidations)
{
2022-08-22 18:02:48 +03:00
var countryToCheck = dbContext.Countries.FirstOrDefault(duplicationValidation);
if (countryToCheck != null)
{
return (false, $"Duplication Validation failed: {duplicationValidation.Body}", null);
}
2022-08-22 05:57:06 +03:00
}
// Creates new currency
var newCountry = new Country(countryCreate);
currency.Countries.Add(newCountry);
var success = dbContext.SaveChanges() >= 0;
2022-08-22 18:02:48 +03:00
return (success, string.Empty, new CountryUnique(newCountry));
2022-08-22 05:57:06 +03:00
}
2022-08-22 18:02:48 +03:00
2022-08-22 00:14:50 +03:00
}
}