Add player settings feature and settings API
This commit is contained in:
@@ -21,9 +21,9 @@ public class ProfilesController(IProfileService profileService) : ControllerBase
|
||||
return profile;
|
||||
}
|
||||
|
||||
[HttpGet("bulk")]
|
||||
[HttpPost("bulk")]
|
||||
public async Task<IReadOnlyList<ProfileDTO>> GetBulkProfiles(
|
||||
[FromQuery(Name = "id")] List<Guid> ids,
|
||||
[FromBody] List<Guid> ids,
|
||||
CancellationToken ct)
|
||||
=> await profileService.GetProfilesAsync(ids, ct);
|
||||
|
||||
|
||||
40
src/API/Controllers/Settings/V1/SettingsController.cs
Normal file
40
src/API/Controllers/Settings/V1/SettingsController.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RecNet.Application.Common.Security;
|
||||
using RecNet.Application.Profiles;
|
||||
|
||||
namespace API.Controllers.Settings.V1;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("api/[controller]/v1")]
|
||||
public class SettingsController(IProfileService profileService) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IReadOnlyList<PlayerSettingDTO>> GetAllAsync(CancellationToken ct)
|
||||
{
|
||||
var profileId = User.GetProfileId();
|
||||
|
||||
return await profileService.GetPlayerSettingsAsync(profileId, ct);
|
||||
}
|
||||
|
||||
[HttpPost("set")]
|
||||
public async Task<IActionResult> SetAsync(
|
||||
[FromBody] PlayerSettingDTO request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var profileId = User.GetProfileId();
|
||||
|
||||
var updated = await profileService.UpdatePlayerSettingAsync
|
||||
(
|
||||
profileId: profileId,
|
||||
command: new UpdatePlayerSettingCommand(request.Key, request.Value),
|
||||
ct: ct
|
||||
);
|
||||
|
||||
if (!updated)
|
||||
return NotFound();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
@@ -9,5 +9,6 @@ public class ApplicationMappingProfile : Profile
|
||||
{
|
||||
CreateMap<Domain.Profiles.Profile, ProfileDTO>();
|
||||
CreateMap<Domain.Profiles.Avatar, AvatarDTO>();
|
||||
CreateMap<Domain.Profiles.PlayerSetting, PlayerSettingDTO>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
namespace RecNet.Application.Profiles;
|
||||
|
||||
public interface IProfileService
|
||||
@@ -5,6 +6,8 @@ public interface IProfileService
|
||||
Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default);
|
||||
Task<AvatarDTO?> GetAvatarAsync(Guid profileId, CancellationToken ct = default);
|
||||
Task<AvatarDTO?> UpdateAvatarAsync(Guid profileId, UpdateAvatarCommand command, CancellationToken ct = default);
|
||||
Task<IReadOnlyList<PlayerSettingDTO>> GetPlayerSettingsAsync(Guid profileId, CancellationToken ct = default);
|
||||
Task<bool> UpdatePlayerSettingAsync(Guid profileId, UpdatePlayerSettingCommand command, CancellationToken ct = default);
|
||||
Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(List<Guid> profileIds, CancellationToken ct = default);
|
||||
Task<LoginProfileResult> LoginAsync(LoginProfileCommand command, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
12
src/RecNet.Application/Profiles/PlayerSettingDto.cs
Normal file
12
src/RecNet.Application/Profiles/PlayerSettingDto.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace RecNet.Application.Profiles;
|
||||
|
||||
public class PlayerSettingDTO
|
||||
{
|
||||
[JsonPropertyName("Key")]
|
||||
public required string Key { get; set; }
|
||||
|
||||
[JsonPropertyName("Value")]
|
||||
public required string Value { get; set; }
|
||||
}
|
||||
@@ -55,6 +55,31 @@ public class ProfileService(
|
||||
return mapper.Map<AvatarDTO>(profile.Avatar);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<PlayerSettingDTO>> GetPlayerSettingsAsync(
|
||||
Guid profileId,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var settings = await profileRepository.GetSettingsByProfileIdAsync(profileId, ct);
|
||||
|
||||
return mapper.Map<List<PlayerSettingDTO>>(settings);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdatePlayerSettingAsync(
|
||||
Guid profileId,
|
||||
UpdatePlayerSettingCommand command,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var profile = await profileRepository.GetByIdWithSettingsAsync(profileId, ct);
|
||||
if (profile is null)
|
||||
return false;
|
||||
|
||||
profile.SetSetting(command.Key, command.Value);
|
||||
|
||||
await profileRepository.SaveChangesAsync(ct);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(
|
||||
List<Guid> profileIds,
|
||||
CancellationToken ct = default)
|
||||
@@ -123,4 +148,4 @@ public class ProfileService(
|
||||
expiresIn: token.ExpiresIn
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace RecNet.Application.Profiles;
|
||||
|
||||
public sealed record UpdatePlayerSettingCommand(
|
||||
string Key,
|
||||
string Value);
|
||||
@@ -5,7 +5,9 @@ namespace RecNet.Domain.Profiles;
|
||||
public interface IProfileRepository
|
||||
{
|
||||
Task<Profile?> GetByIdAsync(Guid profileId, CancellationToken ct = default);
|
||||
Task<Profile?> GetByIdWithSettingsAsync(Guid profileId, CancellationToken ct = default);
|
||||
Task<Avatar?> GetAvatarByProfileIdAsync(Guid profileId, CancellationToken ct = default);
|
||||
Task<IReadOnlyList<PlayerSetting>> GetSettingsByProfileIdAsync(Guid profileId, CancellationToken ct = default);
|
||||
Task<IReadOnlyList<Profile>> GetByIdsAsync(List<Guid> profileIds, CancellationToken ct = default);
|
||||
|
||||
Task<Profile?> GetByPlatform(PlatformType platform, string platformId, CancellationToken ct = default);
|
||||
|
||||
20
src/RecNet.Domain/Profiles/PlayerSetting.cs
Normal file
20
src/RecNet.Domain/Profiles/PlayerSetting.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace RecNet.Domain.Profiles;
|
||||
|
||||
public class PlayerSetting
|
||||
{
|
||||
public Guid UserId { get; private set; }
|
||||
public string Key { get; private set; }
|
||||
public string Value { get; private set; }
|
||||
|
||||
internal PlayerSetting(Guid userId, string key, string value)
|
||||
{
|
||||
UserId = userId;
|
||||
Key = key;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public void UpdateValue(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ public class Profile
|
||||
public DateTimeOffset CreatedAt { get; private set; }
|
||||
|
||||
public Avatar Avatar { get; private set; } = Avatar.Empty;
|
||||
public ICollection<PlayerSetting> Settings { get; } = [];
|
||||
|
||||
public static Profile Create(
|
||||
string name,
|
||||
@@ -86,6 +87,19 @@ public class Profile
|
||||
public void SetAvatar(Avatar avatar)
|
||||
=> Avatar = avatar ?? throw new DomainException("Avatar is required.");
|
||||
|
||||
public void SetSetting(string key, string value)
|
||||
{
|
||||
var existing = Settings.FirstOrDefault(x => x.Key == key);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
Settings.Add(new PlayerSetting(ProfileId, key, value));
|
||||
return;
|
||||
}
|
||||
|
||||
existing.UpdateValue(value);
|
||||
}
|
||||
|
||||
private static string RequireValue(string value, string parameterName)
|
||||
=> !string.IsNullOrWhiteSpace(value) ? value : throw new DomainException($"{parameterName} is required.");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using RecNet.Domain.Profiles;
|
||||
|
||||
namespace RecNet.Infrastructure.Persistence.Configurations;
|
||||
|
||||
public class PlayerSettingConfiguration : IEntityTypeConfiguration<PlayerSetting>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PlayerSetting> builder)
|
||||
{
|
||||
builder.HasKey(x => new { x.UserId, x.Key });
|
||||
|
||||
builder.Property(x => x.Key)
|
||||
.HasMaxLength(100)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Value)
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,13 @@ public class ProfileConfiguration : IEntityTypeConfiguration<Profile>
|
||||
|
||||
builder.Property(x => x.CreatedAt)
|
||||
.IsRequired();
|
||||
|
||||
builder.HasMany(x => x.Settings)
|
||||
.WithOne()
|
||||
.HasForeignKey(x => x.UserId);
|
||||
|
||||
builder.Navigation(x => x.Settings)
|
||||
.UsePropertyAccessMode(PropertyAccessMode.Field);
|
||||
|
||||
builder.OwnsOne(x => x.Avatar);
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace RecNet.Infrastructure.Persistence;
|
||||
public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<Profile> Profiles => Set<Profile>();
|
||||
public DbSet<PlayerSetting> PlayerSettings => Set<PlayerSetting>();
|
||||
public DbSet<GameVersion> GameVersions => Set<GameVersion>();
|
||||
public DbSet<ServerConfig> ServerConfigs => Set<ServerConfig>();
|
||||
|
||||
|
||||
164
src/RecNet.Infrastructure/Persistence/Migrations/20260621035308_PlayerSettings.Designer.cs
generated
Normal file
164
src/RecNet.Infrastructure/Persistence/Migrations/20260621035308_PlayerSettings.Designer.cs
generated
Normal file
@@ -0,0 +1,164 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using RecNet.Infrastructure.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace RecNet.Infrastructure.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(DatabaseContext))]
|
||||
[Migration("20260621035308_PlayerSettings")]
|
||||
partial class PlayerSettings
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "10.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Configuration.ServerConfig", b =>
|
||||
{
|
||||
b.Property<string>("Key")
|
||||
.HasMaxLength(128)
|
||||
.HasColumnType("character varying(128)");
|
||||
|
||||
b.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasColumnType("jsonb");
|
||||
|
||||
b.HasKey("Key");
|
||||
|
||||
b.ToTable("ServerConfigs");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.GameVersions.GameVersion", b =>
|
||||
{
|
||||
b.Property<string>("Version")
|
||||
.HasMaxLength(32)
|
||||
.HasColumnType("character varying(32)");
|
||||
|
||||
b.Property<bool>("IsValid")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.HasKey("Version");
|
||||
|
||||
b.ToTable("GameVersions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.PlayerSetting", b =>
|
||||
{
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Key")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("character varying(100)");
|
||||
|
||||
b.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("UserId", "Key");
|
||||
|
||||
b.ToTable("PlayerSettings");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.Profile", b =>
|
||||
{
|
||||
b.Property<Guid>("ProfileId")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.PrimitiveCollection<string>("DeviceIds")
|
||||
.IsRequired()
|
||||
.HasColumnType("jsonb");
|
||||
|
||||
b.Property<bool>("IsBanned")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<bool>("IsModerator")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(32)
|
||||
.HasColumnType("character varying(32)");
|
||||
|
||||
b.Property<string>("Platform")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.Property<string>("PlatformId")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.HasKey("ProfileId");
|
||||
|
||||
b.HasIndex("Platform", "PlatformId")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Profiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.PlayerSetting", b =>
|
||||
{
|
||||
b.HasOne("RecNet.Domain.Profiles.Profile", null)
|
||||
.WithMany("Settings")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.Profile", b =>
|
||||
{
|
||||
b.OwnsOne("RecNet.Domain.Profiles.Avatar", "Avatar", b1 =>
|
||||
{
|
||||
b1.Property<Guid>("ProfileId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b1.Property<string>("HairColor")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b1.Property<string>("OutfitSelections")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b1.Property<string>("SkinColor")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b1.HasKey("ProfileId");
|
||||
|
||||
b1.ToTable("Profiles");
|
||||
|
||||
b1.WithOwner()
|
||||
.HasForeignKey("ProfileId");
|
||||
});
|
||||
|
||||
b.Navigation("Avatar")
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.Profile", b =>
|
||||
{
|
||||
b.Navigation("Settings");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace RecNet.Infrastructure.Persistence.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class PlayerSettings : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "PlayerSettings",
|
||||
columns: table => new
|
||||
{
|
||||
UserId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Key = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
Value = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_PlayerSettings", x => new { x.UserId, x.Key });
|
||||
table.ForeignKey(
|
||||
name: "FK_PlayerSettings_Profiles_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Profiles",
|
||||
principalColumn: "ProfileId",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "PlayerSettings");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,24 @@ namespace RecNet.Infrastructure.Persistence.Migrations
|
||||
b.ToTable("GameVersions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.PlayerSetting", b =>
|
||||
{
|
||||
b.Property<Guid>("UserId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Key")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("character varying(100)");
|
||||
|
||||
b.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("UserId", "Key");
|
||||
|
||||
b.ToTable("PlayerSettings");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.Profile", b =>
|
||||
{
|
||||
b.Property<Guid>("ProfileId")
|
||||
@@ -93,6 +111,15 @@ namespace RecNet.Infrastructure.Persistence.Migrations
|
||||
b.ToTable("Profiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.PlayerSetting", b =>
|
||||
{
|
||||
b.HasOne("RecNet.Domain.Profiles.Profile", null)
|
||||
.WithMany("Settings")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.Profile", b =>
|
||||
{
|
||||
b.OwnsOne("RecNet.Domain.Profiles.Avatar", "Avatar", b1 =>
|
||||
@@ -123,6 +150,11 @@ namespace RecNet.Infrastructure.Persistence.Migrations
|
||||
b.Navigation("Avatar")
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("RecNet.Domain.Profiles.Profile", b =>
|
||||
{
|
||||
b.Navigation("Settings");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,13 @@ public class ProfileRepository(DatabaseContext dbContext) : IProfileRepository
|
||||
=> dbContext.Profiles
|
||||
.FirstOrDefaultAsync(x => x.ProfileId == profileId, ct);
|
||||
|
||||
public Task<Profile?> GetByIdWithSettingsAsync(
|
||||
Guid profileId,
|
||||
CancellationToken ct = default)
|
||||
=> dbContext.Profiles
|
||||
.Include(x => x.Settings)
|
||||
.FirstOrDefaultAsync(x => x.ProfileId == profileId, ct);
|
||||
|
||||
public Task<Avatar?> GetAvatarByProfileIdAsync(
|
||||
Guid profileId,
|
||||
CancellationToken ct = default)
|
||||
@@ -21,6 +28,14 @@ public class ProfileRepository(DatabaseContext dbContext) : IProfileRepository
|
||||
.Select(x => x.Avatar)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
public async Task<IReadOnlyList<PlayerSetting>> GetSettingsByProfileIdAsync(
|
||||
Guid profileId,
|
||||
CancellationToken ct = default)
|
||||
=> await dbContext.PlayerSettings
|
||||
.AsNoTracking()
|
||||
.Where(x => x.UserId == profileId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<IReadOnlyList<Profile>> GetByIdsAsync(
|
||||
List<Guid> profileIds,
|
||||
CancellationToken ct = default)
|
||||
|
||||
Reference in New Issue
Block a user