Пример #1
0
        public async Task SaveMatch(PickDto pick, string username, int userPick, Result result)
        {
            var dto = MatchDto.FromPickDto(pick);

            dto.PlayerName       = username;
            dto.PlayerMove.Text  = PickDto.ToText(userPick);
            dto.PlayerMove.Value = userPick;
            dto.Result.Value     = (int)result;
            dto.Result.Winner    = Enum.GetName(typeof(Result), result);
            if (_constr == null)
            {
                _logger.LogInformation("+++ Cosmos constr is null. Doc that would be written is:");
                _logger.LogInformation(JsonSerializer.Serialize(dto));
                _logger.LogInformation("+++ Nothing was written on Cosmos");
                return;
            }

            var cResponse = await GetContainer();

            var response = await cResponse.Container.CreateItemAsync(dto);

            if (response.StatusCode != System.Net.HttpStatusCode.OK &&
                response.StatusCode != System.Net.HttpStatusCode.Created)
            {
                _logger.LogInformation($"Cosmos save attempt resulted with StatusCode {response.StatusCode}.");
            }
        }
Пример #2
0
        public async Task SaveMatch(PickDto pick, string username, int userPick, GameApi.Proto.Result result)
        {
            var dto = MatchDto.FromPickDto(pick);

            dto.PlayerLogged     = false;
            dto.PlayerName       = username;
            dto.PlayerMove.Text  = ToText(userPick);
            dto.PlayerMove.Value = userPick;
            dto.Result.Value     = (int)result;
            dto.Result.Winner    = Enum.GetName(typeof(GameApi.Proto.Result), result);

            if (_constr == null)
            {
                _logger.LogInformation("+++ Cosmos constr is null. Doc that would be written is:");
                _logger.LogInformation(JsonSerializer.Serialize(dto));
                _logger.LogInformation("+++ Nothing was written on Cosmos");
                return;
            }
            var client = new CosmosClient(_constr);
            var db     = client.GetDatabase(DatabaseName);
            var cprops = new ContainerProperties()
            {
                Id = "results",
                PartitionKeyPath = "/playerName"
            };
            var cResponse = await db.CreateContainerIfNotExistsAsync(cprops);

            var response = await cResponse.Container.CreateItemAsync(dto);

            if (response.StatusCode != System.Net.HttpStatusCode.OK &&
                response.StatusCode != System.Net.HttpStatusCode.Created)
            {
                _logger.LogInformation($"Cosmos save attempt resulted with StatusCode {response.StatusCode}.");
            }
        }