feat: config helper

This commit is contained in:
CyberL1 2024-05-22 18:05:58 +02:00
parent 3a951beeb3
commit e05a82d4bc
7 changed files with 102 additions and 36 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Helpers;
namespace Minecraft_Realms_Emulator.Controllers
{
@ -16,10 +17,12 @@ namespace Minecraft_Realms_Emulator.Controllers
}
[HttpGet]
public ActionResult<Configuration> GetConfigurationAsync()
public ActionResult<Configuration> GetConfiguration()
{
var configuration = _context.Configuration;
return Ok(configuration);
var config = new ConfigHelper(_context);
var settings = config.GetSettings();
return Ok(settings);
}
}
}

View File

@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Helpers;
using Minecraft_Realms_Emulator.Responses;
using Newtonsoft.Json;
namespace Minecraft_Realms_Emulator.Controllers
{
@ -19,28 +19,29 @@ namespace Minecraft_Realms_Emulator.Controllers
}
[HttpGet("available")]
public bool GetAvailable()
public ActionResult<bool> GetAvailable()
{
return true;
return Ok(true);
}
[HttpGet("client/compatible")]
public string GetCompatible()
public ActionResult<string> GetCompatible()
{
return Compatility.COMPATIBLE.ToString();
return Ok("COMPATIBLE");
}
[HttpGet("v1/news")]
public NewsResponse GetNews()
public ActionResult<NewsResponse> GetNews()
{
var newsLink = _context.Configuration.FirstOrDefault(s => s.Key == "newsLink");
var config = new ConfigHelper(_context);
var newsLink = config.GetSetting("newsLink");
var news = new NewsResponse
{
NewsLink = JsonConvert.DeserializeObject(newsLink.Value),
NewsLink = newsLink.Value
};
return news;
return Ok(news);
}
}
}

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Helpers;
namespace Minecraft_Realms_Emulator.Controllers
{
@ -16,9 +17,13 @@ namespace Minecraft_Realms_Emulator.Controllers
_context = context;
}
[HttpGet(Name = "GetTrial")]
public bool Get() {
return bool.Parse(_context.Configuration.FirstOrDefault(x => x.Key == "trialMode").Value);
[HttpGet]
public ActionResult<bool> GetTrial()
{
var config = new ConfigHelper(_context);
var trialMode = config.GetSetting("trialMode");
return Ok(trialMode.Value);
}
}
}

View File

@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Helpers;
using Minecraft_Realms_Emulator.Requests;
using Minecraft_Realms_Emulator.Responses;
using Newtonsoft.Json;
@ -233,12 +234,13 @@ namespace Minecraft_Realms_Emulator.Controllers
world.State = "OPEN";
world.Subscription = subscription;
var defaultServerAddress = _context.Configuration.FirstOrDefault(x => x.Key == "defaultServerAddress");
var config = new ConfigHelper(_context);
var defaultServerAddress = config.GetSetting("defaultServerAddress");
var connection = new Connection
{
World = world,
Address = JsonConvert.DeserializeObject(defaultServerAddress.Value)
Address = defaultServerAddress.Value
};
Slot slot = new()

View File

@ -0,0 +1,47 @@
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Newtonsoft.Json;
namespace Minecraft_Realms_Emulator.Helpers
{
public class ConfigHelper
{
private readonly DataContext Db;
public ConfigHelper(DataContext db)
{
Db = db;
}
public List<Configuration> GetSettings()
{
List<Configuration> settings = [];
foreach (var setting in Db.Configuration)
{
Configuration settingTyped = new()
{
Key = setting.Key,
Value = JsonConvert.DeserializeObject(setting.Value)
};
settings.Add(settingTyped);
}
return settings;
}
public Configuration? GetSetting(string key)
{
var setting = Db.Configuration.Find(key);
if (setting == null) return null;
return new()
{
Key = setting.Key,
Value = JsonConvert.DeserializeObject(setting.Value)
};
}
}
}

View File

@ -0,0 +1,9 @@
namespace Minecraft_Realms_Emulator.Helpers.Config
{
public class Settings : Dictionary<string, string>
{
public string DefaultServerAddress { get; set; } = "127.0.0.1";
public string NewsLink { get; set; } = "https://github.com/CyberL1/Minecraft-Realms-Emulator";
public bool TrialMode { get; set; } = true;
}
}

View File

@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Helpers.Config;
using Newtonsoft.Json;
namespace Minecraft_Realms_Emulator.Helpers
{
@ -13,37 +15,34 @@ namespace Minecraft_Realms_Emulator.Helpers
db.Database.Migrate();
if (!db.Configuration.Any(s => s.Key == "newsLink"))
var config = new ConfigHelper(db);
var settings = new Settings();
if (config.GetSetting("newsLink") == null)
{
var newsLink = new Configuration
db.Configuration.Add(new Configuration
{
Key = "newsLink",
Value = "\"https://github.com/CyberL1/Minecraft-Realms-Emulator\""
};
db.Configuration.Add(newsLink);
Value = JsonConvert.SerializeObject(settings.NewsLink)
});
}
if (!db.Configuration.Any(s => s.Key == "defaultServerAddress"))
if (config.GetSetting("defaultServerAddress") == null)
{
var defaultServerAddress = new Configuration
db.Configuration.Add(new Configuration
{
Key = "defaultServerAddress",
Value = "\"127.0.0.1\""
};
db.Configuration.Add(defaultServerAddress);
Value = JsonConvert.SerializeObject(settings.DefaultServerAddress)
});
}
if (!db.Configuration.Any(x => x.Key == "trialMode"))
if (config.GetSetting("trialMode") == null)
{
var trialMode = new Configuration
db.Configuration.Add(new Configuration
{
Key = "trialMode",
Value = true
};
db.Configuration.Add(trialMode);
Value = JsonConvert.SerializeObject(settings.TrialMode)
});
}
db.SaveChanges();