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

37 lines
1.2 KiB
C#
Raw Normal View History

2022-08-22 00:14:50 +03:00
using System.Collections.Generic;
using System.Linq;
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.EF
{
internal static class CurrencyExtensions
{
internal static Currency GetCurrencyById(this DbSet<Currency> currencies, int currencyId) => currencies.GetOne(x => x.Id == currencyId);
internal static (bool, T) NotFound<T>() where T : class => (false, null);
internal static (bool, Currency) AddCurrency(this CountryDbContext dbContext, CurrencyCreate currencyToCreate)
{
var newCurrency = new Currency(currencyToCreate);
return dbContext.Currencies.Add(dbContext, newCurrency);
}
internal static (bool, Currency) UpdateCurrency(this CountryDbContext dbContext, int currencyId, CurrencyUpdate currencyToUpdate)
{
var currency = dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
if (currency == null) return NotFound<Currency>();
var newCurrency = new Currency(currencyToUpdate);
var operationResult = dbContext.Currencies.Update(dbContext, newCurrency);
return operationResult;
}
}
}