-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Azure Cosmos DB integration, saving completed games
- Loading branch information
Showing
11 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Battleships.Common.CosmosDb | ||
{ | ||
public class GameSession | ||
{ | ||
[JsonProperty("id")] | ||
public string Id { get; set; } | ||
|
||
[JsonProperty("sessionId")] | ||
public string SessionId { get; set; } | ||
|
||
[JsonProperty("gameState")] | ||
public string GameStateJson { get; set; } | ||
|
||
[JsonProperty("dateCreated")] | ||
public DateTime DateCreated { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Battleships.Common.Settings | ||
{ | ||
public class CosmosDbSettings | ||
{ | ||
public string Account { get; set; } | ||
public string Key { get; set; } | ||
public string DatabaseName { get; set; } | ||
public string ContainerName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Battleships.Common.CosmosDb; | ||
using Battleships.Common.GameClasses; | ||
|
||
namespace Battleships.Services.Interfaces | ||
{ | ||
public interface ICosmosDbService | ||
{ | ||
Task AddGameSessionAsync(GameSession gameSession); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Battleships.Common.CosmosDb; | ||
using Battleships.Common.Settings; | ||
using Battleships.Services.Interfaces; | ||
using Microsoft.Azure.Cosmos; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Battleships.Core.Services | ||
{ | ||
public class CosmosDbService : ICosmosDbService | ||
{ | ||
private readonly Container _container; | ||
private readonly ILogger<CosmosDbService> _logger; | ||
|
||
public CosmosDbService(ILogger<CosmosDbService> logger, CosmosDbSettings settings, CosmosClient dbClient) | ||
{ | ||
_logger = logger; | ||
_container = dbClient.GetContainer(settings.DatabaseName, settings.ContainerName); | ||
} | ||
|
||
public async Task AddGameSessionAsync(GameSession gameSession) | ||
{ | ||
await _container.CreateItemAsync(gameSession, new PartitionKey(gameSession.SessionId)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Battleships.Common.Settings; | ||
using Battleships.Core.Services; | ||
using Microsoft.Azure.Cosmos; | ||
|
||
namespace Battleships.WebApi | ||
{ | ||
public static class ServiceCollectionSetup | ||
{ | ||
public static void InitializeCosmosClientInstanceAsync(CosmosDbSettings settings, IServiceCollection services) | ||
{ | ||
CosmosClient client = new CosmosClient(settings.Account, settings.Key); | ||
services.AddSingleton(client); | ||
|
||
services.AddSingleton(serviceProvider => | ||
{ | ||
var logger = serviceProvider.GetRequiredService<ILogger<CosmosDbService>>(); | ||
return new CosmosDbService(logger, settings, client); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters