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.
148 lines
4.5 KiB
148 lines
4.5 KiB
|
1 month ago
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.IO;
|
||
|
|
using System.Xml.Serialization;
|
||
|
|
|
||
|
|
namespace Housing.Services
|
||
|
|
{
|
||
|
|
// XML 직렬화를 위한 데이터 컨테이너 클래스 정의
|
||
|
|
[XmlRoot("LoginHistory")]
|
||
|
|
public class LoginHistoryData
|
||
|
|
{
|
||
|
|
[XmlElement("Category")]
|
||
|
|
public List<HistoryCategory> Categories { get; set; } = new List<HistoryCategory>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public class HistoryCategory
|
||
|
|
{
|
||
|
|
[XmlAttribute("Name")]
|
||
|
|
public string Name { get; set; }
|
||
|
|
|
||
|
|
[XmlElement("Value")]
|
||
|
|
public List<string> Values { get; set; } = new List<string>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public class LoginHistoryStore
|
||
|
|
{
|
||
|
|
private readonly string _filePath;
|
||
|
|
private readonly Dictionary<string, List<string>> _history = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
||
|
|
|
||
|
|
private LoginHistoryStore(string filePath)
|
||
|
|
{
|
||
|
|
_filePath = filePath;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static LoginHistoryStore Load(string path)
|
||
|
|
{
|
||
|
|
var store = new LoginHistoryStore(path);
|
||
|
|
store.LoadFromFile();
|
||
|
|
return store;
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<string> GetValues(string fieldName)
|
||
|
|
{
|
||
|
|
if (_history.TryGetValue(fieldName, out var list))
|
||
|
|
{
|
||
|
|
return new List<string>(list);
|
||
|
|
}
|
||
|
|
return new List<string>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Remember(string fieldName, string value)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(value)) return;
|
||
|
|
value = value.Trim();
|
||
|
|
|
||
|
|
if (!_history.TryGetValue(fieldName, out var list))
|
||
|
|
{
|
||
|
|
list = new List<string>();
|
||
|
|
_history[fieldName] = list;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 중복 제거 및 맨 앞으로 이동
|
||
|
|
list.RemoveAll(x => string.Equals(x, value, StringComparison.OrdinalIgnoreCase));
|
||
|
|
list.Insert(0, value);
|
||
|
|
|
||
|
|
// 최대 5개 이력 유지
|
||
|
|
if (list.Count > 5)
|
||
|
|
{
|
||
|
|
list.RemoveRange(5, list.Count - 5);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool Forget(string fieldName, string value)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(value)) return false;
|
||
|
|
value = value.Trim();
|
||
|
|
|
||
|
|
if (_history.TryGetValue(fieldName, out var list))
|
||
|
|
{
|
||
|
|
bool removed = list.RemoveAll(x => string.Equals(x, value, StringComparison.OrdinalIgnoreCase)) > 0;
|
||
|
|
return removed;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Save()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var dir = Path.GetDirectoryName(_filePath);
|
||
|
|
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(dir);
|
||
|
|
}
|
||
|
|
|
||
|
|
var data = new LoginHistoryData();
|
||
|
|
foreach (var kvp in _history)
|
||
|
|
{
|
||
|
|
data.Categories.Add(new HistoryCategory
|
||
|
|
{
|
||
|
|
Name = kvp.Key,
|
||
|
|
Values = kvp.Value
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
XmlSerializer serializer = new XmlSerializer(typeof(LoginHistoryData));
|
||
|
|
using (var sw = new StreamWriter(_filePath))
|
||
|
|
{
|
||
|
|
serializer.Serialize(sw, data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
System.Diagnostics.Debug.WriteLine($"[LoginHistoryStore Save Error] {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void LoadFromFile()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (!File.Exists(_filePath)) return;
|
||
|
|
|
||
|
|
XmlSerializer serializer = new XmlSerializer(typeof(LoginHistoryData));
|
||
|
|
using (var sr = new StreamReader(_filePath))
|
||
|
|
{
|
||
|
|
var data = (LoginHistoryData)serializer.Deserialize(sr);
|
||
|
|
if (data?.Categories != null)
|
||
|
|
{
|
||
|
|
_history.Clear();
|
||
|
|
foreach (var cat in data.Categories)
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrEmpty(cat.Name))
|
||
|
|
{
|
||
|
|
_history[cat.Name] = cat.Values ?? new List<string>();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
System.Diagnostics.Debug.WriteLine($"[LoginHistoryStore Load Error] {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|