feat: GET /worlds, GET /worlds/{id}

This commit is contained in:
CyberL1 2024-02-02 18:53:52 +01:00
parent 77d5e2078d
commit 9183c566a6
16 changed files with 389 additions and 14 deletions

View File

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
namespace Minecraft_Realms_Emulator.Controllers.Mco namespace Minecraft_Realms_Emulator.Controllers.Mco
{ {

View File

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
namespace Minecraft_Realms_Emulator.Controllers.Mco.Client namespace Minecraft_Realms_Emulator.Controllers.Mco.Client
{ {

View File

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
namespace Minecraft_Realms_Emulator.Controllers namespace Minecraft_Realms_Emulator.Controllers
{ {

View File

@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
namespace Minecraft_Realms_Emulator.Controllers
{
[Route("[controller]")]
[ApiController]
public class WorldsController : ControllerBase
{
private readonly DataContext _context;
public WorldsController(DataContext context)
{
_context = context;
}
[HttpGet]
public async Task<ActionResult<ServersArray>> GetWorlds()
{
var worlds = await _context.Worlds.ToListAsync();
ServersArray servers = new()
{
Servers = worlds
};
return Ok(servers);
}
[HttpGet("{id}")]
public async Task<ActionResult<World>> GetWorldById(int id)
{
var world = await _context.Worlds.FindAsync(id);
if (world == null) return NotFound("World not found");
return world;
}
}
}

View File

@ -0,0 +1,10 @@
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Entities;
namespace Minecraft_Realms_Emulator.Data
{
public class DataContext(DbContextOptions<DataContext> options) : DbContext(options)
{
public DbSet<World> Worlds { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace Minecraft_Realms_Emulator.Entities
{
public class ServersArray
{
public List<World>? Servers { get; set; }
}
}

View File

@ -0,0 +1,10 @@
namespace Minecraft_Realms_Emulator.Entities
{
public enum State
{
OPEN,
CLOSED,
ADMIN_LOCK,
UNINITIALIZED
}
}

View File

@ -0,0 +1,25 @@
namespace Minecraft_Realms_Emulator.Entities
{
public class World
{
public int Id { get; set; }
public string? RemoteSubscriptionId { get; set; }
public string? Owner { get; set; }
public string? OwnerUUID { get; set; }
public string? Name { get; set; }
public string? Motd { get; set; }
public string State { get; set; } = "OPEN";
public int DaysLeft { get; set; } = 30;
public bool Expired { get; set; } = false;
public bool ExpiredTrial { get; set; } = false;
public string WorldType { get; set; } = "NORMAL";
public string[] Players { get; set; } = [];
public int MaxPlayers { get; set; } = 10;
public string? MinigameName { get; set; }
public int? MinigameId { get; set; }
public string? MinigameImage { get; set; }
public int ActiveSlot { get; set; } = 1;
public string[] Slots { get; set; } = [];
public bool Member { get; set; } = false;
}
}

View File

@ -0,0 +1,10 @@
namespace Minecraft_Realms_Emulator.Entities
{
public enum WorldType
{
NORMAL,
SUPERFLAT,
LARGEBIOMES,
AMPLIFIED
}
}

View File

@ -0,0 +1,101 @@
// <auto-generated />
using System;
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("20240202175209_Initial")]
partial class Initial
{
/// <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.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[]>("Players")
.IsRequired()
.HasColumnType("text[]");
b.Property<string>("RemoteSubscriptionId")
.HasColumnType("text");
b.Property<string[]>("Slots")
.IsRequired()
.HasColumnType("text[]");
b.Property<string>("State")
.IsRequired()
.HasColumnType("text");
b.Property<string>("WorldType")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Worlds");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,52 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Worlds",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RemoteSubscriptionId = table.Column<string>(type: "text", nullable: true),
Owner = table.Column<string>(type: "text", nullable: true),
OwnerUUID = table.Column<string>(type: "text", nullable: true),
Name = table.Column<string>(type: "text", nullable: true),
Motd = table.Column<string>(type: "text", nullable: true),
State = table.Column<string>(type: "text", nullable: false),
DaysLeft = table.Column<int>(type: "integer", nullable: false),
Expired = table.Column<bool>(type: "boolean", nullable: false),
ExpiredTrial = table.Column<bool>(type: "boolean", nullable: false),
WorldType = table.Column<string>(type: "text", nullable: false),
Players = table.Column<string[]>(type: "text[]", nullable: false),
MaxPlayers = table.Column<int>(type: "integer", nullable: false),
MinigameName = table.Column<string>(type: "text", nullable: true),
MinigameId = table.Column<int>(type: "integer", nullable: true),
MinigameImage = table.Column<string>(type: "text", nullable: true),
ActiveSlot = table.Column<int>(type: "integer", nullable: false),
Slots = table.Column<string[]>(type: "text[]", nullable: false),
Member = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Worlds", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Worlds");
}
}
}

View File

@ -0,0 +1,98 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
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))]
partial class DataContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(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.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[]>("Players")
.IsRequired()
.HasColumnType("text[]");
b.Property<string>("RemoteSubscriptionId")
.HasColumnType("text");
b.Property<string[]>("Slots")
.IsRequired()
.HasColumnType("text[]");
b.Property<string>("State")
.IsRequired()
.HasColumnType("text");
b.Property<string>("WorldType")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Worlds");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -9,7 +9,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -6,5 +6,11 @@ Accept: application/json
### ###
GET {{Minecraft_Realms_Emulator_HostAddress}}/mco/client/compatible GET {{Minecraft_Realms_Emulator_HostAddress}}/mco/client/compatible
Accept: application/json
###
GET {{Minecraft_Realms_Emulator_HostAddress}}/worlds
Accept: application/json
### ###

View File

@ -1,3 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
@ -7,6 +10,11 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<DataContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"));
});
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

View File

@ -1,4 +1,7 @@
{ {
"ConnectionStrings": {
"DefaultConnection": "User Id=postgres.aquqykciubzfolgrftwa;Password=8ypKY2pSWNRrlhCt;Server=aws-0-us-west-1.pooler.supabase.com;Port=5432;Database=postgres"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",