2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/Pulsar/Features/Outfitting/OutfittingService.cs
Ben Parsons 68eff73dbd Add Startup Events to Database
Now emit startup events on conneciton
Some events still to add
2024-05-25 19:45:46 +10:00

50 lines
1.6 KiB
C#

namespace Pulsar.Features.Outfitting;
using Observatory.Framework.Files;
public interface IOutfittingService : IJournalHandler<OutfittingFile>;
public class OutfittingService(
IOptions<PulsarConfiguration> options,
IEventHubContext hub,
ILogger<OutfittingService> logger)
: IOutfittingService
{
public async Task<OutfittingFile> Get()
{
var filePath = Path.Combine(options.Value.JournalDirectory, FileName);
if (!FileHelper.ValidateFile(filePath))
{
return new OutfittingFile();
}
await using var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var outfitting = await JsonSerializer.DeserializeAsync<OutfittingFile>(file);
if (outfitting != null) return outfitting;
logger.LogWarning("Failed to deserialize outfitting file {File}", filePath);
return new OutfittingFile();
}
public async Task HandleFile(string path, CancellationToken token = new ())
{
if (!FileHelper.ValidateFile(path))
{
return;
}
var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var outfitting = await JsonSerializer.DeserializeAsync<OutfittingFile>(file, cancellationToken: token);
if (outfitting == null)
{
logger.LogWarning("Failed to deserialize outfitting file {FilePath}", file);
return;
}
await hub.Clients.All.OutfittingUpdated(outfitting);
}
public string FileName => FileHandlerService.OutfittingFileName;
}