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

103 lines
3.7 KiB
C#
Raw Normal View History

using System;
2022-08-22 00:14:50 +03:00
using System.Linq;
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>
public static class CountryDbExtensions
2022-08-22 00:14:50 +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>
public 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 05:57:06 +03:00
if (country == null) return Projection.NotFound;
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-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>
public static (bool, string, Country) AddCurrency( 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);
// 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 05:57:06 +03:00
return Projection.Success(success, country);
}
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>
public static (bool, string, Country) AddCurrency(
this CountryDbContext dbContext,
int countryId, CurrencyCreate currencyCreate,
Expression<Func<Currency,bool>>[] duplicationValidations)
{
var country = dbContext
.Countries
.Where(data => data.Id == countryId)
.Include(o => o.Currencies)
.FirstOrDefault();
// 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
foreach (var duplicationValidation in duplicationValidations)
2022-08-22 00:14:50 +03:00
{
var currencyToGet = dbContext.Currencies.FirstOrDefault(duplicationValidation);
2022-08-22 05:57:06 +03:00
if (currencyToGet != null) return Projection.ErrorMessage($"Duplication Validation failed: {nameof(duplicationValidation.Body.ToString)}");
2022-08-22 00:14:50 +03:00
}
// Creates new currency
var newCurrency = new Currency(currencyCreate);
country.Currencies.Add(newCurrency);
var success = dbContext.SaveChanges() >= 0;
2022-08-22 05:57:06 +03:00
return Projection.Success(success, country);
2022-08-22 00:14:50 +03:00
}
2022-08-22 05:57:06 +03:00
2022-08-22 00:14:50 +03:00
}
}