TourIAm/TIAMWebApp/Server/Services/ExchangeRateService.cs

35 lines
1.1 KiB
C#

using System.IO;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Hosting;
using TIAMWebApp.Shared.Application.Models;
namespace TIAMWebApp.Server.Services
{
public class ExchangeRateService
{
private readonly IWebHostEnvironment _env;
private readonly string _filePath;
public ExchangeRateService(IWebHostEnvironment env)
{
_env = env;
_filePath = Path.Combine(_env.WebRootPath, "ExchangeRate.xml");
}
public async Task<ExchangeRate> GetExchangeRateAsync()
{
using var stream = new FileStream(_filePath, FileMode.Open);
var serializer = new XmlSerializer(typeof(ExchangeRate));
return (ExchangeRate)serializer.Deserialize(stream);
}
public async Task SetExchangeRateAsync(ExchangeRate rate)
{
using var stream = new FileStream(_filePath, FileMode.Create);
var serializer = new XmlSerializer(typeof(ExchangeRate));
serializer.Serialize(stream, rate);
}
}
}