All the code
This commit is contained in:
127
Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs
Normal file
127
Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Websockets.Repositories.Utility;
|
||||
|
||||
namespace Websockets.Repositories
|
||||
{
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task DeleteGame(string gameName);
|
||||
Task<GetGameResponse> GetGame(string gameName);
|
||||
Task<GetGamesResponse> GetGames();
|
||||
Task<GetGamesResponse> GetGames(string playerName);
|
||||
Task<GetMovesResponse> GetMoves(string gameName);
|
||||
Task<PostGameResponse> PostGame(PostGame request);
|
||||
Task<PostJoinByCodeResponse> PostJoinByCode(PostJoinByCode request);
|
||||
Task<PostJoinGameResponse> PostJoinGame(string gameName, PostJoinGame request);
|
||||
Task PostMove(string gameName, PostMove request);
|
||||
Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName);
|
||||
Task<GetPlayerResponse> GetPlayer(string userName);
|
||||
Task<HttpResponseMessage> PostPlayer(PostPlayer request);
|
||||
}
|
||||
|
||||
public class GameboardRepository : IGameboardRepository
|
||||
{
|
||||
private readonly IAuthenticatedHttpClient client;
|
||||
public GameboardRepository(IAuthenticatedHttpClient client)
|
||||
{
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public async Task<GetGamesResponse> GetGames()
|
||||
{
|
||||
var response = await client.GetAsync("Games");
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetGamesResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetGamesResponse> GetGames(string playerName)
|
||||
{
|
||||
var uri = $"Games/{playerName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetGamesResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetGameResponse> GetGame(string gameName)
|
||||
{
|
||||
var uri = $"Game/{gameName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetGameResponse>(json);
|
||||
}
|
||||
|
||||
public async Task DeleteGame(string gameName)
|
||||
{
|
||||
var uri = $"Game/{gameName}";
|
||||
await client.DeleteAsync(Uri.EscapeUriString(uri));
|
||||
}
|
||||
|
||||
public async Task<PostGameResponse> PostGame(PostGame request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("Game", content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostGameResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<PostJoinGameResponse> PostJoinGame(string gameName, PostJoinGame request)
|
||||
{
|
||||
var uri = $"Game/{gameName}/Join";
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostJoinGameResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<PostJoinByCodeResponse> PostJoinByCode(PostJoinByCode request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("Game/Join", content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostJoinByCodeResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetMovesResponse> GetMoves(string gameName)
|
||||
{
|
||||
var uri = $"Game/{gameName}/Moves";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetMovesResponse>(json);
|
||||
}
|
||||
|
||||
public async Task PostMove(string gameName, PostMove request)
|
||||
{
|
||||
var uri = $"Game/{gameName}/Move";
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
}
|
||||
|
||||
public async Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName)
|
||||
{
|
||||
var uri = $"JoinCode/{gameName}";
|
||||
var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName });
|
||||
var content = new StringContent(serialized, Encoding.UTF8, "application/json");
|
||||
var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetPlayerResponse> GetPlayer(string playerName)
|
||||
{
|
||||
var uri = $"Player/{playerName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetPlayerResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> PostPlayer(PostPlayer request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
return await client.PostAsync("Player", content);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user