Handle DB nullability
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
namespace SPTarkov.Server.Core.Exceptions.Database;
|
||||
|
||||
public class DatabaseNullException : Exception
|
||||
{
|
||||
public DatabaseNullException(string message)
|
||||
: base(message) { }
|
||||
|
||||
public DatabaseNullException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
namespace SPTarkov.Server.Core.Exceptions.Database;
|
||||
|
||||
internal class DatabaseTablesAlreadySetException : Exception
|
||||
{
|
||||
public DatabaseTablesAlreadySetException(string message)
|
||||
: base(message) { }
|
||||
|
||||
public DatabaseTablesAlreadySetException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
}
|
||||
@@ -10,23 +10,23 @@ public record DatabaseTables
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, object> ExtensionData { get; init; } = [];
|
||||
|
||||
public Bots.Bots Bots { get; set; }
|
||||
public required Bots.Bots Bots { get; set; }
|
||||
|
||||
public Hideout.Hideout Hideout { get; set; }
|
||||
public required Hideout.Hideout Hideout { get; set; }
|
||||
|
||||
public LocaleBase Locales { get; set; }
|
||||
public required LocaleBase Locales { get; set; }
|
||||
|
||||
public Locations Locations { get; set; }
|
||||
public required Locations Locations { get; set; }
|
||||
|
||||
public Match Match { get; set; }
|
||||
public required Match Match { get; set; }
|
||||
|
||||
public Templates.Templates Templates { get; set; }
|
||||
public required Templates.Templates Templates { get; set; }
|
||||
|
||||
public Dictionary<MongoId, Trader> Traders { get; set; }
|
||||
public required Dictionary<MongoId, Trader> Traders { get; set; }
|
||||
|
||||
public Globals Globals { get; set; }
|
||||
public required Globals Globals { get; set; }
|
||||
|
||||
public ServerBase Server { get; set; }
|
||||
public required ServerBase Server { get; set; }
|
||||
|
||||
public SettingsBase Settings { get; set; }
|
||||
public required SettingsBase Settings { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using SPTarkov.DI.Annotations;
|
||||
using SPTarkov.Server.Core.Exceptions.Database;
|
||||
using SPTarkov.Server.Core.Models.Spt.Server;
|
||||
|
||||
namespace SPTarkov.Server.Core.Servers;
|
||||
@@ -6,15 +7,37 @@ namespace SPTarkov.Server.Core.Servers;
|
||||
[Injectable(InjectionType.Singleton)]
|
||||
public class DatabaseServer
|
||||
{
|
||||
protected DatabaseTables tableData = new();
|
||||
protected DatabaseTables? TableData { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the database tables.
|
||||
/// </summary>
|
||||
/// <returns>The database tables if they have been initialized.</returns>
|
||||
/// <exception cref="DatabaseNullException">Thrown when the database tables have not been initialized.</exception>
|
||||
public DatabaseTables GetTables()
|
||||
{
|
||||
return tableData;
|
||||
if (TableData is null)
|
||||
{
|
||||
throw new DatabaseNullException("Database tables have not been initialized!");
|
||||
}
|
||||
|
||||
return TableData;
|
||||
}
|
||||
|
||||
public void SetTables(DatabaseTables tables)
|
||||
/// <summary>
|
||||
/// Sets the database tables for this instance. Can only be called once.
|
||||
/// </summary>
|
||||
/// <param name="tables">The database tables to set.</param>
|
||||
/// <exception cref="DatabaseTablesAlreadySetException">Thrown if the database tables are already set.</exception>
|
||||
internal void SetTables(DatabaseTables tables)
|
||||
{
|
||||
tableData = tables;
|
||||
if (TableData is null)
|
||||
{
|
||||
TableData = tables;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new DatabaseTablesAlreadySetException("The database is already initialized!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user