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

120 lines
4.4 KiB
C#

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.EF
{
/// <summary>
/// Country table database extensions
/// </summary>
public static class CountryDbExtensions
{
static CountryDbExtensions()
{
}
/// <summary>
/// Returns Not found projection
/// </summary>
/// <returns></returns>
public static (bool, Country) NotFound() => (false, null);
/// <summary>
/// Returns Error Message projection
/// </summary>
/// <param name="message"></param>
/// <returns>(false, message, null)</returns>
public static (bool, string, Country) ErrorMessage(string message) => (false, message, null);
/// <summary>
/// Returns Success intent projection
/// </summary>
/// <param name="success">Success message</param>
/// <param name="country">Entity object</param>
/// <returns>(bool success, Country country)</returns>
public static (bool, string, Country) Success(bool success, Country country) => (success, "1", null);
/// <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>
public static (bool, Country) UpdateCountry(
this CountryDbContext dbContext,
CountryUpdate countryUpdate,
int 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);
}
/// <summary>
/// Adds a new Currency to the specified country
/// </summary>
/// <param name="dbContext"></param>
/// <param name="countryId"></param>
/// <param name="currencyCreate"></param>
/// <returns></returns>
public static (bool, string, Country) AddCurrency( this CountryDbContext dbContext, int countryId, CurrencyCreate currencyCreate)
{
var country = dbContext.Countries.FirstOrDefault(d => d.Id == 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);
}
/// <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>
public static (bool, string, Country) AddCurrency(
this CountryDbContext dbContext,
int countryId, CurrencyCreate currencyCreate,
Expression<Func<Currency,bool>>[] duplicationValidations)
{
var country = dbContext.Countries.FirstOrDefault(d => d.Id == 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 currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
if (currencyToGet != null) return ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
}
// Creates new currency
var newCurrency = new Currency(currencyCreate);
country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0;
return Success(success, country);
}
}
}