Replace custom socket implementation with SignalR.
Replace MSAL and custom cookie auth with Microsoft.Identity.EntityFramework Also some UI redesign to accommodate different login experience.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
namespace Shogi.Api.Repositories.Dto;
|
||||
|
||||
public readonly record struct SessionDto(string Name, string Player1, string Player2)
|
||||
public readonly record struct SessionDto(string Id, string Player1Id, string Player2Id)
|
||||
{
|
||||
}
|
||||
|
||||
54
Shogi.Api/Repositories/EmailSender.cs
Normal file
54
Shogi.Api/Repositories/EmailSender.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Shogi.Api.Repositories;
|
||||
|
||||
// https://app-smtp.brevo.com/real-time
|
||||
|
||||
public class EmailSender : IEmailSender
|
||||
{
|
||||
private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web);
|
||||
private readonly HttpClient client;
|
||||
private string apiKey;
|
||||
|
||||
|
||||
public EmailSender(HttpClient client, IOptionsMonitor<ApiKeys> apiKeys)
|
||||
{
|
||||
this.apiKey = apiKeys.CurrentValue.BrevoEmailService;
|
||||
apiKeys.OnChange(keys => this.apiKey = keys.BrevoEmailService);
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
|
||||
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
|
||||
{
|
||||
var body = new
|
||||
{
|
||||
Sender = new
|
||||
{
|
||||
Name = "Shogi Account Support",
|
||||
Email = "shogi@lucaserver.space",
|
||||
},
|
||||
To = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
Name = email,
|
||||
Email = email,
|
||||
}
|
||||
},
|
||||
Subject = subject,
|
||||
HtmlContent = htmlMessage,
|
||||
};
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://api.brevo.com/v3/smtp/email", UriKind.Absolute));
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
request.Headers.Add("api-key", apiKey);
|
||||
request.Content = JsonContent.Create(body, options: Options);
|
||||
|
||||
var response = await this.client.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,24 @@
|
||||
using Dapper;
|
||||
using Shogi.Contracts.Api;
|
||||
using Shogi.Contracts.Types;
|
||||
using Shogi.Api.Repositories.Dto;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace Shogi.Api.Repositories;
|
||||
|
||||
public class QueryRepository : IQueryRespository
|
||||
public class QueryRepository(IConfiguration configuration)
|
||||
{
|
||||
private readonly string connectionString;
|
||||
private readonly string connectionString = configuration.GetConnectionString("ShogiDatabase")
|
||||
?? throw new InvalidOperationException("No database configured for QueryRepository.");
|
||||
|
||||
public QueryRepository(IConfiguration configuration)
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString("ShogiDatabase") ?? throw new InvalidOperationException("No database configured for QueryRepository.");
|
||||
this.connectionString = connectionString;
|
||||
}
|
||||
public async Task<IEnumerable<SessionDto>> ReadSessionsMetadata(string playerId)
|
||||
{
|
||||
using var connection = new SqlConnection(this.connectionString);
|
||||
|
||||
public async Task<ReadSessionsPlayerCountResponse> ReadSessionPlayerCount(string playerName)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
var results = await connection.QueryMultipleAsync(
|
||||
"session.ReadSessionsMetadata",
|
||||
new { PlayerId = playerId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
var results = await connection.QueryMultipleAsync(
|
||||
"session.ReadSessionPlayerCount",
|
||||
new { PlayerName = playerName },
|
||||
commandType: System.Data.CommandType.StoredProcedure);
|
||||
|
||||
var joinedSessions = await results.ReadAsync<SessionMetadata>();
|
||||
var otherSessions = await results.ReadAsync<SessionMetadata>();
|
||||
return new ReadSessionsPlayerCountResponse
|
||||
{
|
||||
PlayerHasJoinedSessions = joinedSessions.ToList(),
|
||||
AllOtherSessions = otherSessions.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="ValueTuple"/> with Item1 as player 1 and Item2 as player 2.</returns>
|
||||
public async Task<(User Player1, User? Player2)?> GetUsersForSession(string sessionName)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
var results = await connection.QueryAsync<(string Player1Name, string Player1DisplayName, string Player2Name, string Player2DisplayName)>(
|
||||
"session.ReadUsersBySession",
|
||||
new { SessionName = sessionName },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
if (results.Any())
|
||||
{
|
||||
var (Player1Name, Player1DisplayName, Player2Name, Player2DisplayName) = results.First();
|
||||
var p1 = new User(Player1Name, Player1DisplayName);
|
||||
var p2 = Player2Name != null
|
||||
? new User(Player2Name, Player2DisplayName)
|
||||
: null;
|
||||
return (p1, p2);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return await results.ReadAsync<SessionDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IQueryRespository
|
||||
{
|
||||
Task<(User Player1, User? Player2)?> GetUsersForSession(string sessionName);
|
||||
Task<ReadSessionsPlayerCountResponse> ReadSessionPlayerCount(string playerName);
|
||||
}
|
||||
@@ -1,120 +1,84 @@
|
||||
using Dapper;
|
||||
using Shogi.Api.Repositories.Dto;
|
||||
using Shogi.Contracts.Api;
|
||||
using Shogi.Domain;
|
||||
using Shogi.Domain.Aggregates;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace Shogi.Api.Repositories;
|
||||
|
||||
public class SessionRepository : ISessionRepository
|
||||
public class SessionRepository(IConfiguration configuration)
|
||||
{
|
||||
private readonly string connectionString;
|
||||
private readonly string connectionString = configuration.GetConnectionString("ShogiDatabase")
|
||||
?? throw new InvalidOperationException("Database connection string not configured.");
|
||||
|
||||
public SessionRepository(IConfiguration configuration)
|
||||
{
|
||||
connectionString = configuration.GetConnectionString("ShogiDatabase") ?? throw new InvalidOperationException("Database connection string not configured.");
|
||||
}
|
||||
public async Task CreateSession(Session session)
|
||||
{
|
||||
using var connection = new SqlConnection(this.connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.CreateSession",
|
||||
new
|
||||
{
|
||||
session.Id,
|
||||
Player1Id = session.Player1,
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task CreateSession(Session session)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.CreateSession",
|
||||
new
|
||||
{
|
||||
session.Name,
|
||||
Player1Name = session.Player1,
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
public async Task DeleteSession(string id)
|
||||
{
|
||||
using var connection = new SqlConnection(this.connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.DeleteSession",
|
||||
new { Id = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task DeleteSession(string name)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.DeleteSession",
|
||||
new { Name = name },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
public async Task<(SessionDto? Session, IEnumerable<MoveDto> Moves)> ReadSessionAndMoves(string id)
|
||||
{
|
||||
using var connection = new SqlConnection(this.connectionString);
|
||||
var results = await connection.QueryMultipleAsync(
|
||||
"session.ReadSession",
|
||||
new { Id = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
public async Task<Session?> ReadSession(string name)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
var results = await connection.QueryMultipleAsync(
|
||||
"session.ReadSession",
|
||||
new { Name = name },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
var sessionDtos = await results.ReadAsync<SessionDto>();
|
||||
if (!sessionDtos.Any())
|
||||
{
|
||||
return (null, []);
|
||||
}
|
||||
|
||||
var sessionDtos = await results.ReadAsync<SessionDto>();
|
||||
if (!sessionDtos.Any()) return null;
|
||||
var dto = sessionDtos.First();
|
||||
var session = new Session(dto.Name, dto.Player1);
|
||||
if (!string.IsNullOrWhiteSpace(dto.Player2)) session.AddPlayer2(dto.Player2);
|
||||
var moveDtos = await results.ReadAsync<MoveDto>();
|
||||
|
||||
var moveDtos = await results.ReadAsync<MoveDto>();
|
||||
foreach (var move in moveDtos)
|
||||
{
|
||||
if (move.PieceFromHand.HasValue)
|
||||
{
|
||||
session.Board.Move(move.PieceFromHand.Value, move.To);
|
||||
}
|
||||
else if (move.From != null)
|
||||
{
|
||||
session.Board.Move(move.From, move.To, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"Corrupt data during {nameof(ReadSession)}");
|
||||
}
|
||||
}
|
||||
return session;
|
||||
}
|
||||
return new(sessionDtos.First(), moveDtos);
|
||||
}
|
||||
|
||||
public async Task CreateMove(string sessionName, MovePieceCommand command)
|
||||
{
|
||||
var yep = new
|
||||
{
|
||||
command.To,
|
||||
command.From,
|
||||
command.IsPromotion,
|
||||
command.PieceFromHand,
|
||||
SessionName = sessionName
|
||||
};
|
||||
public async Task CreateMove(string sessionId, MovePieceCommand command)
|
||||
{
|
||||
using var connection = new SqlConnection(this.connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.CreateMove",
|
||||
new
|
||||
{
|
||||
command.To,
|
||||
command.From,
|
||||
command.IsPromotion,
|
||||
PieceFromHand = command.PieceFromHand.ToString(),
|
||||
SessionId = sessionId
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.CreateMove",
|
||||
new
|
||||
{
|
||||
command.To,
|
||||
command.From,
|
||||
command.IsPromotion,
|
||||
PieceFromHand = command.PieceFromHand.ToString(),
|
||||
SessionName = sessionName
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task SetPlayer2(string sessionName, string player2Name)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.SetPlayer2",
|
||||
new
|
||||
{
|
||||
SessionName = sessionName,
|
||||
Player2Name = player2Name
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISessionRepository
|
||||
{
|
||||
Task CreateMove(string sessionName, MovePieceCommand command);
|
||||
Task CreateSession(Session session);
|
||||
Task DeleteSession(string name);
|
||||
Task<Session?> ReadSession(string name);
|
||||
Task SetPlayer2(string sessionName, string player2Name);
|
||||
public async Task SetPlayer2(string sessionId, string player2Id)
|
||||
{
|
||||
using var connection = new SqlConnection(this.connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.SetPlayer2",
|
||||
new
|
||||
{
|
||||
SessionId = sessionId,
|
||||
PlayerId = player2Id
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using Dapper;
|
||||
using Shogi.Api.Models;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace Shogi.Api.Repositories;
|
||||
|
||||
public class UserRepository : IUserRepository
|
||||
{
|
||||
private readonly string connectionString;
|
||||
|
||||
public UserRepository(IConfiguration configuration)
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString("ShogiDatabase");
|
||||
if (string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
throw new InvalidOperationException("Connection string for database is empty.");
|
||||
}
|
||||
this.connectionString = connectionString;
|
||||
}
|
||||
|
||||
public async Task CreateUser(User user)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"user.CreateUser",
|
||||
new
|
||||
{
|
||||
Name = user.Id,
|
||||
DisplayName = user.DisplayName,
|
||||
Platform = user.LoginPlatform.ToString()
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task<User?> ReadUser(string id)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
var results = await connection.QueryAsync<User>(
|
||||
"user.ReadUser",
|
||||
new { Name = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IUserRepository
|
||||
{
|
||||
Task CreateUser(User user);
|
||||
Task<User?> ReadUser(string id);
|
||||
}
|
||||
Reference in New Issue
Block a user