Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare season, next round api for admin #119

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
"env": {
"ASPNETCORE_ENVIRONMENT": "Local"
}
},
{
"name": "Debug ArenaService.Admin",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-admin",
"program": "${workspaceFolder}/ArenaService.Admin/bin/Debug/net8.0/ArenaService.Admin.dll",
"cwd": "${workspaceFolder}/ArenaService.Admin",
"stopAtEntry": false,
"env": {
"ASPNETCORE_ENVIRONMENT": "Local"
}
}
]
}
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"problemMatcher": "$msCompile"
},
{
"label": "build-worker",
"label": "build-admin",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ArenaService.Worker/ArenaService.Worker.csproj",
"${workspaceFolder}/ArenaService.Admin/ArenaService.Admin.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
Expand Down
23 changes: 23 additions & 0 deletions ArenaService.Admin/ArenaService.Admin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.24" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="7.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ArenaService.Shared\ArenaService.Shared.csproj" />
</ItemGroup>

</Project>
57 changes: 57 additions & 0 deletions ArenaService.Admin/Controllers/SeasonManagingController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace ArenaService.Controllers;

using ArenaService.Shared.Dtos;
using ArenaService.Shared.Exceptions;
using ArenaService.Shared.Repositories;
using ArenaService.Shared.Services;
using Libplanet.Crypto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;

[Route("manage-seasons")]
[ApiController]
public class SeasonManagingController : ControllerBase
{
private readonly ISeasonRepository _seasonRepo;
private readonly IRoundRepository _roundRepo;
private readonly ISeasonPreparationService _seasonPreparationService;
private readonly IRoundPreparationService _roundPreparationService;

public SeasonManagingController(
ISeasonRepository seasonRepo,
IRoundRepository roundRepo,
ISeasonPreparationService seasonPreparationService,
IRoundPreparationService roundPreparationService
)
{
_seasonRepo = seasonRepo;
_roundRepo = roundRepo;
_seasonPreparationService = seasonPreparationService;
_roundPreparationService = roundPreparationService;
}

[HttpPost("initialize-season")]
[SwaggerResponse(StatusCodes.Status200OK, "OK")]
public async Task<ActionResult> InitializeSeason(int seasonId)
{
var season = await _seasonRepo.GetSeasonAsync(seasonId, q => q.Include(s => s.Rounds));

await _seasonPreparationService.PrepareSeasonAsync(
(season, season.Rounds.OrderBy(r => r.StartBlock).First())
);

return Ok();
}

[HttpPost("prepare-next-round")]
[SwaggerResponse(StatusCodes.Status200OK, "Ok")]
public async Task<ActionResult> PrepareNextRound(int roundId)
{
var round = await _roundRepo.GetRoundAsync(roundId, q => q.Include(r => r.Season));

await _roundPreparationService.PrepareNextRoundWithSnapshotAsync((round!.Season, round));

return Ok();
}
}
9 changes: 9 additions & 0 deletions ArenaService.Admin/Options/RedisOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ArenaService.Admin.Options;

public class RedisOptions
{
public const string SectionName = "Redis";
public string Host { get; set; } = "127.0.0.1";
public string Port { get; set; } = "6379";
public int RankingDbNumber { get; set; } = 1;
}
13 changes: 13 additions & 0 deletions ArenaService.Admin/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ArenaService.Admin;

var builder = WebApplication.CreateBuilder(args);

var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);

var app = builder.Build();

var env = app.Services.GetRequiredService<IWebHostEnvironment>();
startup.Configure(app, env, app.Services);

app.Run();
23 changes: 23 additions & 0 deletions ArenaService.Admin/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5216",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7130;http://localhost:5216",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
123 changes: 123 additions & 0 deletions ArenaService.Admin/Setup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
namespace ArenaService.Admin;

using ArenaService.Admin.Options;
using ArenaService.Shared.Data;
using ArenaService.Shared.Repositories;
using ArenaService.Shared.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
using StackExchange.Redis;

public class Startup
{
public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.Configure<RedisOptions>(Configuration.GetSection(RedisOptions.SectionName));

services
.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});

services.AddDbContext<ArenaDbContext>(options =>
options
.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"))
.UseSnakeCaseNamingConvention()
);

