You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

58 lines
1.6 KiB

using System.Globalization;
using System.IO;
using Housing.Models;
namespace Housing.Services;
public static class InspectionSettingsStore
{
public static InspectionJudgementSettings Load(string filePath)
{
if (!File.Exists(filePath))
{
var defaultSettings = new InspectionJudgementSettings();
Save(filePath, defaultSettings);
return defaultSettings;
}
var values = IniFile.LoadSection(filePath, "Judgement");
return new InspectionJudgementSettings
{
VMin = GetDecimal(values, "VMin", 0.00m),
VMax = GetDecimal(values, "VMax", 9.99m),
AMin = GetDecimal(values, "AMin", 0.00m),
AMax = GetDecimal(values, "AMax", 9.99m)
};
}
public static void Save(string filePath, InspectionJudgementSettings settings)
{
var lines = new[]
{
"[Judgement]",
$"VMin={Format(settings.VMin)}",
$"VMax={Format(settings.VMax)}",
$"AMin={Format(settings.AMin)}",
$"AMax={Format(settings.AMax)}"
};
File.WriteAllLines(filePath, lines);
}
private static decimal GetDecimal(Dictionary<string, string> values, string key, decimal defaultValue)
{
if (!values.TryGetValue(key, out var value))
{
return defaultValue;
}
return decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out var result)
? result
: defaultValue;
}
private static string Format(decimal value)
{
return value.ToString("0.00", CultureInfo.InvariantCulture);
}
}