diff --git a/Minecraft-Realms-Emulator/Controllers/TrialController.cs b/Minecraft-Realms-Emulator/Controllers/TrialController.cs index 314a9a2..fdca4a2 100644 --- a/Minecraft-Realms-Emulator/Controllers/TrialController.cs +++ b/Minecraft-Realms-Emulator/Controllers/TrialController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Minecraft_Realms_Emulator.Data; namespace Minecraft_Realms_Emulator.Controllers { @@ -6,9 +7,16 @@ namespace Minecraft_Realms_Emulator.Controllers [ApiController] public class TrialController : ControllerBase { + private readonly DataContext _context; + + public TrialController(DataContext context) + { + _context = context; + } + [HttpGet(Name = "GetTrial")] - public bool Get() { - return true; + public bool Get() { + return bool.Parse(_context.Configuration.FirstOrDefault(x => x.Key == "trialMode").Value); } } } diff --git a/Minecraft-Realms-Emulator/Controllers/WorldsController.cs b/Minecraft-Realms-Emulator/Controllers/WorldsController.cs index f1cbe94..2754678 100644 --- a/Minecraft-Realms-Emulator/Controllers/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Controllers/WorldsController.cs @@ -2,8 +2,6 @@ using Microsoft.EntityFrameworkCore; using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Entities; -using System.Collections.Generic; -using System.Diagnostics.Eventing.Reader; namespace Minecraft_Realms_Emulator.Controllers { diff --git a/Minecraft-Realms-Emulator/Entities/Configuration.cs b/Minecraft-Realms-Emulator/Entities/Configuration.cs index 514f586..54c3aae 100644 --- a/Minecraft-Realms-Emulator/Entities/Configuration.cs +++ b/Minecraft-Realms-Emulator/Entities/Configuration.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations.Schema; namespace Minecraft_Realms_Emulator.Entities { @@ -6,6 +7,7 @@ namespace Minecraft_Realms_Emulator.Entities public class Configuration { public string Key { get; set; } = string.Empty; - public string Value { get; set; } = string.Empty; + [Column(TypeName = "jsonb")] + public dynamic Value { get; set; } = string.Empty; } } diff --git a/Minecraft-Realms-Emulator/Helpers/Database.cs b/Minecraft-Realms-Emulator/Helpers/Database.cs index 2461401..c59f9ba 100644 --- a/Minecraft-Realms-Emulator/Helpers/Database.cs +++ b/Minecraft-Realms-Emulator/Helpers/Database.cs @@ -18,7 +18,7 @@ namespace Minecraft_Realms_Emulator.Helpers var newsLink = new Configuration { Key = "newsLink", - Value = "https://github.com/CyberL1/Minecraft-Realms-Emulator" + Value = "\"https://github.com/CyberL1/Minecraft-Realms-Emulator\"" }; db.Configuration.Add(newsLink); @@ -29,12 +29,23 @@ namespace Minecraft_Realms_Emulator.Helpers var defaultServerAddress = new Configuration { Key = "defaultServerAddress", - Value = "127.0.0.1" + Value = "\"127.0.0.1\"" }; db.Configuration.Add(defaultServerAddress); } + if (db.Configuration.FirstOrDefault(x => x.Key == "trialMode") == null) + { + var trialMode = new Configuration + { + Key = "trialMode", + Value = true + }; + + db.Configuration.Add(trialMode); + } + db.SaveChanges(); } } diff --git a/Minecraft-Realms-Emulator/Migrations/20240414062222_Configuration_Json.Designer.cs b/Minecraft-Realms-Emulator/Migrations/20240414062222_Configuration_Json.Designer.cs new file mode 100644 index 0000000..55479b4 --- /dev/null +++ b/Minecraft-Realms-Emulator/Migrations/20240414062222_Configuration_Json.Designer.cs @@ -0,0 +1,313 @@ +// +using System; +using System.Text.Json; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Minecraft_Realms_Emulator.Data; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Minecraft_Realms_Emulator.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20240414062222_Configuration_Json")] + partial class Configuration_Json + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BackupId") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastModifiedDate") + .HasColumnType("bigint"); + + b.Property("Metadata") + .IsRequired() + .HasColumnType("jsonb"); + + b.Property("Size") + .HasColumnType("integer"); + + b.Property("WorldId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("WorldId"); + + b.ToTable("Backups"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b => + { + b.Property("Key") + .HasColumnType("text"); + + b.Property("Value") + .IsRequired() + .HasColumnType("jsonb"); + + b.HasKey("Key"); + + b.ToTable("Configuration"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("PendingUpdate") + .HasColumnType("boolean"); + + b.Property("WorldId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("WorldId"); + + b.ToTable("Connections"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("InvitationId") + .IsRequired() + .HasColumnType("text"); + + b.Property("RecipeintUUID") + .IsRequired() + .HasColumnType("text"); + + b.Property("WorldId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("WorldId"); + + b.ToTable("Invites"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Accepted") + .HasColumnType("boolean"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Online") + .HasColumnType("boolean"); + + b.Property("Operator") + .HasColumnType("boolean"); + + b.Property("Permission") + .IsRequired() + .HasColumnType("text"); + + b.Property("Uuid") + .IsRequired() + .HasColumnType("text"); + + b.Property("WorldId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("WorldId"); + + b.ToTable("Players"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("StartDate") + .HasColumnType("timestamp with time zone"); + + b.Property("SubscriptionType") + .IsRequired() + .HasColumnType("text"); + + b.Property("WorldId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("WorldId") + .IsUnique(); + + b.ToTable("Subscriptions"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ActiveSlot") + .HasColumnType("integer"); + + b.Property("MaxPlayers") + .HasColumnType("integer"); + + b.Property("Member") + .HasColumnType("boolean"); + + b.Property("MinigameId") + .HasColumnType("integer"); + + b.Property("MinigameImage") + .HasColumnType("text"); + + b.Property("MinigameName") + .HasColumnType("text"); + + b.Property("Motd") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Owner") + .HasColumnType("text"); + + b.Property("OwnerUUID") + .HasColumnType("text"); + + b.Property("Slots") + .IsRequired() + .HasColumnType("jsonb[]"); + + b.Property("State") + .IsRequired() + .HasColumnType("text"); + + b.Property("WorldType") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Worlds"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b => + { + b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World") + .WithMany() + .HasForeignKey("WorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("World"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b => + { + b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World") + .WithMany() + .HasForeignKey("WorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("World"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b => + { + b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World") + .WithMany() + .HasForeignKey("WorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("World"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b => + { + b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World") + .WithMany("Players") + .HasForeignKey("WorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("World"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b => + { + b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World") + .WithOne("Subscription") + .HasForeignKey("Minecraft_Realms_Emulator.Entities.Subscription", "WorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("World"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b => + { + b.Navigation("Players"); + + b.Navigation("Subscription"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/20240414062222_Configuration_Json.cs b/Minecraft-Realms-Emulator/Migrations/20240414062222_Configuration_Json.cs new file mode 100644 index 0000000..0e8ca69 --- /dev/null +++ b/Minecraft-Realms-Emulator/Migrations/20240414062222_Configuration_Json.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Minecraft_Realms_Emulator.Migrations +{ + /// + public partial class Configuration_Json : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql("ALTER TABLE \"Configuration\" ALTER COLUMN \"Value\" TYPE jsonb USING \"Value\"::jsonb"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql("ALTER TABLE \"Configuration\" ALTER COLUMN \"Value\" TYPE text USING \"Value\"::text"); + } + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs b/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs index ec88393..12d1c8f 100644 --- a/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs +++ b/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs @@ -52,7 +52,7 @@ namespace Minecraft_Realms_Emulator.Migrations b.HasIndex("WorldId"); - b.ToTable("Backups", (string)null); + b.ToTable("Backups"); }); modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b => @@ -60,13 +60,13 @@ namespace Minecraft_Realms_Emulator.Migrations b.Property("Key") .HasColumnType("text"); - b.Property("Value") + b.Property("Value") .IsRequired() - .HasColumnType("text"); + .HasColumnType("jsonb"); b.HasKey("Key"); - b.ToTable("Configuration", (string)null); + b.ToTable("Configuration"); }); modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b => @@ -91,7 +91,7 @@ namespace Minecraft_Realms_Emulator.Migrations b.HasIndex("WorldId"); - b.ToTable("Connections", (string)null); + b.ToTable("Connections"); }); modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b => @@ -120,7 +120,7 @@ namespace Minecraft_Realms_Emulator.Migrations b.HasIndex("WorldId"); - b.ToTable("Invites", (string)null); + b.ToTable("Invites"); }); modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b => @@ -159,7 +159,7 @@ namespace Minecraft_Realms_Emulator.Migrations b.HasIndex("WorldId"); - b.ToTable("Players", (string)null); + b.ToTable("Players"); }); modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b => @@ -185,7 +185,7 @@ namespace Minecraft_Realms_Emulator.Migrations b.HasIndex("WorldId") .IsUnique(); - b.ToTable("Subscriptions", (string)null); + b.ToTable("Subscriptions"); }); modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b => @@ -240,7 +240,7 @@ namespace Minecraft_Realms_Emulator.Migrations b.HasKey("Id"); - b.ToTable("Worlds", (string)null); + b.ToTable("Worlds"); }); modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b => diff --git a/Minecraft-Realms-Emulator/Program.cs b/Minecraft-Realms-Emulator/Program.cs index c08b2a9..cb19d23 100644 --- a/Minecraft-Realms-Emulator/Program.cs +++ b/Minecraft-Realms-Emulator/Program.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Helpers; +using Npgsql; var builder = WebApplication.CreateBuilder(args); DotNetEnv.Env.Load(); @@ -18,9 +19,13 @@ builder.Services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.Re builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +var dataSourceBuilder = new NpgsqlDataSourceBuilder(Environment.GetEnvironmentVariable("CONNECTION_STRING")); +dataSourceBuilder.EnableDynamicJson(); +await using var dataSource = dataSourceBuilder.Build(); + builder.Services.AddDbContext(options => { - options.UseNpgsql(Environment.GetEnvironmentVariable("CONNECTION_STRING")); + options.UseNpgsql(dataSource); }); var app = builder.Build();