From 408578e0980b29c8343f97dc3824c9b082db52d1 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Sat, 2 Mar 2024 00:40:56 +0100 Subject: [PATCH] feat: configuration --- .../Controllers/ConfigurationController.cs | 25 ++ .../Controllers/McoController.cs | 14 +- Minecraft-Realms-Emulator/Data/DataContext.cs | 1 + .../Entities/Configuration.cs | 11 + Minecraft-Realms-Emulator/Helpers/Database.cs | 30 ++ .../20240301214442_Configuration.Designer.cs | 324 ++++++++++++++++++ .../20240301214442_Configuration.cs | 33 ++ .../Migrations/DataContextModelSnapshot.cs | 14 + Minecraft-Realms-Emulator/Program.cs | 4 + 9 files changed, 454 insertions(+), 2 deletions(-) create mode 100644 Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs create mode 100644 Minecraft-Realms-Emulator/Entities/Configuration.cs create mode 100644 Minecraft-Realms-Emulator/Helpers/Database.cs create mode 100644 Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.Designer.cs create mode 100644 Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.cs diff --git a/Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs b/Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs new file mode 100644 index 0000000..84fce0a --- /dev/null +++ b/Minecraft-Realms-Emulator/Controllers/ConfigurationController.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc; +using Minecraft_Realms_Emulator.Data; +using Minecraft_Realms_Emulator.Entities; + +namespace Minecraft_Realms_Emulator.Controllers +{ + [Route("[controller]")] + [ApiController] + public class ConfigurationController : ControllerBase + { + private readonly DataContext _context; + + public ConfigurationController(DataContext context) + { + _context = context; + } + + [HttpGet] + public ActionResult GetConfigurationAsync() + { + var configuration = _context.Configuration; + return Ok(configuration); + } + } +} \ No newline at end of file diff --git a/Minecraft-Realms-Emulator/Controllers/McoController.cs b/Minecraft-Realms-Emulator/Controllers/McoController.cs index dbd29bc..d4c7ebf 100644 --- a/Minecraft-Realms-Emulator/Controllers/McoController.cs +++ b/Minecraft-Realms-Emulator/Controllers/McoController.cs @@ -1,12 +1,20 @@ using Microsoft.AspNetCore.Mvc; +using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Responses; -namespace Minecraft_Realms_Emulator.Controllers.Mco +namespace Minecraft_Realms_Emulator.Controllers { [Route("[controller]")] [ApiController] public class McoController : ControllerBase { + private readonly DataContext _context; + + public McoController(DataContext context) + { + _context = context; + } + [HttpGet("available")] public bool GetAvailable() { @@ -22,9 +30,11 @@ namespace Minecraft_Realms_Emulator.Controllers.Mco [HttpGet("v1/news")] public NewsResponse GetNews() { + var newsLink = _context.Configuration.FirstOrDefault(s => s.Key == "newsLink"); + var news = new NewsResponse { - NewsLink = "https://github.com/CyberL1/Minecraft-Realms-Emulator" + NewsLink = newsLink.Value }; return news; diff --git a/Minecraft-Realms-Emulator/Data/DataContext.cs b/Minecraft-Realms-Emulator/Data/DataContext.cs index 0d05db3..e2562b8 100644 --- a/Minecraft-Realms-Emulator/Data/DataContext.cs +++ b/Minecraft-Realms-Emulator/Data/DataContext.cs @@ -11,5 +11,6 @@ namespace Minecraft_Realms_Emulator.Data public DbSet Backups { get; set; } public DbSet Invites { get; set; } public DbSet Players { get; set; } + public DbSet Configuration { get; set; } } } diff --git a/Minecraft-Realms-Emulator/Entities/Configuration.cs b/Minecraft-Realms-Emulator/Entities/Configuration.cs new file mode 100644 index 0000000..514f586 --- /dev/null +++ b/Minecraft-Realms-Emulator/Entities/Configuration.cs @@ -0,0 +1,11 @@ +using Microsoft.EntityFrameworkCore; + +namespace Minecraft_Realms_Emulator.Entities +{ + [PrimaryKey(nameof(Key))] + public class Configuration + { + public string Key { get; set; } = string.Empty; + public string Value { get; set; } = string.Empty; + } +} diff --git a/Minecraft-Realms-Emulator/Helpers/Database.cs b/Minecraft-Realms-Emulator/Helpers/Database.cs new file mode 100644 index 0000000..1c76a4a --- /dev/null +++ b/Minecraft-Realms-Emulator/Helpers/Database.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using Minecraft_Realms_Emulator.Data; +using Minecraft_Realms_Emulator.Entities; + +namespace Minecraft_Realms_Emulator.Helpers +{ + public class Database + { + public static void Initialize(WebApplication app) + { + var scope = app.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + + db.Database.Migrate(); + + if (db.Configuration.FirstOrDefault(s => s.Key == "newsLink") == null) + { + var newsLink = new Configuration + { + Key = "newsLink", + Value = "https://github.com/CyberL1/Minecraft-Realms-Emulator" + }; + + db.Configuration.Add(newsLink); + + db.SaveChanges(); + } + } + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.Designer.cs b/Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.Designer.cs new file mode 100644 index 0000000..40c432d --- /dev/null +++ b/Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.Designer.cs @@ -0,0 +1,324 @@ +// +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("20240301214442_Configuration")] + partial class Configuration + { + /// + 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("text"); + + 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") + .IsRequired() + .HasColumnType("text"); + + b.Property("SubscriptionType") + .IsRequired() + .HasColumnType("text"); + + b.Property("WorldId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("WorldId"); + + 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("DaysLeft") + .HasColumnType("integer"); + + b.Property("Expired") + .HasColumnType("boolean"); + + b.Property("ExpiredTrial") + .HasColumnType("boolean"); + + 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("RemoteSubscriptionId") + .IsRequired() + .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") + .WithMany() + .HasForeignKey("WorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("World"); + }); + + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b => + { + b.Navigation("Players"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.cs b/Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.cs new file mode 100644 index 0000000..6817710 --- /dev/null +++ b/Minecraft-Realms-Emulator/Migrations/20240301214442_Configuration.cs @@ -0,0 +1,33 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Minecraft_Realms_Emulator.Migrations +{ + /// + public partial class Configuration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Configuration", + columns: table => new + { + Key = table.Column(type: "text", nullable: false), + Value = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Configuration", x => x.Key); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Configuration"); + } + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs b/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs index 50a154e..e760e0e 100644 --- a/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs +++ b/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs @@ -55,6 +55,20 @@ namespace Minecraft_Realms_Emulator.Migrations b.ToTable("Backups"); }); + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b => + { + b.Property("Key") + .HasColumnType("text"); + + b.Property("Value") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Key"); + + b.ToTable("Configuration"); + }); + modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b => { b.Property("Id") diff --git a/Minecraft-Realms-Emulator/Program.cs b/Minecraft-Realms-Emulator/Program.cs index 0b338aa..0c4a572 100644 --- a/Minecraft-Realms-Emulator/Program.cs +++ b/Minecraft-Realms-Emulator/Program.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using Minecraft_Realms_Emulator.Data; +using Minecraft_Realms_Emulator.Helpers; var builder = WebApplication.CreateBuilder(args); @@ -17,6 +18,9 @@ builder.Services.AddDbContext(options => var app = builder.Build(); +// Initialize database +Database.Initialize(app); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) {