Skip to content

Commit

Permalink
Merge pull request #195 from SSchulze1989/feature/modify-raw-results
Browse files Browse the repository at this point in the history
Feature/modify raw results
  • Loading branch information
SSchulze1989 authored Jul 12, 2024
2 parents 5f169f8 + 4ff587e commit 9f18339
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 11 deletions.
17 changes: 17 additions & 0 deletions src/iRLeagueApiCore.Client/Endpoints/PutEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using iRLeagueApiCore.Client.Http;
using iRLeagueApiCore.Client.QueryBuilder;
using iRLeagueApiCore.Client.Results;

namespace iRLeagueApiCore.Client.Endpoints;

internal class PutEndpoint<TResult, TModel> : EndpointBase, IPutEndpoint<TResult, TModel> where TModel : notnull
{
public PutEndpoint(HttpClientWrapper httpClientWrapper, RouteBuilder routeBuilder) :
base(httpClientWrapper, routeBuilder)
{ }

async Task<ClientActionResult<TResult>> IPutEndpoint<TResult, TModel>.Put(TModel model, CancellationToken cancellationToken)
{
return await HttpClientWrapper.PutAsClientActionResult<TResult>(QueryUrl, model, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface IEventResultsEndpoint : IGetAllEndpoint<EventResultModel>, IDel
IPostEndpoint<bool, ParseSimSessionResult> Upload();
IPostEndpoint<bool> Calculate();
IFetchResultsEndpoint Fetch();
IGetEndpoint<RawEventResultModel> Raw();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using iRLeagueApiCore.Common.Models;

namespace iRLeagueApiCore.Client.Endpoints.Results;
public interface IRawResultsEndpoint
{
public IGetEndpoint<RawEventResultModel> EventResult(long eventId);
public IPutEndpoint<RawResultRowModel, RawResultRowModel> ModifyResultRow(long resultRowId, bool triggerCalculation = false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace iRLeagueApiCore.Client.Endpoints.Results;
public interface IResultsEndpoint : IWithIdEndpoint<IResultByIdEndpoint>
{
public IGetEndpoint<IEnumerable<EventResultModel>> Latest();
public IPutEndpoint<RawResultRowModel, RawResultRowModel> ModifyResultRow(long resultRowId, bool triggerCalculation = false);
}
13 changes: 13 additions & 0 deletions src/iRLeagueApiCore.Client/Endpoints/Results/RawResultsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using iRLeagueApiCore.Client.Http;
using iRLeagueApiCore.Client.QueryBuilder;
using iRLeagueApiCore.Common.Models;

namespace iRLeagueApiCore.Client.Endpoints.Results;

internal sealed class RawResultsEndpoint : GetEndpoint<RawEventResultModel>
{
public RawResultsEndpoint(HttpClientWrapper httpClient, RouteBuilder routeBuilder) : base(httpClient, routeBuilder)
{
RouteBuilder.AddEndpoint("Raw");
}
}
14 changes: 14 additions & 0 deletions src/iRLeagueApiCore.Client/Endpoints/Results/ResultsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public ResultsEndpoint(HttpClientWrapper httpClientWrapper, RouteBuilder routeBu
RouteBuilder.AddEndpoint("Results");
}

public IGetEndpoint<RawEventResultModel> Raw()
{
return new RawResultsEndpoint(HttpClientWrapper, RouteBuilder);
}

public IPutEndpoint<RawResultRowModel, RawResultRowModel> ModifyResultRow(long resultRowId, bool triggerCalculation = false)
{
var routeBuilder = RouteBuilder.Copy();
routeBuilder.AddEndpoint("ModResultRow");
routeBuilder.AddParameter(resultRowId);
routeBuilder.WithParameters(x => x.Add("triggerCaculation", triggerCalculation));
return new PutEndpoint<RawResultRowModel, RawResultRowModel>(HttpClientWrapper, routeBuilder);
}

public IPostEndpoint<bool, ParseSimSessionResult> Upload()
{
return new UploadResultEndpoint(HttpClientWrapper, RouteBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
using iRLeagueApiCore.Client.Service;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace Microsoft.Extensions.DependencyInjection;
public static class IRLeagueApiClientServiceExtensions
{
public static IServiceCollection AddLeagueApiClient(this IServiceCollection services, Action<LeagueApiClientConfiguration>? configure = null)
{
services.AddHttpClient();
services.TryAddScoped<HttpClientWrapperFactory>();

var jsonOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
jsonOptions.Converters.Add(new JsonStringEnumConverter());
jsonOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;

services.TryAddScoped<HttpClientWrapperFactory>(sp => new(
sp.GetRequiredService<ILoggerFactory>(),
jsonOptions
));
services.TryAddScoped<ITokenStore, DefaultTokenStore>();
services.TryAddScoped<IAsyncTokenProvider>(sp => sp.GetRequiredService<ITokenStore>());

// run configuration
var configuration = new LeagueApiClientConfiguration(services);
configure?.Invoke(configuration);
Expand Down
2 changes: 1 addition & 1 deletion src/iRLeagueApiCore.Client/iRLeagueApiCore.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<OutputType>Library</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
<PackageId>iRLeagueApiCore.Client</PackageId>
<Version>0.11.5-dev.1</Version>
<Version>0.11.5-dev.9</Version>
<Authors>Simon Schulze</Authors>
<Company>Simon Schulze</Company>
<PackageDescription>This package contains shared objects for all members of the iRLeagueDatabase-iRLeagueApi stack</PackageDescription>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace iRLeagueApiCore.Common.Models;

public sealed class RawEventResultModel
{
public long EventId { get; set; }
public IEnumerable<RawSessionResultModel> SessionResults { get; set; } = Array.Empty<RawSessionResultModel>();
}
96 changes: 96 additions & 0 deletions src/iRLeagueApiCore.Common/Models/Results/RawResultRowModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace iRLeagueApiCore.Common.Models;

[DataContract]
public sealed class RawResultRowModel
{
[DataMember]
public long ResultRowId { get; set; }
[DataMember]
public long MemberId { get; set; }
[DataMember]
public double StartPosition { get; set; }
[DataMember]
public double FinishPosition { get; set; }
[DataMember]
public string CarNumber { get; set; } = string.Empty;
[DataMember]
public int ClassId { get; set; }
[DataMember]
public string Car { get; set; } = string.Empty;
[DataMember]
public string CarClass { get; set; } = string.Empty;
[DataMember]
public double CompletedLaps { get; set; }
[DataMember]
public double LeadLaps { get; set; }
[DataMember]
public int FastLapNr { get; set; }
[DataMember]
public double Incidents { get; set; }
[DataMember]
public int Status { get; set; }
[DataMember]
public TimeSpan QualifyingTime { get; set; }
[DataMember]
public TimeSpan Interval { get; set; }
[DataMember]
public TimeSpan AvgLapTime { get; set; }
[DataMember]
public TimeSpan FastestLapTime { get; set; }
[DataMember]
public double PositionChange { get; set; }
[DataMember]
public string IRacingId { get; set; } = string.Empty;
[DataMember]
public int SimSessionType { get; set; }
[DataMember]
public int OldIRating { get; set; }
[DataMember]
public int NewIRating { get; set; }
[DataMember]
public int SeasonStartIRating { get; set; }
[DataMember]
public string License { get; set; } = string.Empty;
[DataMember]
public double OldSafetyRating { get; set; }
[DataMember]
public double NewSafetyRating { get; set; }
[DataMember]
public int OldCpi { get; set; }
[DataMember]
public int NewCpi { get; set; }
[DataMember]
public int ClubId { get; set; }
[DataMember]
public string ClubName { get; set; } = string.Empty;
[DataMember]
public int CarId { get; set; }
[DataMember]
public double CompletedPct { get; set; }
[DataMember]
public DateTime? QualifyingTimeAt { get; set; }
[DataMember]
public int Division { get; set; }
[DataMember]
public int OldLicenseLevel { get; set; }
[DataMember]
public int NewLicenseLevel { get; set; }
[DataMember]
public int NumPitStops { get; set; }
[DataMember]
public string PittedLaps { get; set; } = string.Empty;
[DataMember]
public int NumOfftrackLaps { get; set; }
[DataMember]
public string OfftrackLaps { get; set; } = string.Empty;
[DataMember]
public int NumContactLaps { get; set; }
[DataMember]
public string ContactLaps { get; set; } = string.Empty;
[DataMember]
public double RacePoints { get; set; }
[DataMember]
public long? TeamId { get; set; }
[DataMember]
public bool PointsEligible { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace iRLeagueApiCore.Common.Models;

public sealed class RawSessionResultModel
{
public long SessionId { get; set; }
public IEnumerable<RawResultRowModel> ResultRows { get; set; } = Array.Empty<RawResultRowModel>();
}
2 changes: 1 addition & 1 deletion src/iRLeagueApiCore.Common/iRLeagueApiCore.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PackageId>iRLeagueApiCore.Common</PackageId>
<Version>0.11.5-dev.1</Version>
<Version>0.11.5-dev.8</Version>
<Authors>Simon Schulze</Authors>
<Company>Simon Schulze</Company>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
23 changes: 22 additions & 1 deletion src/iRLeagueApiCore.Server/Controllers/ResultsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task<ActionResult<IEnumerable<SeasonEventResultModel>>> GetFromSeas
[HttpGet]
[AllowAnonymous]
[Route("/{leagueName}/Events/{eventId:long}/[controller]")]
public async Task<ActionResult<IEnumerable<EventResultModel>>> GetFromEvent([FromRoute] string leagueName, [FromRoute] long eventId,
public async Task<ActionResult<IEnumerable<EventResultModel>>> GetFromEvent([FromRoute] string leagueName, [FromRoute] long eventId,
CancellationToken cancellationToken = default)
{
var request = new GetResultsFromEventRequest(eventId);
Expand Down Expand Up @@ -157,4 +157,25 @@ public async Task<ActionResult<bool>> TriggerResultCalculation([FromRoute] strin
await mediator.Send(request, cancellationToken);
return Ok(true);
}

[HttpGet]
[RequireLeagueRole(LeagueRoles.Admin, LeagueRoles.Organizer)]
[Route("/{leagueName}/Events/{eventId:long}/Results/Raw")]
public async Task<ActionResult<RawEventResultModel>> GetRawEventResult([FromRoute] string leagueName, [FromRoute] long eventId, CancellationToken cancellationToken = default)
{
var request = new GetRawResultsFromEventRequest(eventId);
var result = await mediator.Send(request, cancellationToken);
return Ok(result);
}

[HttpPut]
[RequireLeagueRole(LeagueRoles.Admin, LeagueRoles.Organizer)]
[Route("ModResultRow/{resultRowId:long}")]
public async Task<ActionResult<RawResultRowModel>> ModifyResultRow([FromRoute] string leagueName, [FromRoute] long resultRowId, [FromBody] RawResultRowModel model,
[FromQuery] bool triggerCalculation = false, CancellationToken cancellationToken = default)
{
var request = new ModifyResultRowRequest(resultRowId, model, triggerCalculation);
var result = await mediator.Send(request, cancellationToken);
return Ok(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using iRLeagueApiCore.Common.Models;
using System.Linq.Expressions;

namespace iRLeagueApiCore.Server.Handlers.Results;

public record GetRawResultsFromEventRequest(long EventId) : IRequest<RawEventResultModel>;

public class GetRawResultsFromEventHandler : ResultHandlerBase<GetRawResultsFromEventHandler, GetRawResultsFromEventRequest, RawEventResultModel>
{
public GetRawResultsFromEventHandler(ILogger<GetRawResultsFromEventHandler> logger, LeagueDbContext dbContext,
IEnumerable<IValidator<GetRawResultsFromEventRequest>> validators) : base(logger, dbContext, validators)
{
}

public override async Task<RawEventResultModel> Handle(GetRawResultsFromEventRequest request, CancellationToken cancellationToken)
{
await validators.ValidateAllAndThrowAsync(request, cancellationToken);
var result = await dbContext.EventResults
.Where(x => x.EventId == request.EventId)
.Select(MapToRawEventResultModelExpression)
.FirstOrDefaultAsync(cancellationToken)
?? throw new ResourceNotFoundException();
return result;
}

private Expression<Func<EventResultEntity, RawEventResultModel>> MapToRawEventResultModelExpression => result => new()
{
EventId = result.EventId,
SessionResults = result.SessionResults.Select(sessionResult => new RawSessionResultModel()
{
SessionId = sessionResult.SessionId,
ResultRows = sessionResult.ResultRows.Select(row => new RawResultRowModel()
{
ResultRowId = row.ResultRowId,
MemberId = row.MemberId,
StartPosition = row.StartPosition,
FinishPosition = row.FinishPosition,
CarNumber = row.CarNumber,
ClassId = row.ClassId,
Car = row.Car,
CarClass = row.CarClass,
CompletedLaps = row.CompletedLaps,
LeadLaps = row.LeadLaps,
FastLapNr = row.FastLapNr,
Incidents = row.Incidents,
Status = row.Status,
QualifyingTime = row.QualifyingTime,
Interval = row.Interval,
AvgLapTime = row.AvgLapTime,
FastestLapTime = row.FastestLapTime,
PositionChange = row.PositionChange,
IRacingId = row.IRacingId,
SimSessionType = row.SimSessionType,
OldIRating = row.OldIRating,
NewIRating = row.NewIRating,
SeasonStartIRating = row.SeasonStartIRating,
License = row.License,
OldSafetyRating = row.OldSafetyRating,
NewSafetyRating = row.NewSafetyRating,
OldCpi = row.OldCpi,
NewCpi = row.NewCpi,
ClubId = row.ClubId,
ClubName = row.ClubName,
CarId = row.CarId,
CompletedPct = row.CompletedPct,
QualifyingTimeAt = row.QualifyingTimeAt,
Division = row.Division,
OldLicenseLevel = row.OldLicenseLevel,
NewLicenseLevel = row.NewLicenseLevel,
NumPitStops = row.NumPitStops,
PittedLaps = row.PittedLaps,
NumOfftrackLaps = row.NumOfftrackLaps,
OfftrackLaps = row.OfftrackLaps,
NumContactLaps = row.NumContactLaps,
ContactLaps = row.ContactLaps,
RacePoints = row.RacePoints,
TeamId = row.TeamId,
PointsEligible = row.PointsEligible,
}).ToList(),
}).ToList(),
};
}
Loading

0 comments on commit 9f18339

Please sign in to comment.