phase 1
This commit is contained in:
27
ModVersionChecker/repository/api/ApiBase.cs
Normal file
27
ModVersionChecker/repository/api/ApiBase.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.repository.api
|
||||
{
|
||||
public class ApiBase: IDisposable
|
||||
{
|
||||
private readonly HttpClient _httpClient = new HttpClient();
|
||||
|
||||
public ApiBase()
|
||||
{
|
||||
_httpClient.Timeout = TimeSpan.FromSeconds(30);
|
||||
_httpClient.DefaultRequestHeaders.Add("User-Agent", "ModVersionChecker");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
169
ModVersionChecker/repository/api/ApiRepository.cs
Normal file
169
ModVersionChecker/repository/api/ApiRepository.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
using ModVersionChecker.repository.api.dto;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.repository.api
|
||||
{
|
||||
public class ApiRepository : IApiRepository, IDisposable
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private string baseUrl = "http://192.168.1.115:3115/api";
|
||||
private JwtTokenResponse? _accessToken;
|
||||
private JwtTokenResponse? _refreshToken;
|
||||
private DateTime _accessTokenExpiry = DateTime.MinValue;
|
||||
|
||||
public ApiRepository()
|
||||
{
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.Timeout = TimeSpan.FromSeconds(30);
|
||||
}
|
||||
|
||||
public async Task<bool> AuthenticateAsync(string username, string password)
|
||||
{
|
||||
var url = $"{baseUrl}/auth";
|
||||
var payload = new { username, password };
|
||||
var jsonPayload = JsonSerializer.Serialize(payload);
|
||||
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PostAsync(url, content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var authentication = JsonSerializer.Deserialize<AuthenticationResponse>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
_accessToken = DecodeJwt(authentication?.AccessToken);
|
||||
_refreshToken = DecodeJwt(authentication?.RefreshToken);
|
||||
if (_accessToken == null)
|
||||
throw new Exception("Failed to decode access token.");
|
||||
_accessTokenExpiry = DateTime.UtcNow.AddSeconds(_accessToken.ExpireAt - 60);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private JwtTokenResponse? DecodeJwt(string? token)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token))
|
||||
return null;
|
||||
var parts = token.Split('.');
|
||||
if (parts.Length != 3)
|
||||
throw new ArgumentException("Invalid JWT token format.");
|
||||
var payload = parts[1];
|
||||
var paddedPayload = payload.PadRight(payload.Length + (4 - payload.Length % 4) % 4, '=');
|
||||
var jsonBytes = Convert.FromBase64String(paddedPayload);
|
||||
var json = Encoding.UTF8.GetString(jsonBytes);
|
||||
var doc = JsonSerializer.Deserialize<JwtTokenResponse>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
if (doc != null)
|
||||
doc.Token = token;
|
||||
return doc;
|
||||
}
|
||||
|
||||
private async Task<bool> EnsureTokenValidAsync()
|
||||
{
|
||||
if (_accessToken == null || DateTime.UtcNow >= _accessTokenExpiry)
|
||||
{
|
||||
return await RefreshTokenAsync();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<bool> RefreshTokenAsync()
|
||||
{
|
||||
if (_refreshToken == null)
|
||||
return false;
|
||||
var refreshData = new { refreshToken = _refreshToken };
|
||||
var content = new StringContent(JsonSerializer.Serialize(refreshData), Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PostAsync($"{baseUrl}/auth/refresh", content);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return false;
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var authentication = JsonSerializer.Deserialize<AuthenticationResponse>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
_accessToken = DecodeJwt(authentication?.AccessToken);
|
||||
if (_accessToken == null)
|
||||
throw new Exception("Failed to decode access token.");
|
||||
_accessTokenExpiry = DateTime.UtcNow.AddSeconds(_accessToken.ExpireAt - 60);
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<HttpRequestMessage> CreateRequestAsync(HttpMethod method, string url)
|
||||
{
|
||||
await EnsureTokenValidAsync();
|
||||
var request = new HttpRequestMessage(method, url);
|
||||
if (_accessToken != null)
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token);
|
||||
return request;
|
||||
}
|
||||
|
||||
public async Task<List<AppVersionsResponse>?> GetAppVersionsAsync(List<App> apps)
|
||||
{
|
||||
var url = $"{baseUrl}/app/versions?{string.Join("&", apps.Select(a => $"version={Uri.EscapeDataString(a.Id)}"))}";
|
||||
var request = await CreateRequestAsync(HttpMethod.Get, url);
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<List<AppVersionsResponse>>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
public async Task<AppVersionsResponse?> GetAppLatestVersionAsync(App app)
|
||||
{
|
||||
var url = $"{baseUrl}/app/latest?version={Uri.EscapeDataString(app.Id)}";
|
||||
var request = await CreateRequestAsync(HttpMethod.Get, url);
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<AppVersionsResponse>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
|
||||
public async Task<List<TypeResponse>> GetTypes()
|
||||
{
|
||||
var url = $"{baseUrl}/type";
|
||||
var request = await CreateRequestAsync(HttpMethod.Get, url);
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<List<TypeResponse>>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
|
||||
public async Task<List<SourceResponse>> GetSources()
|
||||
{
|
||||
var url = $"{baseUrl}/source";
|
||||
var request = await CreateRequestAsync(HttpMethod.Get, url);
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<List<SourceResponse>>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
|
||||
public async Task<List<AppResponse>?> SearchApps(string searchText)
|
||||
{
|
||||
var url = $"{baseUrl}/app/search?query={Uri.EscapeDataString(searchText)}";
|
||||
var request = await CreateRequestAsync(HttpMethod.Get, url);
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<List<AppResponse>>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
}
|
||||
|
||||
public async Task<List<AppResponse>> GetAppsByIds(App[] apps)
|
||||
{
|
||||
var query = string.Join("&", apps.Select(a => $"id={Uri.EscapeDataString(a.Id)}"));
|
||||
var url = $"{baseUrl}/app/search?{query}";
|
||||
var request = await CreateRequestAsync(HttpMethod.Get, url);
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<List<AppResponse>>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new List<AppResponse>();
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
17
ModVersionChecker/repository/api/IApiRepository.cs
Normal file
17
ModVersionChecker/repository/api/IApiRepository.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using ModVersionChecker.repository.api.dto;
|
||||
|
||||
namespace ModVersionChecker.repository.api
|
||||
{
|
||||
public interface IApiRepository
|
||||
{
|
||||
Task<bool> AuthenticateAsync(string username, string password);
|
||||
Task<List<AppVersionsResponse>?> GetAppVersionsAsync(List<App> apps);
|
||||
Task<AppVersionsResponse?> GetAppLatestVersionAsync(App app);
|
||||
Task<List<AppResponse>?> SearchApps(string searchText);
|
||||
Task<List<TypeResponse>> GetTypes();
|
||||
Task<List<SourceResponse>> GetSources();
|
||||
Task<List<AppResponse>> GetAppsByIds(App[] apps);
|
||||
}
|
||||
}
|
81
ModVersionChecker/repository/api/dto/AppResponse.cs
Normal file
81
ModVersionChecker/repository/api/dto/AppResponse.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using ModVersionChecker.enums;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ModVersionChecker.repository.api.dto
|
||||
{
|
||||
public class AppResponse
|
||||
{
|
||||
public AppResponse() { }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("uid")]
|
||||
public string Uid { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; } = string.Empty;
|
||||
|
||||
|
||||
[JsonPropertyName("source")]
|
||||
public string Source { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("params")]
|
||||
public Dictionary<string, string> Params { get; set; } = new Dictionary<string, string>();
|
||||
|
||||
[JsonPropertyName("fields")]
|
||||
public Dictionary<string, string> Fields { get; set; } = new Dictionary<string, string>();
|
||||
|
||||
[JsonPropertyName("downloadUrl")]
|
||||
public string DownloadUrl { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("currentVersion")]
|
||||
public string CurrentVersion { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("latestVersion")]
|
||||
public string LatestVersion { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("status")]
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public AppStatus Status { get; set; } = AppStatus.NONE;
|
||||
|
||||
[JsonPropertyName("createdAt")]
|
||||
public long CreatedAt { get; set; } = 0;
|
||||
|
||||
[JsonPropertyName("updatedAt")]
|
||||
public long UpdatedAt { get; set; } = 0;
|
||||
|
||||
[JsonPropertyName("lastCheckedAt")]
|
||||
public long LastCheckedAt { get; set; } = 0;
|
||||
|
||||
[JsonPropertyName("active")]
|
||||
public bool Active { get; set; } = false;
|
||||
|
||||
public static App toModel(AppResponse appResponse)
|
||||
{
|
||||
if (appResponse == null)
|
||||
{
|
||||
return new App();
|
||||
}
|
||||
return new App()
|
||||
{
|
||||
Id = appResponse.Id,
|
||||
Uid = appResponse.Uid,
|
||||
Name = appResponse.Name,
|
||||
Type = appResponse.Type,
|
||||
Source = appResponse.Source,
|
||||
Params = appResponse.Params,
|
||||
Fields = appResponse.Fields,
|
||||
DownloadUrl = appResponse.DownloadUrl,
|
||||
CurrentVersion = appResponse.CurrentVersion,
|
||||
LatestVersion = appResponse.LatestVersion,
|
||||
Status = appResponse.Status,
|
||||
LastCheckedAt = appResponse.LastCheckedAt,
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
15
ModVersionChecker/repository/api/dto/AppVersionsResponse.cs
Normal file
15
ModVersionChecker/repository/api/dto/AppVersionsResponse.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ModVersionChecker.repository.api.dto
|
||||
{
|
||||
public class AppVersionsResponse
|
||||
{
|
||||
public AppVersionsResponse() { }
|
||||
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("latestVersion")]
|
||||
public string LatestVersion { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.repository.api.dto
|
||||
{
|
||||
public class AuthenticationResponse
|
||||
{
|
||||
[JsonPropertyName("accessToken")]
|
||||
public string AccessToken { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("refreshToken")]
|
||||
public string RefreshToken { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
32
ModVersionChecker/repository/api/dto/FieldResponse.cs
Normal file
32
ModVersionChecker/repository/api/dto/FieldResponse.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.repository.api.dto
|
||||
{
|
||||
public class FieldResponse
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("label")]
|
||||
public string Label { get; set; } = string.Empty;
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("required")]
|
||||
public bool Required { get; set; } = false;
|
||||
|
||||
[JsonPropertyName("controlType")]
|
||||
public string ControlType { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("defaultValue")]
|
||||
public string DefaultValue { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
26
ModVersionChecker/repository/api/dto/JwtTokenResponse.cs
Normal file
26
ModVersionChecker/repository/api/dto/JwtTokenResponse.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.repository.api.dto
|
||||
{
|
||||
public class JwtTokenResponse
|
||||
{
|
||||
public JwtTokenResponse() { }
|
||||
public string Token { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("exp")]
|
||||
public long ExpireAt { get; set; } = 0;
|
||||
|
||||
[JsonPropertyName("iat")]
|
||||
public long IssuedAt { get; set; } = 0;
|
||||
|
||||
[JsonPropertyName("sub")]
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
|
||||
|
||||
}
|
||||
}
|
19
ModVersionChecker/repository/api/dto/SourceResponse.cs
Normal file
19
ModVersionChecker/repository/api/dto/SourceResponse.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ModVersionChecker.repository.api.dto
|
||||
{
|
||||
public class SourceResponse
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("defaults")]
|
||||
public Dictionary<string, string> Defaults { get; set; } = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
27
ModVersionChecker/repository/api/dto/TypeResponse.cs
Normal file
27
ModVersionChecker/repository/api/dto/TypeResponse.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.repository.api.dto
|
||||
{
|
||||
public class TypeResponse
|
||||
{
|
||||
|
||||
public string Id { get; set; } = String.Empty;
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("shortName")]
|
||||
public string ShortName { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("configFields")]
|
||||
public List<FieldResponse> ConfigFields { get; set; } = new List<FieldResponse>();
|
||||
|
||||
[JsonPropertyName("appFields")]
|
||||
public List<FieldResponse> AppFields { get; set; } = new List<FieldResponse>();
|
||||
}
|
||||
}
|
52
ModVersionChecker/repository/filesystem/AppStatusManager.cs
Normal file
52
ModVersionChecker/repository/filesystem/AppStatusManager.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using ModVersionChecker.enums;
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.filesystem
|
||||
{
|
||||
|
||||
public class AppStatusManager : IAppStatusManager
|
||||
{
|
||||
private Dictionary<string, AppStatus> _statuses = new Dictionary<string, AppStatus>();
|
||||
|
||||
public AppStatusManager() { }
|
||||
public AppStatus? GetAppStatus(string appId)
|
||||
{
|
||||
if (!_statuses.ContainsKey(appId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _statuses[appId];
|
||||
}
|
||||
public List<AppStatus> Load()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public void Save(List<AppStatus> appStatuses)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public void UpdateAppStatus(string appId, AppStatus appStatus)
|
||||
{
|
||||
if (_statuses.ContainsKey(appId))
|
||||
{
|
||||
_statuses[appId] = appStatus;
|
||||
} else
|
||||
{
|
||||
_statuses.Add(appId, appStatus);
|
||||
}
|
||||
}
|
||||
public bool DeleteAppStatus(string appId) {
|
||||
return _statuses.Remove(appId);
|
||||
}
|
||||
public void ClearAll()
|
||||
{
|
||||
_statuses.Clear();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
42
ModVersionChecker/repository/filesystem/AppsManager.cs
Normal file
42
ModVersionChecker/repository/filesystem/AppsManager.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ModVersionChecker.managers.filesystem
|
||||
{
|
||||
public class AppsManager
|
||||
{
|
||||
private readonly string FilePath = Path.Combine(AppContext.BaseDirectory, "data", "apps.json");
|
||||
|
||||
public List<App> Load()
|
||||
{
|
||||
if (!File.Exists(FilePath))
|
||||
return new List<App>();
|
||||
var json = File.ReadAllText(FilePath);
|
||||
return JsonSerializer.Deserialize<List<App>>(json) ?? new();
|
||||
}
|
||||
|
||||
public void Save(List<App> apps)
|
||||
{
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
var json = JsonSerializer.Serialize(apps, options);
|
||||
File.WriteAllText(FilePath, json);
|
||||
}
|
||||
|
||||
public void Upsert(App app)
|
||||
{
|
||||
var apps = Load();
|
||||
var index = apps.FindIndex(a => a.Id == app.Id);
|
||||
if (index >= 0)
|
||||
{
|
||||
apps[index] = app;
|
||||
}
|
||||
else
|
||||
{
|
||||
apps.Add(app);
|
||||
}
|
||||
Save(apps);
|
||||
}
|
||||
}
|
||||
}
|
40
ModVersionChecker/repository/filesystem/ConfigManager.cs
Normal file
40
ModVersionChecker/repository/filesystem/ConfigManager.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.model;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ModVersionChecker.managers.filesystem
|
||||
{
|
||||
public class ConfigManager : IConfigManager
|
||||
{
|
||||
|
||||
private static readonly string _filePath = Path.Combine(AppContext.BaseDirectory, "data", "config.json");
|
||||
private Config _config;
|
||||
|
||||
public ConfigManager()
|
||||
{
|
||||
_config = Load();
|
||||
}
|
||||
|
||||
public Config Load()
|
||||
{
|
||||
if (!File.Exists(_filePath))
|
||||
return new Config();
|
||||
var json = File.ReadAllText(_filePath);
|
||||
return JsonSerializer.Deserialize<Config>(json) ?? new Config();
|
||||
}
|
||||
|
||||
public Config GetConfig()
|
||||
{
|
||||
return _config;
|
||||
}
|
||||
|
||||
public void Save(Config config)
|
||||
{
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
var json = JsonSerializer.Serialize(config, options);
|
||||
File.WriteAllText(_filePath, json);
|
||||
}
|
||||
}
|
||||
}
|
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);
|
||||
}
|
||||
}
|
||||
}
|
23
ModVersionChecker/repository/interfaces/IAppStatusManager.cs
Normal file
23
ModVersionChecker/repository/interfaces/IAppStatusManager.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using ModVersionChecker.enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.interfaces
|
||||
{
|
||||
public interface IAppStatusManager
|
||||
{
|
||||
List<AppStatus> Load();
|
||||
void Save(List<AppStatus> appStatuses);
|
||||
AppStatus? GetAppStatus(string appId);
|
||||
|
||||
void UpdateAppStatus(string appId, AppStatus appStatus);
|
||||
|
||||
bool DeleteAppStatus(string appId);
|
||||
|
||||
void ClearAll();
|
||||
|
||||
}
|
||||
}
|
26
ModVersionChecker/repository/interfaces/IAppsManager.cs
Normal file
26
ModVersionChecker/repository/interfaces/IAppsManager.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using ModVersionChecker.enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.interfaces
|
||||
{
|
||||
public interface IAppsManager
|
||||
{
|
||||
|
||||
List<App> Load();
|
||||
|
||||
void Save(List<App> apps);
|
||||
|
||||
public void Insert(App app);
|
||||
|
||||
public void Update(App app);
|
||||
|
||||
void Delete(string id);
|
||||
|
||||
void UpdateStatus(App app, AppStatus status);
|
||||
|
||||
}
|
||||
}
|
16
ModVersionChecker/repository/interfaces/IConfigManager.cs
Normal file
16
ModVersionChecker/repository/interfaces/IConfigManager.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using ModVersionChecker.model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.interfaces
|
||||
{
|
||||
public interface IConfigManager
|
||||
{
|
||||
Config Load();
|
||||
void Save(Config config);
|
||||
Config GetConfig();
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
using ModVersionChecker.repository.api.dto;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.interfaces
|
||||
{
|
||||
public interface IFlightSimsManager
|
||||
{
|
||||
List<TypeResponse> Load();
|
||||
void Save(TypeResponse config);
|
||||
|
||||
TypeResponse? GetByShortName(string id);
|
||||
|
||||
void DeleteAll();
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
using ModVersionChecker.repository.api.dto;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.interfaces
|
||||
{
|
||||
public interface ISourcesDefManager
|
||||
{
|
||||
List<SourceResponse> List();
|
||||
|
||||
SourceResponse? GetById(string id);
|
||||
void AddSourceDef(SourceResponse sourceDef);
|
||||
void RemoveSourceDef(string id);
|
||||
void Save(SourceResponse sourceDef);
|
||||
void DeleteAll();
|
||||
}
|
||||
}
|
13
ModVersionChecker/repository/interfaces/ITypeManager.cs
Normal file
13
ModVersionChecker/repository/interfaces/ITypeManager.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using ModVersionChecker.model;
|
||||
|
||||
namespace ModVersionChecker.managers.interfaces
|
||||
{
|
||||
public interface ITypeManager
|
||||
{
|
||||
List<TypeConfig> GetTypeConfigs();
|
||||
void SaveTypeConfigs(List<TypeConfig> types);
|
||||
TypeConfig? GetTypeConfigById(string id);
|
||||
void SaveTypeConfig(TypeConfig type);
|
||||
void DeleteTypeConfig(string id);
|
||||
}
|
||||
}
|
50
ModVersionChecker/repository/litedb/AppLiteDb.cs
Normal file
50
ModVersionChecker/repository/litedb/AppLiteDb.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using ModVersionChecker.enums;
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.litedb
|
||||
{
|
||||
public class AppLiteDb : LiteDb, IAppsManager
|
||||
{
|
||||
protected override string collection => LiteDb.APPS_COLLECTION;
|
||||
|
||||
public List<App> Load()
|
||||
{
|
||||
return GetCollection<App>().FindAll().ToList();
|
||||
}
|
||||
|
||||
public void Insert(App app)
|
||||
{
|
||||
var now = TimeUtils.GetUnixTimeMillis(null);
|
||||
GetCollection<App>().Insert(app);
|
||||
}
|
||||
|
||||
public void Update(App app)
|
||||
{
|
||||
var now = TimeUtils.GetUnixTimeMillis(null);
|
||||
GetCollection<App>().Update(app);
|
||||
}
|
||||
|
||||
public void Delete(string id)
|
||||
{
|
||||
GetCollection<App>().Delete(id);
|
||||
}
|
||||
|
||||
public void Save(List<App> apps)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void UpdateStatus(App app, AppStatus status)
|
||||
{
|
||||
app.LastCheckedAt = TimeUtils.GetUnixTimeMillis(null);
|
||||
app.Status = status;
|
||||
GetCollection<App>().Update(app);
|
||||
}
|
||||
}
|
||||
}
|
22
ModVersionChecker/repository/litedb/ConfigLiteDb.cs
Normal file
22
ModVersionChecker/repository/litedb/ConfigLiteDb.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.model;
|
||||
|
||||
namespace ModVersionChecker.managers.litedb
|
||||
{
|
||||
public class ConfigLiteDb : LiteDb, IConfigManager
|
||||
{
|
||||
protected override string collection => LiteDb.CONFIG_COLLECTION;
|
||||
public Config Load()
|
||||
{
|
||||
return GetCollection<Config>().FindAll().FirstOrDefault() ?? new Config();
|
||||
}
|
||||
public void Save(Config config)
|
||||
{
|
||||
GetCollection<Config>().Upsert(config);
|
||||
}
|
||||
public Config GetConfig()
|
||||
{
|
||||
return Load();
|
||||
}
|
||||
}
|
||||
}
|
25
ModVersionChecker/repository/litedb/LiteDb.cs
Normal file
25
ModVersionChecker/repository/litedb/LiteDb.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using LiteDB;
|
||||
|
||||
namespace ModVersionChecker.managers.litedb
|
||||
{
|
||||
public abstract class LiteDb
|
||||
{
|
||||
public static string DB_PATH = "ModVersionChecker.db";
|
||||
public static string APPS_COLLECTION = "apps";
|
||||
public static string CHECKER_TYPES_DEF_COLLECTION = "checker_types_def";
|
||||
public static string SOURCES_DEF_COLLECTION = "sources_def";
|
||||
public static string CONFIG_COLLECTION = "config";
|
||||
public static string FLIGHT_SIMS_COLLECTION = "flight_sims";
|
||||
public static string TYPES_COLLECTION = "types";
|
||||
public static string LOCAL_APPS_COLLECTION = "local_apps";
|
||||
|
||||
protected LiteDatabase _db = LiteDbSingleton.Instance;
|
||||
|
||||
protected abstract string collection { get; }
|
||||
|
||||
protected ILiteCollection<T> GetCollection<T>()
|
||||
{
|
||||
return _db.GetCollection<T>(collection);
|
||||
}
|
||||
}
|
||||
}
|
44
ModVersionChecker/repository/litedb/SourcesLiteDb.cs
Normal file
44
ModVersionChecker/repository/litedb/SourcesLiteDb.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.repository.api.dto;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.litedb
|
||||
{
|
||||
public class SourcesLiteDb : LiteDb, ISourcesDefManager
|
||||
{
|
||||
protected override string collection => SOURCES_DEF_COLLECTION;
|
||||
|
||||
public List<SourceResponse> List()
|
||||
{
|
||||
return GetCollection<SourceResponse>().FindAll().ToList();
|
||||
}
|
||||
|
||||
public SourceResponse? GetById(string id)
|
||||
{
|
||||
return GetCollection<SourceResponse>().FindOne(x => x.Id == id);
|
||||
}
|
||||
|
||||
public void AddSourceDef(SourceResponse sourceDef)
|
||||
{
|
||||
GetCollection<SourceResponse>().Insert(sourceDef);
|
||||
}
|
||||
public void RemoveSourceDef(string id)
|
||||
{
|
||||
GetCollection<SourceResponse>().Delete(id);
|
||||
}
|
||||
|
||||
public void Save(SourceResponse sourceDef)
|
||||
{
|
||||
GetCollection<SourceResponse>().Upsert(sourceDef);
|
||||
}
|
||||
|
||||
public void DeleteAll()
|
||||
{
|
||||
GetCollection<SourceResponse>().DeleteAll();
|
||||
}
|
||||
}
|
||||
}
|
35
ModVersionChecker/repository/litedb/TypeConfigLiteDb.cs
Normal file
35
ModVersionChecker/repository/litedb/TypeConfigLiteDb.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.model;
|
||||
|
||||
namespace ModVersionChecker.managers.litedb
|
||||
{
|
||||
internal class TypeConfigLiteDb : LiteDb, ITypeManager
|
||||
{
|
||||
protected override string collection => TYPES_COLLECTION;
|
||||
|
||||
public void DeleteTypeConfig(string id)
|
||||
{
|
||||
GetCollection<TypeConfig>().Delete(id);
|
||||
}
|
||||
|
||||
public TypeConfig? GetTypeConfigById(string id)
|
||||
{
|
||||
return GetCollection<TypeConfig>().FindOne(x => x.Id == id);
|
||||
}
|
||||
|
||||
public List<TypeConfig> GetTypeConfigs()
|
||||
{
|
||||
return GetCollection<TypeConfig>().FindAll().ToList();
|
||||
}
|
||||
|
||||
public void SaveTypeConfig(TypeConfig type)
|
||||
{
|
||||
GetCollection<TypeConfig>().Upsert(type);
|
||||
}
|
||||
|
||||
public void SaveTypeConfigs(List<TypeConfig> types)
|
||||
{
|
||||
GetCollection<TypeConfig>().InsertBulk(types);
|
||||
}
|
||||
}
|
||||
}
|
35
ModVersionChecker/repository/litedb/TypeLiteDb.cs
Normal file
35
ModVersionChecker/repository/litedb/TypeLiteDb.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using ModVersionChecker.managers.interfaces;
|
||||
using ModVersionChecker.repository.api.dto;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.managers.litedb
|
||||
{
|
||||
internal class TypeLiteDb : LiteDb, IFlightSimsManager
|
||||
{
|
||||
protected override string collection => FLIGHT_SIMS_COLLECTION;
|
||||
|
||||
public List<TypeResponse> Load()
|
||||
{
|
||||
return GetCollection<TypeResponse>().FindAll().ToList();
|
||||
}
|
||||
|
||||
public void Save(TypeResponse config)
|
||||
{
|
||||
GetCollection<TypeResponse>().Upsert(config);
|
||||
}
|
||||
|
||||
public TypeResponse? GetByShortName(string id)
|
||||
{
|
||||
return GetCollection<TypeResponse>().FindOne(x => x.ShortName == id);
|
||||
}
|
||||
|
||||
public void DeleteAll()
|
||||
{
|
||||
GetCollection<TypeResponse>().DeleteAll();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user