Skip to content

Commit

Permalink
feat(account)!: switch to Bohemia id #96
Browse files Browse the repository at this point in the history
- temporary create account if not found on request, later to be replaced, see #96
  • Loading branch information
kwinkel committed Oct 6, 2023
1 parent 096fd82 commit edbcec7
Show file tree
Hide file tree
Showing 10 changed files with 638 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Api/Accounts/AccountEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static WebApplication MapAccountEndpoints(this WebApplication app)
.MapPost(
"/sessions",
async ([FromBody] SessionRequestDto sessionRequest, IMediator mediator, IMapper mapper, CancellationToken cancellationToken)
=> Results.Ok(mapper.Map<ResultDto<SessionDto>>(await mediator.Send(new CreateSessionRequest(sessionRequest.SteamId), cancellationToken))))
=> Results.Ok(mapper.Map<ResultDto<SessionDto>>(await mediator.Send(new CreateSessionRequest(sessionRequest.BohemiaId), cancellationToken))))
.Produces<ResultDto<SessionDto>>();

return app;
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Accounts/CreateSessionRequestDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public class SessionRequestDto
{
public long SteamId { get; init; }
public Guid BohemiaId { get; init; }
}
2 changes: 2 additions & 0 deletions src/Api/Accounts/SessionDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public class SessionDto : SessionRequestDto
{
public Guid AccountId { get; init; }

public bool Locked { get; init; }
}
4 changes: 3 additions & 1 deletion src/Api/Accounts/SessionProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ELifeRPG.Application.Sessions;
using ELifeRPG.Core.Api.Characters;
using ELifeRPG.Core.Api.Models;
using ELifeRPG.Domain.Accounts;

namespace ELifeRPG.Core.Api.Accounts;

Expand All @@ -11,7 +12,8 @@ public class SessionProfile : Profile
public SessionProfile()
{
CreateMap<CreateSessionResult, SessionDto>()
.ForMember(d => d.AccountId, o => o.MapFrom(s => s.AccountId));
.ForMember(d => d.AccountId, o => o.MapFrom(s => s.AccountId))
.ForMember(d => d.Locked, o => o.MapFrom(s => s.AccountStatus == AccountStatus.Locked));

CreateMap<CreateSessionResult, ResultDto<SessionDto>>()
.ForMember(d => d.Data, o => o.MapFrom(s => s));
Expand Down
24 changes: 13 additions & 11 deletions src/Application/Sessions/CreateSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ namespace ELifeRPG.Application.Sessions;

public class CreateSessionResult : AbstractResult
{
public CreateSessionResult(Guid accountId)
{
AccountId = accountId;
}
public required Guid AccountId { get; init; }

public Guid AccountId { get; }
public required AccountStatus AccountStatus { get; init; }
}

public class CreateSessionRequest : IRequest<CreateSessionResult>
{
public CreateSessionRequest(long steamId)
public CreateSessionRequest(Guid bohemiaId)
{
SteamId = steamId;
BohemiaId = bohemiaId;
}

public long SteamId { get; }
public Guid BohemiaId { get; }
}

public class CreateSessionHandler : IRequestHandler<CreateSessionRequest, CreateSessionResult>
Expand All @@ -37,16 +34,21 @@ public CreateSessionHandler(IDatabaseContext databaseContext)
public async Task<CreateSessionResult> Handle(CreateSessionRequest request, CancellationToken cancellationToken)
{
var account = await _databaseContext.Accounts
.SingleOrDefaultAsync(x => x.SteamId == request.SteamId, cancellationToken);
.SingleOrDefaultAsync(x => x.BohemiaId == request.BohemiaId, cancellationToken);

if (account is null)
{
account = new Account(request.SteamId);
// temporary until ingame account linking is finished, then blocking this case -> https://github.com/ELifeRPG/Core/issues/96
account = new Account(request.BohemiaId);
_databaseContext.Accounts.Add(account);
await _databaseContext.SaveChangesAsync(cancellationToken);
}

var response = new CreateSessionResult(account.Id);
var response = new CreateSessionResult
{
AccountId = account.Id,
AccountStatus = account.Status,
};

if (account.Status == AccountStatus.Locked)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Domain/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ public Account(long steamId)
DomainEvents.Add(new AccountCreatedEvent(this));
}

public Account(Guid bohemiaId)
{
Id = Guid.NewGuid();
BohemiaId = bohemiaId;

DomainEvents.Add(new AccountCreatedEvent(this));
}

public Guid Id { get; init; }

public long SteamId { get; init; }

public Guid? BohemiaId { get; init; }

public AccountStatus Status
{
Expand Down
3 changes: 3 additions & 0 deletions src/Infrastructure/Accounts/AccountTypeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public void Configure(EntityTypeBuilder<Account> builder)
builder.Property(x => x.SteamId).HasColumnName("SteamId").IsRequired();
builder.HasIndex(x => x.SteamId).HasDatabaseName("IDX_Account_SteamId");

builder.Property(x => x.BohemiaId).HasColumnName("BohemiaId");
builder.HasIndex(x => x.BohemiaId).HasDatabaseName("IDX_Account_BohemiaId");

builder.Property(x => x.Status).HasColumnName("Status").IsRequired();
}
}
Loading

0 comments on commit edbcec7

Please sign in to comment.