services.AddSingleton<IConnectionMultiplexer>(provider =>
{
var redisOptions = provider.GetRequiredService<IOptions<RedisOptions>>().Value;
return ConnectionMultiplexer.Connect(
$"{redisOptions.Host}:{redisOptions.Port},defaultDatabase={redisOptions.RankingDbNumber}"
);
});

services.AddEndpointsApiExplorer();

services.AddSwaggerGen(options =>
{
options.SwaggerDoc(
"v1",
new OpenApiInfo { Title = "ArenaService API", Version = "v1" }
);

options.AddSecurityDefinition(
"BearerAuth",
new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description =
"Enter 'Bearer' followed by a space and the JWT. Example: 'Bearer your-token'"
}
);

options.EnableAnnotations();
});
services.AddSwaggerGenNewtonsoftSupport();

services.AddCors(options =>
{
options.AddPolicy(
"AllowAllOrigins",
builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()
);
});

services.AddScoped<ISeasonRepository, SeasonRepository>();
services.AddScoped<IRoundRepository, RoundRepository>();
services.AddScoped<IParticipantRepository, ParticipantRepository>();
services.AddScoped<IRankingSnapshotRepository, RankingSnapshotRepository>();
services.AddScoped<IAllClanRankingRepository, AllClanRankingRepository>();
services.AddScoped<IRankingRepository, RankingRepository>();
services.AddScoped<IClanRepository, ClanRepository>();
services.AddScoped<IClanRankingRepository, ClanRankingRepository>();
services.AddScoped<IMedalRepository, MedalRepository>();
services.AddScoped<ISeasonService, SeasonService>();
services.AddScoped<IRankingService, RankingService>();

services.AddScoped<ISeasonPreparationService, SeasonPreparationService>();
services.AddScoped<IRoundPreparationService, RoundPreparationService>();

services.AddHealthChecks();
}

public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IServiceProvider serviceProvider
)
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseCors("AllowAllOrigins");

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapSwagger();
endpoints.MapHealthChecks("/ping");
});
}
}
8 changes: 8 additions & 0 deletions ArenaService.Admin/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
12 changes: 12 additions & 0 deletions ArenaService.Admin/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=arena;Username=yourusername;Password=yourpassword"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
2 changes: 1 addition & 1 deletion ArenaService.IntegrationTests/Repositories/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Repositories;
using ArenaService.Shared.Repositories;
using StackExchange.Redis;

namespace ArenaService.IntegrationTests.Repositories;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ArenaService.Constants;
using ArenaService.Shared.Constants;
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Repositories;
using ArenaService.Shared.Repositories;
using Libplanet.Crypto;
using StackExchange.Redis;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ArenaService.Constants;
using ArenaService.Shared.Constants;
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Repositories;
using ArenaService.Shared.Repositories;
using Libplanet.Crypto;
using StackExchange.Redis;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ArenaService.Constants;
using ArenaService.Shared.Constants;
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Repositories;
using ArenaService.Shared.Repositories;
using Libplanet.Crypto;
using StackExchange.Redis;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Constants;
using ArenaService.Repositories;
using ArenaService.Shared.Constants;
using ArenaService.Shared.Repositories;
using Libplanet.Crypto;
using StackExchange.Redis;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Constants;
using ArenaService.Repositories;
using ArenaService.Shared.Constants;
using ArenaService.Shared.Repositories;
using Libplanet.Crypto;
using StackExchange.Redis;
using Xunit;
Expand Down
2 changes: 1 addition & 1 deletion ArenaService.IntegrationTests/Services/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Repositories;
using ArenaService.Shared.Repositories;
using StackExchange.Redis;

namespace ArenaService.IntegrationTests.Services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ArenaService.IntegrationTests.Fixtures;
using ArenaService.Repositories;
using ArenaService.Shared.Repositories;

namespace ArenaService.IntegrationTests.Services.RankingServiceTests;

Expand Down
6 changes: 6 additions & 0 deletions ArenaService.Shared/ArenaService.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

<ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="8.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Libplanet.Crypto" Version="5.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="7.2.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.24" />
<PackageReference Include="Libplanet.Types" Version="5.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ArenaService.Constants;
namespace ArenaService.Shared.Constants;

public static class ArenaServiceConfig
{
Expand Down
Loading
Loading