feat: subscriptions

This commit is contained in:
CyberL1 2024-02-03 14:27:05 +01:00
parent 4e737305dd
commit b84ddbceee
9 changed files with 278 additions and 2 deletions

View File

@ -0,0 +1,31 @@
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 SubscriptionsController : ControllerBase
{
private readonly DataContext _context;
public SubscriptionsController(DataContext context)
{
_context = context;
}
[HttpGet("{id}")]
public async Task<ActionResult<Subscription>> Get(int id)
{
var world = await _context.Worlds.FindAsync(id);
var subscriptions = await _context.Subscriptions.ToListAsync();
if (world == null) return NotFound("Subscription njot found");
var subscription = subscriptions.Find(s => s.RemoteId == world.RemoteSubscriptionId);
return Ok(subscription);
}
}
}

View File

@ -53,8 +53,8 @@ namespace Minecraft_Realms_Emulator.Controllers
};
worlds.Add(world);
_context.Worlds.Add(world);
_context.SaveChanges();
}
@ -90,7 +90,17 @@ namespace Minecraft_Realms_Emulator.Controllers
world.Motd = body.Description;
world.State = State.OPEN.ToString();
var subscription = new Subscription
{
RemoteId = world.RemoteSubscriptionId,
StartDate = ((DateTimeOffset) DateTime.Now).ToUnixTimeMilliseconds().ToString(),
DaysLeft = 30,
SubscriptionType = SubscriptionType.NORMAL.ToString()
};
_context.Worlds.Update(world);
_context.Subscriptions.Add(subscription);
_context.SaveChanges();
return Ok(world);

View File

@ -6,5 +6,6 @@ namespace Minecraft_Realms_Emulator.Data
public class DataContext(DbContextOptions<DataContext> options) : DbContext(options)
{
public DbSet<World> Worlds { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
}
}

View File

@ -0,0 +1,11 @@
namespace Minecraft_Realms_Emulator.Entities
{
public class Subscription
{
public int Id { get; set; }
public string RemoteId { get; set; } = Guid.NewGuid().ToString();
public string StartDate { get; set; } = ((DateTimeOffset) DateTime.Now).ToUnixTimeMilliseconds().ToString();
public int DaysLeft { get; set; } = 30;
public string SubscriptionType { get; set; } = "NORMAL";
}
}

View File

@ -0,0 +1,8 @@
namespace Minecraft_Realms_Emulator.Entities
{
public enum SubscriptionType
{
NORMAL,
RECURRING
}
}

View File

@ -3,7 +3,7 @@
public class World
{
public int Id { get; set; }
public string? RemoteSubscriptionId { get; set; }
public string RemoteSubscriptionId { get; set; } = Guid.NewGuid().ToString();
public string? Owner { get; set; }
public string? OwnerUUID { get; set; }
public string? Name { get; set; }

View File

@ -0,0 +1,130 @@
// <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("20240203132410_Subscriptions")]
partial class Subscriptions
{
/// <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.Subscription", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("DaysLeft")
.HasColumnType("integer");
b.Property<string>("RemoteId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("StartDate")
.IsRequired()
.HasColumnType("text");
b.Property<string>("SubscriptionType")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
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[]>("Players")
.IsRequired()
.HasColumnType("text[]");
b.Property<string>("RemoteSubscriptionId")
.IsRequired()
.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,56 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class Subscriptions : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "RemoteSubscriptionId",
table: "Worlds",
type: "text",
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "text",
oldNullable: true);
migrationBuilder.CreateTable(
name: "Subscriptions",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RemoteId = table.Column<string>(type: "text", nullable: false),
StartDate = table.Column<string>(type: "text", nullable: false),
DaysLeft = table.Column<int>(type: "integer", nullable: false),
SubscriptionType = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Subscriptions", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Subscriptions");
migrationBuilder.AlterColumn<string>(
name: "RemoteSubscriptionId",
table: "Worlds",
type: "text",
nullable: true,
oldClrType: typeof(string),
oldType: "text");
}
}
}

View File

@ -22,6 +22,34 @@ namespace Minecraft_Realms_Emulator.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("DaysLeft")
.HasColumnType("integer");
b.Property<string>("RemoteId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("StartDate")
.IsRequired()
.HasColumnType("text");
b.Property<string>("SubscriptionType")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Subscriptions");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Property<int>("Id")
@ -74,6 +102,7 @@ namespace Minecraft_Realms_Emulator.Migrations
.HasColumnType("text[]");
b.Property<string>("RemoteSubscriptionId")
.IsRequired()
.HasColumnType("text");
b.Property<string[]>("Slots")