68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using GameData.Model;
|
|
using GameModel;
|
|
using GameModel.Contracts;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
namespace GameData.Repository
|
|
{
|
|
public class GameRepository : IGameRepository
|
|
{
|
|
readonly ILogger<GameRepository> _log;
|
|
private ApiClient _client;
|
|
|
|
string UrlGameState => "items/gamestate";
|
|
string UrlGame => "items/game";
|
|
|
|
public GameRepository(ILogger<GameRepository> log, ApiClient apiClient)
|
|
{
|
|
_log = log;
|
|
_client = apiClient;
|
|
}
|
|
|
|
public GameState Load(Guid gameId)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<GameState> Save(GameState gameState)
|
|
{
|
|
var str = JsonConvert.SerializeObject(gameState, new JsonSerializerSettings()
|
|
{
|
|
TypeNameHandling = TypeNameHandling.Auto
|
|
});
|
|
var gameStateDo = new GameStateDo(gameState.Id, gameState.GameId, gameState.GameName, str, gameState.Counter);
|
|
var res = await _client.Post<GameStateDo>(gameStateDo,UrlGameState);
|
|
|
|
|
|
var obj = JsonConvert.DeserializeObject<GameState>(res.GameState, new JsonSerializerSettings()
|
|
{
|
|
TypeNameHandling = TypeNameHandling.Auto
|
|
});
|
|
|
|
return obj;
|
|
}
|
|
|
|
public async Task Update(Game game)
|
|
{
|
|
await _client.Put<Game>(game, UrlGame);
|
|
}
|
|
|
|
public async Task<Game> Create(Game game)
|
|
{
|
|
return await _client.Post<Game>(game, UrlGame);
|
|
}
|
|
|
|
public async Task<Game> LoadGame(Guid gameId)
|
|
{
|
|
return await _client.GetSingle<Game>(UrlGame + "/" + gameId);
|
|
}
|
|
}
|
|
}
|