phase 1
This commit is contained in:
52
ModVersionChecker/repository/filesystem/SourcesDefManager.cs
Normal file
52
ModVersionChecker/repository/filesystem/SourcesDefManager.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.repository.api.dto;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ModVersionChecker.managers.filesystem
|
||||
{
|
||||
public class SourcesDefManager
|
||||
{
|
||||
private readonly string _filePath = Path.Combine(AppContext.BaseDirectory, "data", "sourcesDef.json");
|
||||
private List<SourceResponse> _sourcesDef = new List<SourceResponse>();
|
||||
|
||||
public SourcesDefManager()
|
||||
{
|
||||
_sourcesDef = Load();
|
||||
}
|
||||
|
||||
private List<SourceResponse> Load()
|
||||
{
|
||||
if (!File.Exists(_filePath))
|
||||
return new List<SourceResponse>();
|
||||
var json = File.ReadAllText(_filePath);
|
||||
return JsonSerializer.Deserialize<List<SourceResponse>>(json) ?? new();
|
||||
}
|
||||
|
||||
public List<SourceResponse> GetSourcesDef()
|
||||
{
|
||||
return _sourcesDef;
|
||||
}
|
||||
|
||||
public void AddSourceDef(SourceResponse sourceDef)
|
||||
{
|
||||
_sourcesDef.Add(sourceDef);
|
||||
Save(_sourcesDef);
|
||||
}
|
||||
|
||||
public void RemoveSourceDef(string id)
|
||||
{
|
||||
_sourcesDef.RemoveAll(s => s.Id == id);
|
||||
Save(_sourcesDef);
|
||||
}
|
||||
|
||||
|
||||
public void Save(List<SourceResponse> sourcesDef)
|
||||
{
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
var json = JsonSerializer.Serialize(sourcesDef, options);
|
||||
File.WriteAllText(_filePath, json);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user