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

55 lines
1.8 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
{
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>
/// Not found projection
/// </summary>
/// <returns></returns>
public static (bool, Currency) NotFound() => (false, null);
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
{
var currency = dbContext.Currencies.FirstOrDefault(x => x.Id == currencyId);
if (currency == null) return NotFound();
currency.Update(currencyToUpdate);
dbContext.Update(currency);
var resultOperation = dbContext.SaveChanges() >= 0;
return (resultOperation, currency);
2022-08-22 00:14:50 +03:00
}
}
}