feat: configuration

This commit is contained in:
CyberL1 2024-03-02 00:40:56 +01:00
parent 937e5aeb8c
commit 408578e098
9 changed files with 454 additions and 2 deletions

View File

@ -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<Configuration> GetConfigurationAsync()
{
var configuration = _context.Configuration;
return Ok(configuration);
}
}
}

View File

@ -1,12 +1,20 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Responses; using Minecraft_Realms_Emulator.Responses;
namespace Minecraft_Realms_Emulator.Controllers.Mco namespace Minecraft_Realms_Emulator.Controllers
{ {
[Route("[controller]")] [Route("[controller]")]
[ApiController] [ApiController]
public class McoController : ControllerBase public class McoController : ControllerBase
{ {
private readonly DataContext _context;
public McoController(DataContext context)
{
_context = context;
}
[HttpGet("available")] [HttpGet("available")]
public bool GetAvailable() public bool GetAvailable()
{ {
@ -22,9 +30,11 @@ namespace Minecraft_Realms_Emulator.Controllers.Mco
[HttpGet("v1/news")] [HttpGet("v1/news")]
public NewsResponse GetNews() public NewsResponse GetNews()
{ {
var newsLink = _context.Configuration.FirstOrDefault(s => s.Key == "newsLink");
var news = new NewsResponse var news = new NewsResponse
{ {
NewsLink = "https://github.com/CyberL1/Minecraft-Realms-Emulator" NewsLink = newsLink.Value
}; };
return news; return news;

View File

@ -11,5 +11,6 @@ namespace Minecraft_Realms_Emulator.Data
public DbSet<Backup> Backups { get; set; } public DbSet<Backup> Backups { get; set; }
public DbSet<Invite> Invites { get; set; } public DbSet<Invite> Invites { get; set; }
public DbSet<Player> Players { get; set; } public DbSet<Player> Players { get; set; }
public DbSet<Configuration> Configuration { get; set; }
} }
} }

View File

@ -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;
}
}

View File

@ -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<DataContext>();
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();
}
}
}
}

View File

@ -0,0 +1,324 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("BackupId")
.IsRequired()
.HasColumnType("text");
b.Property<long>("LastModifiedDate")
.HasColumnType("bigint");
b.Property<JsonDocument>("Metadata")
.IsRequired()
.HasColumnType("jsonb");
b.Property<int>("Size")
.HasColumnType("integer");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Backups");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b =>
{
b.Property<string>("Key")
.HasColumnType("text");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("text");
b.HasKey("Key");
b.ToTable("Configuration");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("PendingUpdate")
.HasColumnType("boolean");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Connections");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("Date")
.HasColumnType("timestamp with time zone");
b.Property<string>("InvitationId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("RecipeintUUID")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Invites");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<bool>("Accepted")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("Online")
.HasColumnType("boolean");
b.Property<bool>("Operator")
.HasColumnType("boolean");
b.Property<string>("Permission")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Uuid")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Players");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("StartDate")
.IsRequired()
.HasColumnType("text");
b.Property<string>("SubscriptionType")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Subscriptions");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ActiveSlot")
.HasColumnType("integer");
b.Property<int>("DaysLeft")
.HasColumnType("integer");
b.Property<bool>("Expired")
.HasColumnType("boolean");
b.Property<bool>("ExpiredTrial")
.HasColumnType("boolean");
b.Property<int>("MaxPlayers")
.HasColumnType("integer");
b.Property<bool>("Member")
.HasColumnType("boolean");
b.Property<int?>("MinigameId")
.HasColumnType("integer");
b.Property<string>("MinigameImage")
.HasColumnType("text");
b.Property<string>("MinigameName")
.HasColumnType("text");
b.Property<string>("Motd")
.HasColumnType("text");
b.Property<string>("Name")
.HasColumnType("text");
b.Property<string>("Owner")
.HasColumnType("text");
b.Property<string>("OwnerUUID")
.HasColumnType("text");
b.Property<string>("RemoteSubscriptionId")
.IsRequired()
.HasColumnType("text");
b.Property<JsonDocument[]>("Slots")
.IsRequired()
.HasColumnType("jsonb[]");
b.Property<string>("State")
.IsRequired()
.HasColumnType("text");
b.Property<string>("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
}
}
}

View File

@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class Configuration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Configuration",
columns: table => new
{
Key = table.Column<string>(type: "text", nullable: false),
Value = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Configuration", x => x.Key);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Configuration");
}
}
}

View File

@ -55,6 +55,20 @@ namespace Minecraft_Realms_Emulator.Migrations
b.ToTable("Backups"); b.ToTable("Backups");
}); });
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b =>
{
b.Property<string>("Key")
.HasColumnType("text");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("text");
b.HasKey("Key");
b.ToTable("Configuration");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b => modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")

View File

@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Helpers;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -17,6 +18,9 @@ builder.Services.AddDbContext<DataContext>(options =>
var app = builder.Build(); var app = builder.Build();
// Initialize database
Database.Initialize(app);
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {